-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(fe): refactor settings page using context and hooks (#2274)
* refactor(fe): refactor settings page using context and hooks * refactor(fe): refactor settings page * fix(fe): change type name to Profile * fix(fe): no need to check response status * fix(fe): add toast error when failed to check password * fix(fe): remove unnecessary type use
- Loading branch information
1 parent
550601e
commit a0b215b
Showing
14 changed files
with
299 additions
and
265 deletions.
There are no files selected for viewing
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
13 changes: 5 additions & 8 deletions
13
apps/frontend/app/(client)/(main)/settings/_components/IdSection.tsx
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
24 changes: 9 additions & 15 deletions
24
apps/frontend/app/(client)/(main)/settings/_components/NameSection.tsx
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
5 changes: 3 additions & 2 deletions
5
apps/frontend/app/(client)/(main)/settings/_components/SaveButton.tsx
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
25 changes: 8 additions & 17 deletions
25
apps/frontend/app/(client)/(main)/settings/_components/StudentIdSection.tsx
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
6 changes: 5 additions & 1 deletion
6
apps/frontend/app/(client)/(main)/settings/_components/TopicSection.tsx
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
56 changes: 56 additions & 0 deletions
56
apps/frontend/app/(client)/(main)/settings/_components/context.ts
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,56 @@ | ||
import type { SettingsFormat } from '@/types/type' | ||
import { createContext, useContext } from 'react' | ||
import type { FieldErrors, UseFormRegister } from 'react-hook-form' | ||
|
||
export interface Profile { | ||
username: string // ID | ||
userProfile: { | ||
realName: string | ||
} | ||
studentId: string | ||
major: string | ||
} | ||
|
||
interface PasswordState { | ||
passwordShow: boolean | ||
setPasswordShow: React.Dispatch<React.SetStateAction<boolean>> | ||
newPasswordShow: boolean | ||
setNewPasswordShow: React.Dispatch<React.SetStateAction<boolean>> | ||
confirmPasswordShow: boolean | ||
setConfirmPasswordShow: React.Dispatch<React.SetStateAction<boolean>> | ||
} | ||
|
||
interface MajorState { | ||
majorOpen: boolean | ||
setMajorOpen: React.Dispatch<React.SetStateAction<boolean>> | ||
majorValue: string | ||
setMajorValue: React.Dispatch<React.SetStateAction<string>> | ||
} | ||
|
||
interface FormState { | ||
register: UseFormRegister<SettingsFormat> | ||
errors: FieldErrors<SettingsFormat> | ||
} | ||
|
||
export type SettingsContextType = { | ||
defaultProfileValues: Profile | ||
passwordState: PasswordState | ||
majorState: MajorState | ||
formState: FormState | ||
updateNow: boolean | ||
isLoading: boolean | ||
} | ||
|
||
const SettingsContext = createContext<SettingsContextType | undefined>( | ||
undefined | ||
) | ||
export const SettingsProvider = SettingsContext.Provider | ||
|
||
// useSettingsContext custom hook | ||
export const useSettingsContext = () => { | ||
const context = useContext(SettingsContext) | ||
if (!context) { | ||
throw new Error('useSettingsContext must be used within a SettingsProvider') | ||
} | ||
return context | ||
} |
49 changes: 49 additions & 0 deletions
49
apps/frontend/app/(client)/(main)/settings/_libs/hooks/useCheckPassword.ts
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,49 @@ | ||
import { safeFetcher } from '@/libs/utils' | ||
import { useState } from 'react' | ||
import { toast } from 'sonner' | ||
|
||
interface UseCheckPasswordResult { | ||
isPasswordCorrect: boolean | ||
newPasswordAble: boolean | ||
isCheckButtonClicked: boolean | ||
checkPassword: () => Promise<void> | ||
} | ||
|
||
/** | ||
* Hook that returns whether a new password can be set after verifying the current password | ||
* @param defaultProfileValues Default profile values | ||
* @param currentPassword The password currently entered by the user | ||
*/ | ||
export const useCheckPassword = ( | ||
defaultProfileValues: { username: string }, | ||
currentPassword: string | ||
): UseCheckPasswordResult => { | ||
const [isPasswordCorrect, setIsPasswordCorrect] = useState(false) | ||
const [newPasswordAble, setNewPasswordAble] = useState(false) | ||
const [isCheckButtonClicked, setIsCheckButtonClicked] = useState(false) | ||
|
||
const checkPassword = async () => { | ||
setIsCheckButtonClicked(true) | ||
try { | ||
const response = await safeFetcher.post('auth/login', { | ||
json: { | ||
username: defaultProfileValues.username, | ||
password: currentPassword | ||
} | ||
}) | ||
|
||
setIsPasswordCorrect(true) | ||
setNewPasswordAble(true) | ||
} catch { | ||
toast.error('Failed to check password') | ||
console.error('Failed to check password') | ||
} | ||
} | ||
|
||
return { | ||
isPasswordCorrect, | ||
newPasswordAble, | ||
isCheckButtonClicked, | ||
checkPassword | ||
} | ||
} |
Oops, something went wrong.