-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(fe): use tanstack query for settings page #2308
Open
Kimhyojung0810
wants to merge
9
commits into
main
Choose a base branch
from
t1091-refactor-settings-v4
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
51093de
refactor(fe): refactor settings page
Kimhyojung0810 a45abcd
fix(fe): fix rendering error
Kimhyojung0810 86c8792
refactor(fe): delete unused apis and queires
Kimhyojung0810 26bc32a
refactor(fe): add apis and queries
Kimhyojung0810 a657752
refactor(fe): use tanstack query for settings page
Kimhyojung0810 455cb23
refactor(fe): fix build error
Kimhyojung0810 9ac9c5a
refactor(fe): fix build error and check major api error
Kimhyojung0810 55cb666
Merge branch 'main' into t1091-refactor-settings-v4
Kimhyojung0810 721d7ee
Merge branch 'main' into t1091-refactor-settings-v4
Kimhyojung0810 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -1,13 +1,15 @@ | ||||
'use client' | ||||
|
||||
import { safeFetcherWithAuth } from '@/libs/utils' | ||||
import type { SettingsFormat } from '@/types/type' | ||||
import { valibotResolver } from '@hookform/resolvers/valibot' | ||||
import { useMutation, useQuery } from '@tanstack/react-query' | ||||
import { useRouter, useSearchParams } from 'next/navigation' | ||||
import { useEffect, useRef } from 'react' | ||||
import { useState } from 'react' | ||||
import { useForm } from 'react-hook-form' | ||||
import { toast } from 'sonner' | ||||
import { updateUserProfile } from '../../_libs/apis/profile' | ||||
import { profileQueries } from '../../_libs/queries/profile' | ||||
import { ConfirmModal } from './_components/ConfirmModal' | ||||
import { CurrentPwSection } from './_components/CurrentPwSection' | ||||
import { IdSection } from './_components/IdSection' | ||||
|
@@ -20,7 +22,6 @@ import { SaveButton } from './_components/SaveButton' | |||
import { StudentIdSection } from './_components/StudentIdSection' | ||||
import { TopicSection } from './_components/TopicSection' | ||||
import { SettingsProvider } from './_components/context' | ||||
import type { Profile } from './_components/context' | ||||
import { useCheckPassword } from './_libs/hooks/useCheckPassword' | ||||
import { getSchema } from './_libs/schemas' | ||||
import { useConfirmNavigation } from './_libs/utils' | ||||
|
@@ -38,31 +39,28 @@ export default function Page() { | |||
const updateNow = searchParams.get('updateNow') | ||||
const router = useRouter() | ||||
const bypassConfirmation = useRef<boolean>(false) | ||||
const [defaultProfileValues, setdefaultProfileValues] = useState<Profile>({ | ||||
username: '', | ||||
userProfile: { | ||||
realName: '' | ||||
|
||||
const { data: defaultProfileValues, isLoading } = useQuery({ | ||||
...profileQueries.fetch(), | ||||
initialData: { | ||||
username: '', | ||||
userProfile: { | ||||
realName: '' | ||||
}, | ||||
studentId: '', | ||||
major: '' | ||||
}, | ||||
studentId: '', | ||||
major: '' | ||||
retry: false | ||||
}) | ||||
|
||||
// Fetch default profile values | ||||
const [majorValue, setMajorValue] = useState(defaultProfileValues.major) | ||||
|
||||
useEffect(() => { | ||||
const fetchDefaultProfile = async () => { | ||||
try { | ||||
const data: Profile = await safeFetcherWithAuth.get('user').json() | ||||
setMajorValue(data.major) | ||||
setdefaultProfileValues(data) | ||||
setIsLoading(false) | ||||
} catch (error) { | ||||
console.error('Failed to fetch profile:', error) | ||||
toast.error('Failed to load profile data') | ||||
setIsLoading(false) | ||||
} | ||||
if (!isLoading && defaultProfileValues.major) { | ||||
console.log('Setting majorValue:', defaultProfileValues.major) | ||||
setMajorValue(defaultProfileValues.major) | ||||
} | ||||
fetchDefaultProfile() | ||||
}, []) | ||||
}, [defaultProfileValues.major, isLoading]) | ||||
|
||||
const { | ||||
register, | ||||
|
@@ -91,6 +89,7 @@ export default function Page() { | |||
|
||||
const { isConfirmModalOpen, setIsConfirmModalOpen, confirmAction } = | ||||
useConfirmNavigation(bypassConfirmation, Boolean(updateNow)) | ||||
|
||||
const { | ||||
isPasswordCorrect, | ||||
newPasswordAble, | ||||
|
@@ -102,8 +101,6 @@ export default function Page() { | |||
const [newPasswordShow, setNewPasswordShow] = useState<boolean>(false) | ||||
const [confirmPasswordShow, setConfirmPasswordShow] = useState<boolean>(false) | ||||
const [majorOpen, setMajorOpen] = useState<boolean>(false) | ||||
const [majorValue, setMajorValue] = useState<string>('') | ||||
const [isLoading, setIsLoading] = useState<boolean>(true) | ||||
|
||||
const isPasswordsMatch = newPassword === confirmPassword && newPassword !== '' | ||||
const saveAblePassword: boolean = | ||||
|
@@ -121,53 +118,56 @@ export default function Page() { | |||
(!newPassword && !confirmPassword)) | ||||
const saveAbleUpdateNow = | ||||
Boolean(studentId) && majorValue !== 'none' && !errors.studentId | ||||
// 일치 여부에 따라 New Password Input, Re-enter Password Input 창의 border 색상을 바꿈 | ||||
|
||||
useEffect(() => { | ||||
if (isPasswordsMatch) { | ||||
setValue('newPassword', newPassword) | ||||
setValue('confirmPassword', confirmPassword) | ||||
} | ||||
}, [isPasswordsMatch, newPassword, confirmPassword]) | ||||
const onSubmit = async (data: SettingsFormat) => { | ||||
try { | ||||
// 필요 없는 필드 제외 (defaultProfileValues와 값이 같은 것들은 제외) | ||||
const updatePayload: UpdatePayload = {} | ||||
if (data.realName !== defaultProfileValues.userProfile?.realName) { | ||||
updatePayload.realName = data.realName | ||||
} | ||||
if (majorValue !== defaultProfileValues.major) { | ||||
updatePayload.major = majorValue | ||||
} | ||||
if (data.currentPassword !== 'tmppassword1') { | ||||
updatePayload.password = data.currentPassword | ||||
} | ||||
if (data.newPassword !== 'tmppassword1') { | ||||
updatePayload.newPassword = data.newPassword | ||||
} | ||||
if (updateNow && data.studentId !== '0000000000') { | ||||
updatePayload.studentId = data.studentId | ||||
} | ||||
const response = await safeFetcherWithAuth.patch('user', { | ||||
json: updatePayload | ||||
}) | ||||
if (response.ok) { | ||||
toast.success('Successfully updated your information') | ||||
bypassConfirmation.current = true | ||||
setTimeout(() => { | ||||
if (updateNow) { | ||||
router.push('/') | ||||
} else { | ||||
window.location.reload() | ||||
} | ||||
}, 1500) | ||||
} | ||||
} catch (error) { | ||||
}, [isPasswordsMatch, newPassword, confirmPassword, setValue]) | ||||
|
||||
const { mutate } = useMutation({ | ||||
mutationFn: updateUserProfile, | ||||
onError: (error) => { | ||||
console.error(error) | ||||
toast.error('Failed to update your information, Please try again') | ||||
setTimeout(() => { | ||||
window.location.reload() | ||||
}, 1500) | ||||
}, | ||||
onSuccess: () => { | ||||
toast.success('Successfully updated your information') | ||||
bypassConfirmation.current = true | ||||
setTimeout(() => { | ||||
if (updateNow) { | ||||
router.push('/') | ||||
} else { | ||||
window.location.reload() | ||||
} | ||||
}, 1500) | ||||
} | ||||
}) | ||||
|
||||
const onSubmit = (data: SettingsFormat) => { | ||||
const updatePayload: UpdatePayload = {} | ||||
|
||||
if (data.realName !== defaultProfileValues.userProfile?.realName) { | ||||
updatePayload.realName = data.realName | ||||
} | ||||
if (majorValue !== defaultProfileValues.major) { | ||||
updatePayload.major = majorValue | ||||
} | ||||
if (data.currentPassword !== 'tmppassword1') { | ||||
updatePayload.password = data.currentPassword | ||||
} | ||||
if (data.newPassword !== 'tmppassword1') { | ||||
updatePayload.newPassword = data.newPassword | ||||
} | ||||
if (updateNow && data.studentId !== '0000000000') { | ||||
updatePayload.studentId = data.studentId | ||||
} | ||||
|
||||
mutate(updatePayload) | ||||
} | ||||
|
||||
const resetToSubmittableValue = ( | ||||
|
@@ -193,6 +193,7 @@ export default function Page() { | |||
|
||||
const settingsContextValue = { | ||||
defaultProfileValues, | ||||
isLoading, | ||||
passwordState: { | ||||
passwordShow, | ||||
setPasswordShow, | ||||
|
@@ -211,10 +212,9 @@ export default function Page() { | |||
register, | ||||
errors | ||||
}, | ||||
updateNow: Boolean(updateNow), | ||||
isLoading | ||||
updateNow: Boolean(updateNow) | ||||
//isLoading | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
} | ||||
|
||||
return ( | ||||
<div className="flex w-full gap-20 py-6"> | ||||
{/* Logo */} | ||||
|
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,22 @@ | ||
import { safeFetcherWithAuth } from '@/libs/utils' | ||
|
||
export interface Profile { | ||
username: string // ID | ||
userProfile: { | ||
realName: string | ||
} | ||
studentId: string | ||
major: string | ||
} | ||
|
||
export const fetchUserProfile = async (): Promise<Profile> => { | ||
return await safeFetcherWithAuth.get('user').json() | ||
} | ||
|
||
export const updateUserProfile = async ( | ||
updatePayload: Partial<Profile & { password?: string; newPassword?: string }> | ||
): Promise<Response> => { | ||
return await safeFetcherWithAuth.patch('user', { | ||
json: updatePayload | ||
}) | ||
} |
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,9 @@ | ||
import { fetchUserProfile } from '../apis/profile' | ||
|
||
export const profileQueries = { | ||
all: () => ['userProfile'] as const, | ||
fetch: () => ({ | ||
queryKey: profileQueries.all(), | ||
queryFn: fetchUserProfile | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
console.log 지워주세요!
isLoading
값을 사용할 필요는 없는 것 같아요!