-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(NotificationContext) add notification provider consumer and hook…
… WD-4256
- Loading branch information
Showing
11 changed files
with
468 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/components/NotificationProvider/NotificationProvider.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import React from "react"; | ||
import { render, screen } from "@testing-library/react"; | ||
import userEvent from "@testing-library/user-event"; | ||
|
||
import { | ||
NotificationConsumer, | ||
NotificationProvider, | ||
useNotify, | ||
} from "./NotificationProvider"; | ||
import { BrowserRouter } from "react-router-dom"; | ||
import Button from "../Button"; | ||
import { act } from "react-dom/test-utils"; | ||
|
||
describe("NotificationProvider", () => { | ||
it("stores and renders notifications", async () => { | ||
const handleRetry = jest.fn(); | ||
|
||
const TriggerComponent = () => { | ||
const notify = useNotify(); | ||
const handleSuccess = () => notify.success("My success!"); | ||
const handleClear = () => notify.clear(); | ||
const handleInfo = () => notify.info("Some more details", "Test info"); | ||
const handleError = () => | ||
notify.failure( | ||
"Fail title", | ||
new Error("error message"), | ||
"Button caused a failure", | ||
[ | ||
{ | ||
label: "Retry", | ||
onClick: handleRetry, | ||
}, | ||
] | ||
); | ||
|
||
return ( | ||
<div> | ||
<Button onClick={handleSuccess} data-testid="success-btn" /> | ||
<Button onClick={handleClear} data-testid="clear-btn" /> | ||
<Button onClick={handleError} data-testid="error-btn" /> | ||
<Button onClick={handleInfo} data-testid="info-btn" /> | ||
</div> | ||
); | ||
}; | ||
|
||
render( | ||
<div> | ||
<NotificationProvider> | ||
<TriggerComponent /> | ||
<div data-testid="notification-consumer"> | ||
<NotificationConsumer /> | ||
</div> | ||
</NotificationProvider> | ||
</div>, | ||
{ wrapper: BrowserRouter } | ||
); | ||
|
||
const clickBtn = async (testId: string) => | ||
await act(async () => await userEvent.click(screen.getByTestId(testId))); | ||
|
||
expect(screen.getByTestId("notification-consumer")).toBeEmptyDOMElement(); | ||
|
||
await clickBtn("success-btn"); | ||
expect(screen.getByTestId("notification-consumer")).toMatchSnapshot(); | ||
|
||
await clickBtn("clear-btn"); | ||
expect(screen.getByTestId("notification-consumer")).toBeEmptyDOMElement(); | ||
|
||
await clickBtn("error-btn"); | ||
expect(screen.getByTestId("notification-consumer")).toMatchSnapshot(); | ||
|
||
expect(handleRetry).not.toHaveBeenCalled(); | ||
await clickBtn("notification-action"); | ||
expect(handleRetry).toHaveBeenCalled(); | ||
|
||
await clickBtn("info-btn"); | ||
expect(screen.getByTestId("notification-consumer")).toMatchSnapshot(); | ||
}); | ||
}); |
103 changes: 103 additions & 0 deletions
103
src/components/NotificationProvider/NotificationProvider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import React, { | ||
createContext, | ||
FC, | ||
useContext, | ||
useEffect, | ||
useRef, | ||
useState, | ||
} from "react"; | ||
import { | ||
NotificationType, | ||
NotificationHelper, | ||
QueuedNotification, | ||
NotifyProviderProps, | ||
} from "./types"; | ||
import { useLocation } from "react-router-dom"; | ||
import isEqual from "lodash/isEqual"; | ||
import { info, failure, success, queue } from "./messageBuilder"; | ||
import Notification, { DefaultTitles } from "../Notification/Notification"; | ||
|
||
const NotifyContext = createContext<NotificationHelper>({ | ||
notification: null, | ||
clear: () => undefined, | ||
failure: () => undefined, | ||
success: () => undefined, | ||
info: () => undefined, | ||
queue: () => undefined, | ||
}); | ||
|
||
export const NotificationProvider: FC<NotifyProviderProps> = ({ children }) => { | ||
const [notification, setNotification] = useState<NotificationType | null>( | ||
null | ||
); | ||
const { state, pathname } = useLocation() as QueuedNotification; | ||
|
||
const clear = () => notification !== null && setNotification(null); | ||
|
||
const setDeduplicated = (value: NotificationType) => { | ||
if (!isEqual(value, notification)) { | ||
setNotification(value); | ||
} | ||
return value; | ||
}; | ||
|
||
useEffect(() => { | ||
if (state?.queuedNotification) { | ||
setDeduplicated(state.queuedNotification); | ||
window.history.replaceState({}, ""); | ||
} else { | ||
clear(); | ||
} | ||
}, [state, pathname]); | ||
|
||
const helper: NotificationHelper = { | ||
notification, | ||
clear, | ||
queue, | ||
failure: (title, error, message, actions) => | ||
setDeduplicated(failure(title, error, message, actions)), | ||
info: (message, title) => setDeduplicated(info(message, title)), | ||
success: (message) => setDeduplicated(success(message)), | ||
}; | ||
|
||
return ( | ||
<NotifyContext.Provider value={helper}>{children}</NotifyContext.Provider> | ||
); | ||
}; | ||
|
||
export function useNotify() { | ||
return useContext(NotifyContext); | ||
} | ||
|
||
export const NotificationConsumer: FC = () => { | ||
const notify = useNotify(); | ||
const ref = useRef<HTMLDivElement>(null); | ||
|
||
useEffect(() => { | ||
if (ref.current?.hasAttribute("scrollIntoView")) { | ||
ref.current.scrollIntoView({ | ||
behavior: "smooth", | ||
block: "center", | ||
inline: "start", | ||
}); | ||
} | ||
}, [notify.notification]); | ||
|
||
if (!notify.notification) { | ||
return null; | ||
} | ||
const { actions, title, type, message } = notify.notification; | ||
|
||
return ( | ||
<div ref={ref}> | ||
<Notification | ||
title={title ?? DefaultTitles[type]} | ||
actions={actions} | ||
severity={type} | ||
onDismiss={notify.clear} | ||
> | ||
{message} | ||
</Notification> | ||
</div> | ||
); | ||
}; |
120 changes: 120 additions & 0 deletions
120
src/components/NotificationProvider/__snapshots__/NotificationProvider.test.tsx.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`NotificationProvider stores and renders notifications 1`] = ` | ||
<div | ||
data-testid="notification-consumer" | ||
> | ||
<div> | ||
<div | ||
class="p-notification--positive" | ||
> | ||
<div | ||
class="p-notification__content" | ||
> | ||
<h5 | ||
class="p-notification__title" | ||
data-testid="notification-title" | ||
> | ||
Success | ||
</h5> | ||
<p | ||
class="p-notification__message" | ||
> | ||
My success! | ||
</p> | ||
<button | ||
class="p-notification__close" | ||
data-testid="notification-close-button" | ||
> | ||
Close notification | ||
</button> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
`; | ||
|
||
exports[`NotificationProvider stores and renders notifications 2`] = ` | ||
<div | ||
data-testid="notification-consumer" | ||
> | ||
<div> | ||
<div | ||
class="p-notification--negative" | ||
> | ||
<div | ||
class="p-notification__content" | ||
> | ||
<h5 | ||
class="p-notification__title" | ||
data-testid="notification-title" | ||
> | ||
Fail title | ||
</h5> | ||
<p | ||
class="p-notification__message" | ||
> | ||
Button caused a failure | ||
error message | ||
</p> | ||
<button | ||
class="p-notification__close" | ||
data-testid="notification-close-button" | ||
> | ||
Close notification | ||
</button> | ||
</div> | ||
<div | ||
class="p-notification__meta" | ||
data-testid="notification-meta" | ||
> | ||
<div | ||
class="p-notification__actions" | ||
> | ||
<button | ||
class="p-button--link p-notification__action" | ||
data-testid="notification-action" | ||
> | ||
Retry | ||
</button> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
`; | ||
|
||
exports[`NotificationProvider stores and renders notifications 3`] = ` | ||
<div | ||
data-testid="notification-consumer" | ||
> | ||
<div> | ||
<div | ||
class="p-notification--information" | ||
> | ||
<div | ||
class="p-notification__content" | ||
> | ||
<h5 | ||
class="p-notification__title" | ||
data-testid="notification-title" | ||
> | ||
Test info | ||
</h5> | ||
<p | ||
class="p-notification__message" | ||
> | ||
Some more details | ||
</p> | ||
<button | ||
class="p-notification__close" | ||
data-testid="notification-close-button" | ||
> | ||
Close notification | ||
</button> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export { | ||
NotificationConsumer, | ||
NotificationProvider, | ||
useNotify, | ||
} from "./NotificationProvider"; | ||
export { info, success, failure, queue } from "./messageBuilder"; | ||
export type { | ||
NotificationAction, | ||
NotificationType, | ||
QueuedNotification, | ||
NotificationHelper, | ||
} from "./types"; |
Oops, something went wrong.