Skip to content
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): refactor settings page using context and custom hooks #2268

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
15a5e25
feat(fe): confirm modal when leaving settings page
jihorobert Dec 19, 2024
1af0f76
feat(fe): add dark and bright mode
jihorobert Dec 19, 2024
d920f98
chore(fe): add comments for basemodal
jihorobert Dec 19, 2024
06c9976
fix(fe): decide darkmode at ConfirmModal
jihorobert Dec 19, 2024
694cd9e
chore(fe): add explanation for BaseModal component
jihorobert Dec 19, 2024
4bf3467
chore(fe): add explanation for Basemodal component_2
jihorobert Dec 19, 2024
0792ba9
chore(fe): change comment for BaseModal
jihorobert Dec 19, 2024
216eb81
fix(fe): change description props type to string from ReactNode
jihorobert Dec 19, 2024
c773605
chore(fe): import types
jihorobert Dec 20, 2024
0943160
chore(fe): change comment for basemodal
jihorobert Dec 23, 2024
65d9621
chore(fe): change comment for confirmmodal
jihorobert Dec 23, 2024
396dcfc
fix(fe): move confirmNavigation to utils file
jihorobert Dec 29, 2024
0058790
refactor(fe): refactor settings page using context and custom hooks
jihorobert Dec 30, 2024
e0a9dfd
Merge branch 'main' into t1115-refactor-settings
jihorobert Dec 30, 2024
c9cfb97
chore(fe): change props order
jihorobert Dec 30, 2024
34a39bb
chore(fe): change darkmode props name for BaseModal
jihorobert Dec 30, 2024
4d942a4
chore(fe): change frament tag to p tag
jihorobert Dec 30, 2024
28531bb
chore(fe): remove duplicate backdrop blur by setting it as a default …
jihorobert Dec 30, 2024
d7e2a8e
refactor(fe): move hooks folder to libs folder
jihorobert Dec 30, 2024
d94655d
fix(fe): change dependency for ConfirmNavigation
jihorobert Dec 30, 2024
c971660
fix(fe): change dashboard image alignment (#2271)
HypePenguin Jan 2, 2025
4c2bbfe
fix(fe): fix mouse scroll cursor problem (#2270)
mingyu27 Jan 2, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import BaseModal from '@/components/BaseModal'
import {
AlertDialogAction,
AlertDialogCancel,
AlertDialogFooter
} from '@/components/shadcn/alert-dialog'

interface ModalProps {
open: boolean
handleOpen: () => void
handleClose: () => void
confirmAction: () => void
title?: string
description?: string
}

/**
*
* ConfirmModal component renders a modal dialog with confirm and cancel actions.
*
* @param open - Determines if the modal is open.
* @param handleClose - Function to close the modal.
* @param confirmAction - Function to execute when the user confirms.
* @param title - Title of the modal.
* @param description - Description of the modal.
*
* @remarks
* * AlertDialogFooter section (Button section) is separated using ConfirmModal component for reusability.
*/
export default function ConfirmModal({
open,
handleClose,
confirmAction,
title = '',
description = ''
}: ModalProps) {
return (
<BaseModal
handleClose={handleClose}
title={title}
open={open}
description={description}
darkMode={false}
>
<AlertDialogFooter>
<AlertDialogAction
className="border-none bg-slate-100 text-[#3333334D] hover:bg-slate-200"
onClick={confirmAction}
>
Leave
</AlertDialogAction>
<AlertDialogCancel
className="bg-primary hover:bg-primary-strong border-none text-white"
onClick={handleClose}
>
Stay
</AlertDialogCancel>
</AlertDialogFooter>
</BaseModal>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<React.SetStateAction<boolean>>
passwordShow: boolean
checkPassword: () => Promise<void>
register: UseFormRegister<SettingsFormat>
errors: FieldErrors<SettingsFormat>
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 (
<>
<label className="-mb-4 mt-4 text-xs">Password</label>
Expand Down
Original file line number Diff line number Diff line change
@@ -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 (
<>
<label className="-mb-4 text-xs">ID</label>
<Input
placeholder={isLoading ? 'Loading...' : defaultUsername}
placeholder={isLoading ? 'Loading...' : defaultProfileValues.username}
disabled={true}
className="border-neutral-300 text-neutral-600 placeholder:text-neutral-400 disabled:bg-neutral-200"
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<React.SetStateAction<boolean>>
majorValue: string
setMajorValue: React.Dispatch<React.SetStateAction<string>>
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 (
<>
<label className="-mb-4 mt-2 text-xs">First Major</label>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<SettingsFormat>
errors: FieldErrors<SettingsFormat>
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 (
<>
<label className="-mb-4 text-xs">Name</label>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<React.SetStateAction<boolean>>
newPasswordAble: boolean
isPasswordsMatch: boolean
newPassword: string
confirmPassword: string
updateNow: boolean
register: UseFormRegister<SettingsFormat>
errors: FieldErrors<SettingsFormat>
}

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 (
<>
<div className="flex items-center gap-2">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<React.SetStateAction<boolean>>
newPasswordAble: boolean
updateNow: boolean
register: UseFormRegister<SettingsFormat>
getValues: UseFormGetValues<SettingsFormat>
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 */}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 (
<div className="mt-2 text-end">
<Button
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
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 StudentIdSectionProps {
studentId: string
updateNow: boolean
isLoading: boolean
errors: FieldErrors<SettingsFormat>
register: UseFormRegister<SettingsFormat>
defaultProfileValues: {
studentId?: string
}
}

export default function StudentIdSection({
studentId,
updateNow,
isLoading,
errors,
register,
defaultProfileValues
}: StudentIdSectionProps) {
export default function StudentIdSection({ studentId }: StudentIdSectionProps) {
const {
isLoading,
updateNow,
defaultProfileValues,
formState: { register, errors }
} = useSettingsContext()
return (
<>
<label className="-mb-4 mt-2 text-xs">Student ID</label>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export default function TopicSection({ updateNow }: { updateNow: boolean }) {
import { useSettingsContext } from './context'

export default function TopicSection() {
const { updateNow } = useSettingsContext()

return (
<>
<h1 className="-mb-1 text-center text-2xl font-bold">Settings</h1>
Expand Down
Loading