-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
210 additions
and
37 deletions.
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
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 |
|---|---|---|
| @@ -1,10 +1,5 @@ | ||
| function uuidv4() { | ||
| return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { | ||
| var r = Math.random() * 16 | 0, v = c === 'x' ? r : ((r & 0x3) | 0x8); | ||
| return v.toString(16); | ||
| }); | ||
| } | ||
| import {uuid} from '../utility/uuid'; | ||
|
|
||
| export function useGuid() { | ||
| return uuidv4(); | ||
| return uuid(); | ||
| } |
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,6 @@ | ||
| export function uuid() { | ||
| return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { | ||
| var r = Math.random() * 16 | 0, v = c === 'x' ? r : ((r & 0x3) | 0x8); | ||
| return v.toString(16); | ||
| }); | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import React from 'react'; | ||
| import { Alert } from 'reactstrap'; | ||
| import { NotificationContext, removeNotificationAction } from '../hoc/Notifications'; | ||
|
|
||
| export function NotificationItem ({ type, body, timeout, id }) { | ||
|
|
||
| const { dispatch } = React.useContext(NotificationContext); | ||
|
|
||
| React.useEffect(() => { | ||
| if (timeout) { | ||
| setTimeout(() => { | ||
| dispatch(removeNotificationAction(id)); | ||
| }, timeout); | ||
| } | ||
| }, [timeout, id, dispatch]); | ||
|
|
||
| return ( | ||
| <Alert color={type} toggle={() => dispatch(removeNotificationAction(id))}> | ||
| {body} | ||
| </Alert> | ||
| ) | ||
| } |
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,18 @@ | ||
| import React from 'react'; | ||
| import { NotificationContext } from '../hoc/Notifications'; | ||
| import { NotificationItem } from './NotificationItem'; | ||
|
|
||
| export function NotificationList () { | ||
|
|
||
| const { state } = React.useContext(NotificationContext); | ||
|
|
||
| return ( | ||
| <ul className="notification-list list-unstyled position-fixed p-4 w-25"> | ||
| {state.notifications.map((n) => ( | ||
| <li key={n.id}> | ||
| <NotificationItem id={n.id} type={n.type} body={n.body} timeout={n.timeout} /> | ||
| </li> | ||
| ))} | ||
| </ul> | ||
| ); | ||
| } |
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,82 @@ | ||
| import React from "react"; | ||
| import { uuid } from '../../core/utility/uuid'; | ||
|
|
||
| const NotificationContext = React.createContext(); | ||
|
|
||
| const { Provider, Consumer } = NotificationContext; | ||
|
|
||
| const initialState = { | ||
| notifications: [] | ||
| }; | ||
|
|
||
| export const NotificationActions = { | ||
| ADD_NOTIFICATION: 'add notification', | ||
| REMOVE_NOTIFICATION: 'remove notification' | ||
| }; | ||
|
|
||
| export const NotificationTypes = { | ||
| SUCCESS: 'success', | ||
| DANGER: 'danger', | ||
| ERROR: 'danger', | ||
| WARNING: 'warn', | ||
| INFO: 'info' | ||
| }; | ||
|
|
||
| export const createNotificationAction = (body, type = NotificationTypes.SUCCESS, timeout=20000) => { | ||
| return { | ||
| type: NotificationActions.ADD_NOTIFICATION, | ||
| payload: { | ||
| id: uuid(), | ||
| type, | ||
| body, | ||
| timeout | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export const removeNotificationAction = (id) => { | ||
| return { | ||
| type: NotificationActions.REMOVE_NOTIFICATION, | ||
| payload: id | ||
| } | ||
| } | ||
|
|
||
| function reducer(state, action) { | ||
| switch (action.type) { | ||
| case NotificationActions.ADD_NOTIFICATION: | ||
| return { | ||
| notifications: [ | ||
| ...state.notifications, | ||
| { | ||
| ...action.payload | ||
| } | ||
| ] | ||
| }; | ||
| case NotificationActions.REMOVE_NOTIFICATION: | ||
| return { | ||
| notifications: [ | ||
| ...state.notifications.filter(n => n.id !== action.payload) | ||
| ] | ||
| }; | ||
| default: | ||
| throw new Error(); | ||
| } | ||
| } | ||
|
|
||
| /*eslint-disable react-hooks/exhaustive-deps*/ | ||
| function Notifications ({ children }) { | ||
| const [state, dispatch] = React.useReducer(reducer, initialState); | ||
|
|
||
| const contextValue = React.useMemo(() => ({ state, dispatch }), [state, dispatch]); | ||
|
|
||
| return ( | ||
| <Provider value={contextValue}>{children}</Provider> | ||
| ); | ||
| } | ||
|
|
||
| export { | ||
| Notifications, | ||
| NotificationContext, | ||
| Provider as NotificationProvider, | ||
| Consumer as NotificationConsumer | ||
| }; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| .notification-list { | ||
| bottom: 50px; | ||
| right: 0px; | ||
| z-index: 2000; | ||
| } |