-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic useToast hook plus success toast on addNewEditor modal
- Loading branch information
Showing
10 changed files
with
129 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import Alert from "@mui/material/Alert"; | ||
import Snackbar from "@mui/material/Snackbar"; | ||
import { useToast } from "hooks/useToast"; | ||
import React from "react"; | ||
|
||
interface ToastProps { | ||
message: string; | ||
type: ToastType; | ||
id: number; | ||
} | ||
|
||
export type ToastType = "success" | "warning" | "info" | "error"; | ||
|
||
const Toast = ({ message, type = "success", id }: ToastProps) => { | ||
const toast = useToast(); | ||
|
||
const handleCloseToast = () => { | ||
toast.remove(id); | ||
}; | ||
|
||
return ( | ||
<Snackbar onClose={handleCloseToast} autoHideDuration={6000} open={true}> | ||
<Alert onClose={handleCloseToast} severity={type} sx={{ width: "100%" }}> | ||
{message} | ||
</Alert> | ||
</Snackbar> | ||
); | ||
}; | ||
|
||
export default Toast; |
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,15 @@ | ||
import React from "react"; | ||
|
||
import Toast from "./Toast"; | ||
|
||
const ToastContainer = ({ toasts }) => { | ||
return ( | ||
<div className="toasts-container"> | ||
{toasts.map((toast) => ( | ||
<Toast key={toast.id} {...toast} /> | ||
))} | ||
</div> | ||
); | ||
}; | ||
|
||
export default ToastContainer; |
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,47 @@ | ||
import { ToastType } from "components/Toast/Toast"; | ||
import ToastContainer from "components/Toast/ToastContainer"; | ||
import React, { createContext, ReactNode, useReducer } from "react"; | ||
import { toastReducer } from "reducers/toastReducer"; | ||
|
||
export const ToastContext = createContext(null); | ||
|
||
const initialState = { | ||
toasts: [], | ||
}; | ||
|
||
export const ToastContextProvider = ({ | ||
children, | ||
}: Readonly<{ children: ReactNode }>) => { | ||
const [state, dispatch] = useReducer(toastReducer, initialState); | ||
const addToast = (type: ToastType, message: string) => { | ||
const id = Math.floor(Math.random() * 10_000_000); | ||
dispatch({ type: "ADD_TOAST", payload: { id, message, type } }); | ||
}; | ||
const remove = (id: number) => { | ||
dispatch({ type: "DELETE_TOAST", payload: id }); | ||
}; | ||
const success = (message: string) => { | ||
addToast("success", message); | ||
}; | ||
|
||
const warning = (message: string) => { | ||
addToast("warning", message); | ||
}; | ||
|
||
const info = (message: string) => { | ||
addToast("info", message); | ||
}; | ||
|
||
const error = (message: string) => { | ||
addToast("error", message); | ||
}; | ||
|
||
const value = { success, warning, info, error, remove }; | ||
|
||
return ( | ||
<ToastContext.Provider value={value}> | ||
<ToastContainer toasts={state.toasts} /> | ||
{children} | ||
</ToastContext.Provider> | ||
); | ||
}; |
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 @@ | ||
import { useContext } from "react"; | ||
|
||
import { ToastContext } from "../contexts/ToastContext"; | ||
|
||
export const useToast = () => useContext(ToastContext); |
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
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,20 @@ | ||
export const toastReducer = (state, action) => { | ||
switch (action.type) { | ||
case "ADD_TOAST": | ||
return { | ||
...state, | ||
toasts: [...state.toasts, action.payload], | ||
}; | ||
case "DELETE_TOAST": { | ||
const updatedToasts = state.toasts.filter( | ||
(toast) => toast.id !== action.payload, | ||
); | ||
return { | ||
...state, | ||
toasts: updatedToasts, | ||
}; | ||
} | ||
default: | ||
throw new Error(`Unhandled action type: ${action.type}`); | ||
} | ||
}; |