Skip to content

Commit

Permalink
Merge pull request #121 from boostcampwm-2022/refactor/toastMsg
Browse files Browse the repository at this point in the history
[Refactor] Toast UI 에 전역 상태값 추가
  • Loading branch information
iyu88 authored Feb 17, 2023
2 parents 5e7e715 + f1d9f61 commit 4d91bc3
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 10 deletions.
1 change: 1 addition & 0 deletions frontend/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<body>
<div id="root"></div>
<div id="modal-root"></div>
<div id="toast-root"></div>
</body>

</html>
4 changes: 2 additions & 2 deletions frontend/src/Router.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { lazy, Suspense } from 'react';
import { BrowserRouter, Navigate, Route, Routes } from 'react-router-dom';
import { Spinner } from './components/spinner';
import { ToastMsg } from './components/toastMsg';
import ToastPortal from './components/toastMsg/ToastPortal';
import MainLayout from './pages/MainLayout';
import NotFoundPage from './pages/NotFoundPage';

Expand All @@ -15,7 +15,7 @@ const DocumentPage = lazy(() => import('./pages/DocumentPage'));
const Router = () => {
return (
<BrowserRouter>
<ToastMsg />
<ToastPortal />
<Suspense fallback={<Spinner />}>
<Routes>
<Route path="/" element={<LandingPage />} />
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/atoms/toastMsgAtom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const toastMsgState = atom({
key: 'toastMsg',
default: {
type: 'INIT',
msg: ''
msg: '',
key: 0,
}
});

Expand Down
12 changes: 6 additions & 6 deletions frontend/src/components/toastMsg/ToastMsg.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ import { devices } from '../../constants/breakpoints';

const ToastMsg = () => {
const theme = useTheme();
const toastMsg = useRecoilValue(toastMsgState);
const {type, msg, key} = useRecoilValue(toastMsgState);

const ICON_SIZE = 24;
const TOAST_COLOR = toastMsg.type === 'INFO'
const TOAST_COLOR = type === 'INFO'
? theme.primary
: theme.caution;

if (toastMsg.type !== 'INIT') {
if (type !== 'INIT') {
return (
<ToastMsgWrapper key={+new Date()} color={TOAST_COLOR}>
{toastMsg.type === 'INFO'
<ToastMsgWrapper key={key} color={TOAST_COLOR}>
{type === 'INFO'
? <CheckIcon
width={ICON_SIZE}
height={ICON_SIZE}
Expand All @@ -28,7 +28,7 @@ const ToastMsg = () => {
height={ICON_SIZE}
fill={TOAST_COLOR} />
}
<ToastText color={TOAST_COLOR}>{toastMsg.msg}</ToastText>
<ToastText color={TOAST_COLOR}>{msg}</ToastText>
</ToastMsgWrapper>
);
} else {
Expand Down
10 changes: 10 additions & 0 deletions frontend/src/components/toastMsg/ToastPortal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react';
import ReactDOM from 'react-dom';
import { ToastMsg } from './ToastMsg';

const ToastPortal = () => {
const toastRoot = document.getElementById('toast-root');
return ReactDOM.createPortal(<ToastMsg />, toastRoot as HTMLElement );
};

export default ToastPortal;
3 changes: 2 additions & 1 deletion frontend/src/hooks/useToast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const useToast = () => {
const alertToast = (type: string, msg: string) => {
setToastMsg({
type,
msg
msg,
key: +new Date(),
});
};

Expand Down

0 comments on commit 4d91bc3

Please sign in to comment.