forked from keep-starknet-strange/madara-tsukuyomi
-
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.
feat: add error boundary and snackbar store (keep-starknet-strange#15)
* feat: add error boundary and snackbar store * chore: add placeholder for logging service * chore: make toast styles constant * chore: shift toast options to global level
- Loading branch information
Showing
7 changed files
with
116 additions
and
15 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,39 @@ | ||
import React from 'react'; | ||
import { toast } from 'react-toastify'; | ||
import defaultToastStyleOptions from 'shared/constants'; | ||
|
||
interface Props { | ||
children: React.ReactNode; | ||
} | ||
|
||
interface State { | ||
error: boolean | Error; | ||
} | ||
|
||
class ErrorBoundary extends React.Component<Props, State> { | ||
constructor(props: Props) { | ||
super(props); | ||
this.state = { error: false }; | ||
} | ||
|
||
static getDerivedStateFromError(error: Error): object { | ||
return { error }; | ||
} | ||
|
||
// componentDidCatch(error: Error, errorInfo: any): void { | ||
// // add our error logging service here | ||
// // errorLoggingService(error,errorInfo) | ||
// } | ||
|
||
render(): React.ReactNode { | ||
const { children } = this.props; | ||
const { error } = this.state; | ||
if (error) { | ||
toast('Something went wrong', defaultToastStyleOptions); | ||
} | ||
|
||
return children; | ||
} | ||
} | ||
|
||
export default ErrorBoundary; |
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,39 @@ | ||
import { createSlice } from '@reduxjs/toolkit'; | ||
import { toast, ToastOptions } from 'react-toastify'; | ||
import defaultToastStyleOptions from 'shared/constants'; | ||
|
||
const initialState = { | ||
isVisible: false, | ||
}; | ||
|
||
const snackbarSlice = createSlice({ | ||
name: 'snackbar', | ||
initialState, | ||
// The `reducers` field lets us define reducers and generate associated actions | ||
reducers: { | ||
showToast: (state) => { | ||
state.isVisible = true; | ||
}, | ||
hideToast: (state) => { | ||
state.isVisible = false; | ||
}, | ||
}, | ||
}); | ||
|
||
const { showToast, hideToast } = snackbarSlice.actions; | ||
|
||
export const showSnackbar = (message?: string) => (dispatch: any) => { | ||
dispatch(showToast()); | ||
const TOAST_STYLE: ToastOptions = { | ||
...defaultToastStyleOptions, | ||
autoClose: 2000, | ||
}; | ||
toast(message ?? 'Something went wrong', { ...TOAST_STYLE }); | ||
}; | ||
|
||
export const hideSnackbar = () => (dispatch: any) => { | ||
dispatch(hideToast()); | ||
toast.dismiss(); | ||
}; | ||
|
||
export default snackbarSlice.reducer; |
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,12 @@ | ||
import { ToastOptions } from 'react-toastify'; | ||
|
||
const defaultToastStyleOptions: ToastOptions = { | ||
position: 'bottom-center', | ||
hideProgressBar: true, | ||
closeOnClick: false, | ||
pauseOnHover: false, | ||
draggable: false, | ||
theme: 'dark', | ||
}; | ||
|
||
export default defaultToastStyleOptions; |