11import { createContext , FC , ReactNode , useCallback , useContext , useState } from 'react'
22
3+ import type { Error as ErrorProps } from '../models'
4+
5+ import { ErrorComp } from '../components'
6+
7+ import { useCustomRender } from './config'
8+
39interface ErrorDetails {
410 title : string
511 description : string
12+ statusCode ?: number
613}
714
815interface ErrorDisplayProps extends ErrorDetails {
916 children ?: ReactNode
1017}
1118
12- export type ErrorDisplayType = FC < ErrorDisplayProps >
13-
1419interface ErrorContextType {
1520 error : ErrorDetails | null
1621 setError : ( error : ErrorDetails | null ) => void
17- DisplayError : ErrorDisplayType
1822}
1923
20- const DefaultErrorDisplay : ErrorDisplayType = ( { title, description, children } ) => (
21- < >
22- < div className = "alert alert-danger m-3" role = "alert" >
23- < h4 > { title } </ h4 >
24- { description }
25- </ div >
26- { children }
27- </ >
28- )
24+ export const DisplayError : FC < ErrorDisplayProps > = ( { title, description, statusCode, children } ) => {
25+ const props : ErrorProps = {
26+ title,
27+ description,
28+ statusCode,
29+ children,
30+ type : 'Error' ,
31+ }
32+ const CustomRenderComp = useCustomRender ( props )
33+ if ( CustomRenderComp ) {
34+ return < CustomRenderComp />
35+ } else {
36+ return < ErrorComp { ...props } />
37+ }
38+ }
2939
3040export const ErrorContext = createContext < ErrorContextType > ( {
3141 error : null ,
3242 setError : ( ) => null ,
33- DisplayError : DefaultErrorDisplay ,
3443} )
3544
3645const MaybeError : FC < { children : ReactNode } > = ( { children } ) => {
37- const { error, DisplayError } = useContext ( ErrorContext )
46+ const { error } = useContext ( ErrorContext )
3847 if ( error ) {
3948 return < DisplayError { ...error } > { children } </ DisplayError >
4049 } else {
@@ -43,11 +52,10 @@ const MaybeError: FC<{ children: ReactNode }> = ({ children }) => {
4352}
4453
4554interface Props {
46- DisplayError ?: ErrorDisplayType
4755 children : ReactNode
4856}
4957
50- export const ErrorContextProvider : FC < Props > = ( { DisplayError , children } ) => {
58+ export const ErrorContextProvider : FC < Props > = ( { children } ) => {
5159 const [ error , setErrorState ] = useState < ErrorDetails | null > ( null )
5260
5361 const setError = useCallback (
@@ -59,10 +67,9 @@ export const ErrorContextProvider: FC<Props> = ({ DisplayError, children }) => {
5967 } ,
6068 [ setErrorState ] ,
6169 )
62- const contextValue : ErrorContextType = { error, setError, DisplayError : DisplayError ?? DefaultErrorDisplay }
6370
6471 return (
65- < ErrorContext . Provider value = { contextValue } >
72+ < ErrorContext . Provider value = { { error , setError } } >
6673 < MaybeError > { children } </ MaybeError >
6774 </ ErrorContext . Provider >
6875 )
0 commit comments