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

Fix form input bug in User Profile #154

Merged
merged 4 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
62 changes: 45 additions & 17 deletions src/component/form/form-field/form-field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ interface ExtendedFormFieldProps extends FormFieldProps {
errors?: any
helperText?: string
defaultValue?: string
isAccount?: boolean
disabled?: boolean
setValue?: (value: string) => void
}

Expand All @@ -37,6 +39,8 @@ const FormField = forwardRef(
helperText,
isGithub,
errors,
isAccount = false,
disabled = false,
required = false
}: ExtendedFormFieldProps,
ref
Expand All @@ -56,10 +60,10 @@ const FormField = forwardRef(

return (
<TextField
variant='filled'
variant={isAccount ? 'outlined' : 'filled'}
color='primary'
autoComplete={autoComplete}
disabled={submitting || sent}
disabled={submitting || sent || disabled}
label={label}
placeholder={placeholder}
type={showPassword ? type : isPassword ? KEY.PASSWORD : type}
Expand Down Expand Up @@ -95,26 +99,50 @@ const FormField = forwardRef(
fullWidth
{...register(name)}
sx={{
color: 'common.white',
'& .MuiFilledInput-root': {
color: 'common.black',
backgroundColor: 'common.white',
'& .MuiInputLabel-root': {
...(!isAccount && {
color: 'common.white',
'& .MuiFilledInput-root': {
color: 'common.black',
backgroundColor: 'common.white',
'& .MuiInputLabel-root': {
color: 'common.white'
},
'&.Mui-focused .MuiInputBase-input': {
color: 'common.black'
},
'&.Mui-focused': {
backgroundColor: 'secondary.main'
},
'&:hover': {
backgroundColor: 'secondary.main'
}
},
'&:active': {
color: 'common.white'
}
}),
// if isAccount is true, then apply the following styles
...(isAccount && {
'& .MuiOutlinedInput-notchedOutline': {
border: 'none',
fontWeight: 'bold'
},
'&.Mui-focused': {
backgroundColor: 'transparent'
},
'&.Mui-focused .MuiInputBase-input': {
// edit label color
'& .MuiInputLabel-root': {
color: 'common.black'
},
'&.Mui-focused': {
backgroundColor: 'secondary.main'
// disabled text color
'& .MuiInputBase-input ': {
color: 'grey.800',
opacity: 1,
fontWeight: 'bold'
},
'&:hover': {
backgroundColor: 'secondary.main'
}
},
'&:active': {
color: 'common.white'
},
backgroundColor: 'transparent',
fontWeight: 'bold'
}),
padding: 1,
marginY: 1
}}
Expand Down
2 changes: 2 additions & 0 deletions src/config/icon-directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const ICON_WEB = {
MODE_LIGHT: _getWebIcon('sun'),
MODE_DARK: _getWebIcon('moon'),

SAVE: _getWebIcon('save-outline'),
SUCCESS: _getWebIcon('checkmark-circle-2'),
SETTING: _getWebIcon('cog'),
WARNING: _getWebIcon('alert-triangle'),
Expand Down Expand Up @@ -87,6 +88,7 @@ export enum ICON_WEB_NAME {
FULLSCREEN_EXIT = 'FULLSCREEN_EXIT',
REFRESH = 'REFRESH',

SAVE = 'SAVE',
SUCCESS = 'SUCCESS',
SETTING = 'SETTING',
WARNING = 'WARNING',
Expand Down
1 change: 1 addition & 0 deletions src/constant/key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ enum KEY {
PHOTO_DEFAULT = 'no-photo.jpeg',
BADGE_DEFAULT = 'no-badge.png',
// @form
FULL_NAME = 'fullname',
FIRST_NAME = 'firstname',
LAST_NAME = 'lastname',
EMAIL = 'email',
Expand Down
244 changes: 179 additions & 65 deletions src/page/auth/account/user-account.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,87 @@
import React, { useEffect } from 'react'
import { useState, useEffect, Fragment } from 'react'
import * as Yup from 'yup'
import { yupResolver } from '@hookform/resolvers/yup'
import { ICON_NAME, useIcon } from 'hook'
import { CardMedia, Box, Typography, TextField, Button, Avatar, Grid, Divider, Accordion, AccordionSummary, AccordionDetails } from '@mui/material'
import { useForm } from 'react-hook-form'
import { useUpdateUserMutation } from 'store/slice'
import { CardMedia, Box, Typography, Grid, Divider, Accordion, AccordionSummary, AccordionDetails, IconButton } from '@mui/material'
import { useTheme } from '@mui/material/styles'
import { FormField, FormProvider } from 'component/form'
import { MotionLazyContainer } from 'component/motion'
import { BootcampCard } from 'section/bootcamp'
import { DefaultAvatar } from 'component/avatar'
import { useAccountQuery } from 'store/slice'
import { SCard } from 'theme/style'
import { ASSET } from 'config'
import { KEY } from 'constant'
import { mockBootcamp } from '_mock'

const UserAccount = () => {
const theme = useTheme()
const { data: account, isLoading, refetch, error } = useAccountQuery({}) as any
const [isEditState, setIsEditState] = useState<boolean>(false)
const { data: account, isLoading, error, refetch } = useAccountQuery({}) as any
const [updateAccount] = useUpdateUserMutation()
const { Icon, iconSrc: editIconSrc } = useIcon(ICON_NAME.EDIT)
const { iconSrc: downIconSrc } = useIcon(ICON_NAME.CHEVRON_DOWN)
const { iconSrc: saveSrc } = useIcon(ICON_NAME.SAVE)
const theme = useTheme()

const date = new Date(account?.data?.createdAt)
const year = date.getFullYear()
const fullName = account?.data?.firstname + ' ' + account?.data?.lastname
const fullname = account?.data?.firstname + ' ' + account?.data?.lastname

const accountSchema = Yup.object().shape({
firstname: Yup.string().required(),
lastname: Yup.string().required(),
fullname: Yup.string(),
location: Yup.string(),
email: Yup.string().email(),
password: Yup.string(),
confirmPassword: Yup.string().oneOf([Yup.ref('password'), ''], 'Passwords must match')
})

const methods = useForm({
resolver: yupResolver(accountSchema),
defaultValues: {
firstname: account?.data?.firstname || '',
lastname: account?.data?.lastname || '',
email: account?.data?.email || '',
location: account?.data?.location || '',
fullname,
password: '',
confirmPassword: ''
}
})

const {
control,
handleSubmit,
reset,
setError,
formState: { errors, isSubmitting, isSubmitSuccessful }
} = methods

const onSubmit = async (data: any) => {
try {
if (account) {
await updateAccount(data)
refetch()
}
} catch (error: any) {
setError('email', {
type: 'manual',
message: error.message
})
}
}

const handleEdit = () => {
setIsEditState(!isEditState)
}

// refetch account data
useEffect(() => {
refetch()
}, [refetch])

return (
<MotionLazyContainer>
Expand Down Expand Up @@ -57,51 +120,100 @@ const UserAccount = () => {
<Box mb={2}>
<Divider />
</Box>
<TextField
variant='outlined'
label='Full Name'
value={fullName}
style={{ marginTop: '1rem' }}
fullWidth
InputProps={{
sx: {
'& .MuiOutlinedInput-notchedOutline': {
border: 'none'
},
fontWeight: 'bold'
}
}}
/>
<TextField
variant='outlined'
label='Location'
value={account?.data?.location}
style={{ marginTop: '1rem' }}
fullWidth
InputProps={{
sx: {
'& .MuiOutlinedInput-notchedOutline': {
border: 'none'
},
fontWeight: 'bold'
}
}}
/>
<TextField
variant='outlined'
label='Email'
value={account?.data?.email}
style={{ marginTop: '1rem' }}
fullWidth
InputProps={{
sx: {
'& .MuiOutlinedInput-notchedOutline': {
border: 'none'
},
fontWeight: 'bold'
}
}}
/>

<FormProvider methods={methods} onSubmit={handleSubmit(onSubmit)}>
<FormField
name={KEY.EMAIL}
submitting={isSubmitting}
sent={isSubmitSuccessful}
errors={errors}
label='Email'
placeholder={account?.data?.email}
autoComplete='email'
type='email'
disabled={!isEditState}
required
isAccount
/>
<FormField
name={KEY.LOCATION}
submitting={isSubmitting}
sent={isSubmitSuccessful}
errors={errors}
label='Location'
placeholder={account?.data?.location}
autoComplete='location'
disabled={!isEditState}
required
isAccount
/>
{isEditState ? (
<Fragment>
<FormField
name={KEY.FIRST_NAME}
submitting={isSubmitting}
sent={isSubmitSuccessful}
errors={errors}
label='First name'
placeholder={account?.data?.firstname}
autoComplete='firstname'
disabled={!isEditState}
required
isAccount
/>
ballyalley-o marked this conversation as resolved.
Show resolved Hide resolved
<FormField
name={KEY.LAST_NAME}
submitting={isSubmitting}
sent={isSubmitSuccessful}
errors={errors}
label='Last name'
placeholder={account?.data?.lastname}
autoComplete='lastname'
disabled={!isEditState}
required
isAccount
/>
<Divider />
<FormField
name={KEY.PASSWORD}
submitting={isSubmitting}
sent={isSubmitSuccessful}
errors={errors}
label='Password'
placeholder='Password'
autoComplete='new-password'
disabled={!isEditState}
required
isAccount
/>
<FormField
name={KEY.CONFIRM_PASSWORD}
submitting={isSubmitting}
sent={isSubmitSuccessful}
errors={errors}
label='Confirm password'
placeholder='Confirm password'
autoComplete='new-password'
disabled={!isEditState}
required
isAccount
/>
</Fragment>
) : (
<FormField
name={KEY.FULL_NAME}
submitting={isSubmitting}
sent={isSubmitSuccessful}
errors={errors}
label='Full name'
placeholder={fullname}
autoComplete='fullname'
disabled={!isEditState}
required
isAccount
/>
)}
</FormProvider>
</Box>
</Grid>
<Grid item sm={9}>
Expand All @@ -120,21 +232,23 @@ const UserAccount = () => {
</Typography>
</Grid>
<Grid item sm={12} display='flex' justifyContent='flex-end'>
<Icon
icon={editIconSrc}
width={25}
sx={{
cursor: 'pointer',
color: theme.palette.common.white,
backgroundColor: theme.palette.primary.main,
padding: '0.5rem',
borderRadius: '.4rem',
transition: 'all 0.3s ease',
'&:hover': {
transform: 'scale(1.1)'
}
}}
/>
<IconButton onClick={handleEdit}>
<Icon
icon={isEditState ? saveSrc : editIconSrc}
width={25}
sx={{
cursor: 'pointer',
color: theme.palette.common.white,
backgroundColor: theme.palette.primary.main,
padding: '0.5rem',
borderRadius: '.4rem',
transition: 'all 0.3s ease',
'&:hover': {
transform: 'scale(1.1)'
}
}}
/>
</IconButton>
</Grid>
</Grid>
</Box>
Expand Down
Loading