-
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.
- Loading branch information
Showing
13 changed files
with
214 additions
and
5 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,55 @@ | ||
import styled, { css } from 'styled-components'; | ||
|
||
interface IProps { | ||
mode: 'block' | 'center' | 'fixed' | 'inline' | 'absolute'; | ||
} | ||
|
||
export default function Loading({ mode = 'block' }: IProps) { | ||
return ( | ||
<S.Loading mode={mode}> | ||
<img src="/public/images/Spinner200px.gif" /> | ||
</S.Loading> | ||
); | ||
} | ||
|
||
const S = { | ||
Loading: styled.div<{ mode: string }>` | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
z-index: 20; | ||
width: 100%; | ||
height: 100%; | ||
img { | ||
width: 80px; | ||
height: 80px; | ||
} | ||
${(props) => | ||
props.mode === 'inline' && | ||
css` | ||
width: 100%; | ||
`}; | ||
${(props) => | ||
props.mode === 'center' && | ||
css` | ||
height: 50%; | ||
`}; | ||
${(props) => | ||
props.mode === 'absolute' && | ||
css` | ||
position: absolute; | ||
top: 0; | ||
height: 100%; | ||
`}; | ||
${(props) => | ||
props.mode === 'fixed' && | ||
css` | ||
position: fixed; | ||
top: 0; | ||
left: 0; | ||
right: 0; | ||
`}; | ||
`, | ||
}; |
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
const recoilKeys = { | ||
MODAL: 'MODAL', | ||
MODAL_WITH_TEXT: 'MODAL_WITH_TEXT', | ||
LOADING: 'LOADING', | ||
}; | ||
|
||
export default recoilKeys; |
Empty file.
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,75 @@ | ||
import { modalState } from '@/stores/modal'; | ||
import { DefaultResponse } from '@/types'; | ||
import axios, { AxiosError } from 'axios'; | ||
import { useResetRecoilState, useSetRecoilState } from 'recoil'; | ||
|
||
export function useErrorHandler() { | ||
const setModalState = useSetRecoilState(modalState); | ||
const resetModalState = useResetRecoilState(modalState); | ||
const handleError = (error: any) => { | ||
if (axios.isAxiosError(error)) { | ||
return setModalState((prev) => ({ | ||
...prev, | ||
type: 'ALERT', | ||
titleText: '오류 발생', | ||
descText: error?.message ? error.message : '잠시 후 다시 시도해주세요', | ||
confirmButtonText: '확인', | ||
onClickConfirm: () => { | ||
resetModalState(); | ||
}, | ||
})); | ||
} | ||
const axiosError = error as AxiosError<DefaultResponse>; | ||
const isConnectionRefusedError = error.code?.includes('ERR_NETWORK'); | ||
const isUnprocessableEntity = axiosError.response?.status === 422; | ||
const isInternalServerError = axiosError.response?.status === 500; | ||
|
||
if (isConnectionRefusedError) { | ||
return setModalState((prev) => ({ | ||
...prev, | ||
type: 'ALERT', | ||
titleText: '네트워크 통신이 원활하지 않습니다.', | ||
descText: '네트워크 상태를 확인해주세요.', | ||
confirmButtonText: '확인', | ||
onClickConfirm: () => { | ||
resetModalState(); | ||
}, | ||
})); | ||
} | ||
if (isUnprocessableEntity) { | ||
return setModalState((prev) => ({ | ||
...prev, | ||
type: 'ALERT', | ||
titleText: '유효하지 않습니다.', | ||
descText: axiosError.response?.data.message, | ||
confirmButtonText: '확인', | ||
onClickConfirm: () => { | ||
resetModalState(); | ||
}, | ||
})); | ||
} | ||
if (isInternalServerError) { | ||
return setModalState((prev) => ({ | ||
...prev, | ||
type: 'ALERT', | ||
titleText: '오류 발생', | ||
descText: axiosError.message ? axiosError.message : '서버에서 오류가 발생했습니다. 잠시 후 다시 시도해주세요.', | ||
confirmButtonText: '확인', | ||
onClickConfirm: () => { | ||
resetModalState(); | ||
}, | ||
})); | ||
} | ||
return setModalState((prev) => ({ | ||
...prev, | ||
type: 'ALERT', | ||
titleText: '오류 발생', | ||
descText: '잠시 후 다시 시도해주세요.', | ||
confirmButtonText: '확인', | ||
onClickConfirm: () => { | ||
resetModalState(); | ||
}, | ||
})); | ||
}; | ||
return { handleError }; | ||
} |
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 { loadingState } from '@/stores/loading'; | ||
import React from 'react'; | ||
import { useRecoilState, useResetRecoilState } from 'recoil'; | ||
|
||
export default function useLoading() { | ||
const [loadingValue, setLoadingValue] = useRecoilState(loadingState); | ||
const isLoading = loadingValue.isLoading; | ||
const setIsLoading = (value: boolean) => setLoadingValue((prev) => ({ ...prev, isLoading: value })); | ||
const resetLoading = useResetRecoilState(loadingState); | ||
React.useEffect(() => { | ||
resetLoading(); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
return { isLoading, setIsLoading }; | ||
} |
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,13 @@ | ||
import { atom } from 'recoil'; | ||
import recoilKeys from '@/constants/recoilKeys'; | ||
|
||
interface ILoadingState { | ||
isLoading: boolean; | ||
} | ||
|
||
export const loadingState = atom<ILoadingState>({ | ||
key: recoilKeys.LOADING, | ||
default: { | ||
isLoading: false, | ||
}, | ||
}); |
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,17 @@ | ||
const ACCESS_TOKEN = 'jwt'; | ||
|
||
export const storage = { | ||
setAccessTokenLocalStorage: (value: string) => { | ||
localStorage.setItem(ACCESS_TOKEN, value); | ||
}, | ||
getAccessTokenLocalStorageItem: (): string | null => { | ||
const accessToken = localStorage.getItem(ACCESS_TOKEN); | ||
if (!accessToken) { | ||
return null; | ||
} | ||
return JSON.parse(accessToken); | ||
}, | ||
removeAccessToken: () => { | ||
return localStorage.removeItem(ACCESS_TOKEN); | ||
}, | ||
}; |