Skip to content

Commit

Permalink
fix: atomWithStorage 사용하여 새로고침시 atom값 날라가는 오류 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
iOdiO89 committed Jul 2, 2024
1 parent 03e8514 commit 7d75fd4
Show file tree
Hide file tree
Showing 8 changed files with 491 additions and 35 deletions.
12 changes: 0 additions & 12 deletions apis/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,6 @@ const getTokenFromLocalStorage = () => {
return accessToken;
};

export const setIsAdminAtLocalStorage = (is_admin: string) => {
localStorage.setItem("is_admin", is_admin);
};

const getIsAdminFromLocalStorage = () => {
const isAdmin = localStorage.getItem("is_admin");
if (!isAdmin) {
return null;
}
return isAdmin;
};

const client = axios.create({
baseURL: process.env.NEXT_PUBLIC_API_URL,
withCredentials: true,
Expand Down
2 changes: 2 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const nextConfig = {
env: {
NEXT_PUBLIC_API_URL: process.env.NEXT_PUBLIC_API_URL,
},

transpilePackages: ["jotai-devtools"],
};

module.exports = nextConfig;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"embla-carousel-react": "^8.1.3",
"gh-pages": "^6.1.1",
"jotai": "^2.8.3",
"jotai-devtools": "^0.10.0",
"lottie-web": "^5.12.2",
"moment": "^2.30.1",
"next": "12.3.4",
Expand Down
18 changes: 12 additions & 6 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,22 @@ import RootLayout from "@/components/Layout";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
import { AppProps } from "next/app";
import { DevTools } from "jotai-devtools";
import "jotai-devtools/styles.css";
import { Provider } from "jotai";

const queryClient = new QueryClient();

export default function App({ Component, pageProps }: AppProps) {
return (
<QueryClientProvider client={queryClient}>
<RootLayout>
<Component {...pageProps} />
</RootLayout>
<ReactQueryDevtools initialIsOpen={false} />
</QueryClientProvider>
<Provider>
<QueryClientProvider client={queryClient}>
<RootLayout>
<Component {...pageProps} />
</RootLayout>
<ReactQueryDevtools initialIsOpen={false} />
<DevTools />
</QueryClientProvider>
</Provider>
);
}
2 changes: 0 additions & 2 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import FlexBox from "@/components/Flexbox";
import { ToastContainer, Zoom, toast } from "react-toastify";
import "react-toastify/dist/ReactToastify.css";
import CheckIcon from "@/public/svgs/Check.svg";
import { atom, useAtomValue } from "jotai";
import { isAdminAtom } from "@/utils/atom";

const Home: NextPage = () => {
const notify = (msg: string) => {
Expand Down
9 changes: 2 additions & 7 deletions pages/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import { useRouter } from "next/router";
import Button from "@/components/Button";
import LogoLetterIcon from "@/public/svgs/LogoLetter.svg";
import { useMutation } from "@tanstack/react-query";
import {
ResponseBody,
setIsAdminAtLocalStorage,
setTokenFromLocalStorage,
} from "@/apis/client";
import { ResponseBody, setTokenFromLocalStorage } from "@/apis/client";
import { SignIn } from "@/apis/auth";
import { atom, useAtom } from "jotai";
import { centerNameAtom, isAdminAtom } from "@/utils/atom";
Expand All @@ -21,7 +17,7 @@ interface userProps {

const Login: NextPage = () => {
const router = useRouter();
const [isAdmin, setIsAdmin] = useAtom(isAdminAtom);
const [, setIsAdmin] = useAtom(isAdminAtom);
const [, setCenterName] = useAtom(centerNameAtom);

const [userInfo, setUserInfo] = useState<userProps>({
Expand Down Expand Up @@ -61,7 +57,6 @@ const Login: NextPage = () => {
const isAdmin = data.result.isAdmin;
if (isAdmin) setCenterName(data.result.centerName);
setIsAdmin(isAdmin);
setIsAdminAtLocalStorage(isAdmin);
setTokenFromLocalStorage(accessToken);

router.push("/");
Expand Down
8 changes: 6 additions & 2 deletions utils/atom.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { atom } from "jotai";
import { atomWithStorage } from "jotai/utils";

export const isAdminAtom = atom<boolean>(true);
export const isAdminAtom = atomWithStorage("isAdmin", false);
isAdminAtom.debugLabel = "isAdminAtom";

export const challengeIdxAtom = atom<number>(-1);
challengeIdxAtom.debugLabel = "challengeIdxAtom";

export const centerNameAtom = atom<string>("");
export const centerNameAtom = atomWithStorage("centerName", "");
centerNameAtom.debugLabel = "centerNameAtom";
Loading

0 comments on commit 7d75fd4

Please sign in to comment.