-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: add useToast hook for toast notifications #3593
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5ea971b
Basic useToast hook plus success toast on addNewEditor modal
jamdelion 86dbdf4
Add types into own file
jamdelion 287f3ba
Fix other type issues
jamdelion cefb523
Type fix
jamdelion d5a2412
Merge branch 'main' into jh/use-toast-hook
jamdelion 1a691e0
Add default context value
jamdelion 1fff00c
Use useToast hook everywhere
jamdelion 5bec72b
Merge branch 'main' into jh/use-toast-hook
jamdelion 697f67d
Tidy no existing members snackbar
jamdelion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,28 @@ | ||
import Alert from "@mui/material/Alert"; | ||
import Snackbar from "@mui/material/Snackbar"; | ||
import { useToast } from "hooks/useToast"; | ||
import React from "react"; | ||
|
||
import { Toast as ToastProps } from "./types"; | ||
|
||
const Toast = ({ message, type = "success", id }: ToastProps) => { | ||
const toast = useToast(); | ||
|
||
const handleCloseToast = () => { | ||
if (toast) { | ||
toast.remove(id); | ||
} else { | ||
console.warn("ToastContext is not provided."); | ||
} | ||
}; | ||
|
||
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,16 @@ | ||
import React from "react"; | ||
|
||
import Toast from "./Toast"; | ||
import { Toast as ToastComponent } from "./types"; | ||
|
||
const ToastContainer = ({ toasts }: { toasts: ToastComponent[] }) => { | ||
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,31 @@ | ||
export interface Toast { | ||
message: string; | ||
type: ToastType; | ||
id: number; | ||
} | ||
export type ToastType = "success" | "warning" | "info" | "error"; | ||
|
||
export type ToastState = { | ||
toasts: Toast[]; | ||
}; | ||
|
||
export type ToastAction = AddToast | DeleteToast; | ||
|
||
type AddToast = { | ||
type: "ADD_TOAST"; | ||
payload: Toast; | ||
}; | ||
|
||
type DeleteToast = { | ||
type: "DELETE_TOAST"; | ||
payload: { id: number }; | ||
}; | ||
|
||
export type ToastContextType = { | ||
addToast: (type: ToastType, message: string) => void; | ||
remove: (id: number) => void; | ||
success: (message: string) => void; | ||
warning: (message: string) => void; | ||
info: (message: string) => void; | ||
error: (message: string) => void; | ||
}; |
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,70 @@ | ||
/* eslint-disable @typescript-eslint/no-empty-function */ | ||
import ToastContainer from "components/Toast/ToastContainer"; | ||
import { | ||
ToastContextType, | ||
ToastState, | ||
ToastType, | ||
} from "components/Toast/types"; | ||
import React, { createContext, ReactNode, useReducer } from "react"; | ||
import { toastReducer } from "reducers/toastReducer"; | ||
|
||
const defaultCreateContextValue = { | ||
remove: (_id: number) => {}, | ||
addToast: (_type: ToastType, _message: string) => {}, | ||
success: (_message: string) => {}, | ||
warning: (_message: string) => {}, | ||
info: (_message: string) => {}, | ||
error: (_message: string) => {}, | ||
}; | ||
|
||
export const ToastContext = createContext<ToastContextType>( | ||
defaultCreateContextValue, | ||
); | ||
|
||
const initialState: ToastState = { | ||
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: ToastContextType = { | ||
success, | ||
warning, | ||
info, | ||
error, | ||
remove, | ||
addToast, | ||
}; | ||
|
||
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: we also have the
uuid
package available on the frontend which would let you generate a unique uuid id likeconst id = uuidV4()
(consistent with how we generate session ids) - happy for this to work either way though / chances of unintentionally colliding/non-unique IDs seems veryyy low here 🙂There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice tip thanks! I'll include that in the next PR 👍