Skip to content

Commit

Permalink
feat: 라우팅 로직 로그인부분 추가 개선
Browse files Browse the repository at this point in the history
  • Loading branch information
lgrin-byte committed Dec 29, 2024
1 parent da303f8 commit bfca027
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
22 changes: 17 additions & 5 deletions app/login/Login.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";

import React from "react";
import React, { useEffect } from "react";
import { SubmitHandler, useForm } from "react-hook-form";
import { useRouter } from "next/navigation";

Expand All @@ -10,6 +10,7 @@ import { usePostLogin } from "@/mutations/postLogin";
import useCheckSignIn from "@/hooks/useCheckSignIn";
import useChannelTalk from "@/hooks/useChannelTalk";
import { setCookie } from "@/utils/cookie";
import { useGetThemeList } from "@/queries/getThemeList";

import LoginView from "./LoginView";

Expand Down Expand Up @@ -40,17 +41,28 @@ function Login() {
useCheckSignIn();
useChannelTalk();

const router = useRouter();
const formValue = watch();

const { data: themeList, isLoading: isThemeLoading } = useGetThemeList();
const router = useRouter();

const onSubmit: SubmitHandler<FormValues> = async (data) => {
try {
await postLogin(data);
router.push("/admin");
} catch (err) {
console.error("Login failed:", err);
} catch (error) {
console.error("Login failed:", error);
}
};

useEffect(() => {
if (themeList && themeList.length > 0) {
const defaultThemeId = themeList[0].id;
router.push(`/admin?themeId=${defaultThemeId}`);
} else {
router.push(`/admin`);
}
}, [isThemeLoading]);

const formProps = {
component: "form",
noValidate: true,
Expand Down
10 changes: 5 additions & 5 deletions app/mutations/postLogin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useMutation } from "@tanstack/react-query";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { AxiosError, AxiosResponse } from "axios";

import { useSnackBarWrite } from "@/components/atoms/snackBar.atom";
Expand Down Expand Up @@ -31,13 +31,14 @@ export const postLogin = async (data: Request) => {
URL_PATH,
data
);

return res.data;
};

export const usePostLogin = (configOptions?: MutationConfigOptions) => {
const setIsLoggedIn = useIsLoggedInWrite();
const setSnackBar = useSnackBarWrite();
const queryClient = useQueryClient();

const info = useMutation<Response, AxiosError<ApiError>, Request, void>({
mutationKey: MUTATION_KEY,
mutationFn: (req) => postLogin(req),
Expand All @@ -54,11 +55,10 @@ export const usePostLogin = (configOptions?: MutationConfigOptions) => {
accessTokenExpiresIn: data.accessTokenExpiresIn,
});
setIsLoggedIn(true);

queryClient.invalidateQueries({ queryKey: ["/v1/theme"] });
}
},
onSettled: () => {
// console.log("항상 실행");
},
onError: (error) => {
setSnackBar({
isOpen: true,
Expand Down
4 changes: 0 additions & 4 deletions app/mutations/putTheme.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { AxiosResponse } from "axios";
import { useRouter } from "next/navigation";

import { useSelectedThemeValue } from "@/components/atoms/selectedTheme.atom";
import { useToastWrite } from "@/components/atoms/toast.atom";
Expand Down Expand Up @@ -30,15 +29,12 @@ export const putTheme = async (req: Request) => {
export const usePutTheme = (configOptions?: MutationConfigOptions) => {
const queryClient = useQueryClient();
const setToast = useToastWrite();
const selectedTheme = useSelectedThemeValue();
const router = useRouter();
const info = useMutation<Response, void, Request, void>({
mutationKey: MUTATION_KEY,
mutationFn: (req) => putTheme(req),
...configOptions?.options,
onSuccess: () => {
queryClient.invalidateQueries(QUERY_KEY);
router.push(`/admin?themeId=${selectedTheme.id}`);

setToast({
isOpen: true,
Expand Down

0 comments on commit bfca027

Please sign in to comment.