From 005879065b8789f7a0372b781330583138fadea1 Mon Sep 17 00:00:00 2001 From: jiho Date: Mon, 30 Dec 2024 10:37:24 +0900 Subject: [PATCH] refactor(fe): refactor settings page using context and custom hooks --- .../settings/_components/CurrentPwSection.tsx | 21 +- .../(main)/settings/_components/IdSection.tsx | 13 +- .../settings/_components/MajorSection.tsx | 28 +- .../settings/_components/NameSection.tsx | 24 +- .../settings/_components/NewPwSection.tsx | 21 +- .../_components/ReEnterNewPwSection.tsx | 17 +- .../settings/_components/SaveButton.tsx | 5 +- .../settings/_components/StudentIdSection.tsx | 25 +- .../settings/_components/TopicSection.tsx | 6 +- .../(main)/settings/_components/context.ts | 56 ++++ .../_components/hooks/useCheckPassword.ts | 49 ++++ .../(client)/(main)/settings/_libs/schemas.ts | 24 ++ .../app/(client)/(main)/settings/page.tsx | 270 +++++++----------- 13 files changed, 298 insertions(+), 261 deletions(-) create mode 100644 apps/frontend/app/(client)/(main)/settings/_components/context.ts create mode 100644 apps/frontend/app/(client)/(main)/settings/_components/hooks/useCheckPassword.ts create mode 100644 apps/frontend/app/(client)/(main)/settings/_libs/schemas.ts diff --git a/apps/frontend/app/(client)/(main)/settings/_components/CurrentPwSection.tsx b/apps/frontend/app/(client)/(main)/settings/_components/CurrentPwSection.tsx index 346824b56f..1eae0835b0 100644 --- a/apps/frontend/app/(client)/(main)/settings/_components/CurrentPwSection.tsx +++ b/apps/frontend/app/(client)/(main)/settings/_components/CurrentPwSection.tsx @@ -3,35 +3,30 @@ import { Input } from '@/components/shadcn/input' import { cn } from '@/libs/utils' import invisibleIcon from '@/public/icons/invisible.svg' import visibleIcon from '@/public/icons/visible.svg' -import type { SettingsFormat } from '@/types/type' import Image from 'next/image' import React from 'react' -import type { FieldErrors, UseFormRegister } from 'react-hook-form' import { FaCheck } from 'react-icons/fa6' +import { useSettingsContext } from './context' interface CurrentPwSectionProps { currentPassword: string isCheckButtonClicked: boolean isPasswordCorrect: boolean - setPasswordShow: React.Dispatch> - passwordShow: boolean checkPassword: () => Promise - register: UseFormRegister - errors: FieldErrors - updateNow: boolean } export default function CurrentPwSection({ currentPassword, isCheckButtonClicked, isPasswordCorrect, - setPasswordShow, - passwordShow, - checkPassword, - register, - errors, - updateNow + checkPassword }: CurrentPwSectionProps) { + const { + passwordState: { passwordShow, setPasswordShow }, + updateNow, + formState: { register, errors } + } = useSettingsContext() + return ( <> diff --git a/apps/frontend/app/(client)/(main)/settings/_components/IdSection.tsx b/apps/frontend/app/(client)/(main)/settings/_components/IdSection.tsx index 4f22d13b8b..57996dfe7f 100644 --- a/apps/frontend/app/(client)/(main)/settings/_components/IdSection.tsx +++ b/apps/frontend/app/(client)/(main)/settings/_components/IdSection.tsx @@ -1,17 +1,14 @@ import { Input } from '@/components/shadcn/input' +import { useSettingsContext } from './context' + +export default function IdSection() { + const { isLoading, defaultProfileValues } = useSettingsContext() -export default function IdSection({ - isLoading, - defaultUsername -}: { - isLoading: boolean - defaultUsername: string -}) { return ( <> diff --git a/apps/frontend/app/(client)/(main)/settings/_components/MajorSection.tsx b/apps/frontend/app/(client)/(main)/settings/_components/MajorSection.tsx index d8f73776f8..a18dafcfe9 100644 --- a/apps/frontend/app/(client)/(main)/settings/_components/MajorSection.tsx +++ b/apps/frontend/app/(client)/(main)/settings/_components/MajorSection.tsx @@ -17,28 +17,16 @@ import { majors } from '@/libs/constants' import { cn } from '@/libs/utils' import React from 'react' import { FaChevronDown, FaCheck } from 'react-icons/fa6' +import { useSettingsContext } from './context' -interface MajorSectionProps { - majorOpen: boolean - setMajorOpen: React.Dispatch> - majorValue: string - setMajorValue: React.Dispatch> - updateNow: boolean - isLoading: boolean - defaultProfileValues: { - major?: string - } -} +export default function MajorSection() { + const { + isLoading, + updateNow, + majorState: { majorOpen, setMajorOpen, majorValue, setMajorValue }, + defaultProfileValues + } = useSettingsContext() -export default function MajorSection({ - majorOpen, - setMajorOpen, - majorValue, - setMajorValue, - updateNow, - isLoading, - defaultProfileValues -}: MajorSectionProps) { return ( <> diff --git a/apps/frontend/app/(client)/(main)/settings/_components/NameSection.tsx b/apps/frontend/app/(client)/(main)/settings/_components/NameSection.tsx index 0946919f16..d8a4714144 100644 --- a/apps/frontend/app/(client)/(main)/settings/_components/NameSection.tsx +++ b/apps/frontend/app/(client)/(main)/settings/_components/NameSection.tsx @@ -1,25 +1,19 @@ import { Input } from '@/components/shadcn/input' import { cn } from '@/libs/utils' -import type { SettingsFormat } from '@/types/type' -import type { FieldErrors, UseFormRegister } from 'react-hook-form' +import { useSettingsContext } from './context' interface NameSectionProps { - isLoading: boolean - updateNow: boolean - defaultProfileValues: { userProfile?: { realName?: string } } - register: UseFormRegister - errors: FieldErrors realName: string } -export default function NameSection({ - isLoading, - updateNow, - defaultProfileValues, - register, - errors, - realName -}: NameSectionProps) { +export default function NameSection({ realName }: NameSectionProps) { + const { + isLoading, + updateNow, + defaultProfileValues, + formState: { register, errors } + } = useSettingsContext() + return ( <> diff --git a/apps/frontend/app/(client)/(main)/settings/_components/NewPwSection.tsx b/apps/frontend/app/(client)/(main)/settings/_components/NewPwSection.tsx index 8e7730c440..09d5757f3b 100644 --- a/apps/frontend/app/(client)/(main)/settings/_components/NewPwSection.tsx +++ b/apps/frontend/app/(client)/(main)/settings/_components/NewPwSection.tsx @@ -2,34 +2,29 @@ import { Input } from '@/components/shadcn/input' import { cn } from '@/libs/utils' import invisibleIcon from '@/public/icons/invisible.svg' import visibleIcon from '@/public/icons/visible.svg' -import type { SettingsFormat } from '@/types/type' import Image from 'next/image' import React from 'react' -import type { FieldErrors, UseFormRegister } from 'react-hook-form' +import { useSettingsContext } from './context' interface NewPwSectionProps { - newPasswordShow: boolean - setNewPasswordShow: React.Dispatch> newPasswordAble: boolean isPasswordsMatch: boolean newPassword: string confirmPassword: string - updateNow: boolean - register: UseFormRegister - errors: FieldErrors } export default function NewPwSection({ - newPasswordShow, - setNewPasswordShow, newPasswordAble, isPasswordsMatch, newPassword, - confirmPassword, - updateNow, - register, - errors + confirmPassword }: NewPwSectionProps) { + const { + passwordState: { newPasswordShow, setNewPasswordShow }, + updateNow, + formState: { register, errors } + } = useSettingsContext() + return ( <>
diff --git a/apps/frontend/app/(client)/(main)/settings/_components/ReEnterNewPwSection.tsx b/apps/frontend/app/(client)/(main)/settings/_components/ReEnterNewPwSection.tsx index 0aa62d2649..75f40a0ced 100644 --- a/apps/frontend/app/(client)/(main)/settings/_components/ReEnterNewPwSection.tsx +++ b/apps/frontend/app/(client)/(main)/settings/_components/ReEnterNewPwSection.tsx @@ -4,29 +4,28 @@ import visibleIcon from '@/public/icons/visible.svg' import type { SettingsFormat } from '@/types/type' import Image from 'next/image' import React from 'react' -import type { UseFormRegister, UseFormGetValues } from 'react-hook-form' +import type { UseFormGetValues } from 'react-hook-form' +import { useSettingsContext } from './context' interface ReEnterNewPwSectionProps { - confirmPasswordShow: boolean - setConfirmPasswordShow: React.Dispatch> newPasswordAble: boolean - updateNow: boolean - register: UseFormRegister getValues: UseFormGetValues confirmPassword: string isPasswordsMatch: boolean } export default function ReEnterNewPwSection({ - confirmPasswordShow, - setConfirmPasswordShow, newPasswordAble, - updateNow, - register, getValues, confirmPassword, isPasswordsMatch }: ReEnterNewPwSectionProps) { + const { + updateNow, + passwordState: { confirmPasswordShow, setConfirmPasswordShow }, + formState: { register } + } = useSettingsContext() + return ( <> {/* Re-enter new password */} diff --git a/apps/frontend/app/(client)/(main)/settings/_components/SaveButton.tsx b/apps/frontend/app/(client)/(main)/settings/_components/SaveButton.tsx index a7a458a290..b31c7740f8 100644 --- a/apps/frontend/app/(client)/(main)/settings/_components/SaveButton.tsx +++ b/apps/frontend/app/(client)/(main)/settings/_components/SaveButton.tsx @@ -1,18 +1,19 @@ import { Button } from '@/components/shadcn/button' +import { useSettingsContext } from './context' interface SaveButtonProps { - updateNow: boolean saveAbleUpdateNow: boolean saveAble: boolean onSubmitClick: () => void } export default function SaveButton({ - updateNow, saveAbleUpdateNow, saveAble, onSubmitClick }: SaveButtonProps) { + const { updateNow } = useSettingsContext() + return (