frontend add

This commit is contained in:
shinmj
2021-10-21 09:03:17 +09:00
parent 8caa4bbc5a
commit cb9d50511e
443 changed files with 88282 additions and 0 deletions

View File

@@ -0,0 +1,57 @@
import { DEFAULT_ERROR_MESSAGE } from '@constants'
import { AxiosError } from 'axios'
import { atom, DefaultValue, selector } from 'recoil'
/**
* Global error 상태관리
*/
interface IErrors {
defaultMessage: string
field: string
rejectedValue?: string
}
export interface IErrorProps {
open?: boolean
error?: AxiosError
status?: number
message?: string
errors?: IErrors[]
}
export const errorStateAtom = atom<IErrorProps>({
key: 'errorStateAtom',
default: { error: null } as IErrorProps,
})
export const errorStateSelector = selector<IErrorProps>({
key: 'errorStateSelector',
get: ({ get }) => {
return get(errorStateAtom)
},
set: ({ set, reset }, newValue) => {
if (newValue instanceof DefaultValue) {
reset(errorStateAtom)
} else {
const error = newValue.error
let message = error?.message || newValue.message || DEFAULT_ERROR_MESSAGE
let errors: IErrors[] = []
let status = newValue.status || 500
if (error?.response) {
message = error.response.data.message || message
errors = error.response.data.errors
status = error.response.status
}
set(errorStateAtom, {
open: true,
error,
status,
message,
errors,
})
}
},
})