-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add profile picture change functionality
- Loading branch information
1 parent
6cc0066
commit 10451bb
Showing
4 changed files
with
136 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
'use client'; | ||
|
||
import React, { useCallback } from 'react'; | ||
import { useRouter } from 'next/navigation'; | ||
import { Controller, type SubmitHandler, useForm } from 'react-hook-form'; | ||
import { toast } from 'react-toastify'; | ||
import { zodResolver } from '@hookform/resolvers/zod'; | ||
import { z } from 'zod'; | ||
import { Modal, ModalButton, type ModalProps } from '@/components/Modal'; | ||
import { FileInput } from '@/components/Forms'; | ||
import { Button } from '@/components/Button'; | ||
import { fetchApi } from '@/utils/fetch'; | ||
import { type User } from '@/types/users'; | ||
|
||
const profilePictureSchema = z.object({ | ||
avatar: z.union([ | ||
z.instanceof(File, { message: 'This field is required' }), | ||
z.string(), | ||
]), | ||
}); | ||
|
||
type ProfilePictureFormValues = z.infer<typeof profilePictureSchema>; | ||
|
||
interface ChangeProfilePictureModalProps extends ModalProps { | ||
user: User; | ||
} | ||
|
||
export const ChangeProfilePictureModal: React.FC<ChangeProfilePictureModalProps> = ({ user, show, close }) => { | ||
const router = useRouter(); | ||
|
||
const { control, handleSubmit, formState: { errors, isSubmitting } } = useForm<ProfilePictureFormValues>({ | ||
resolver: zodResolver(profilePictureSchema), | ||
}); | ||
|
||
const putProfilePicture: SubmitHandler<ProfilePictureFormValues> = useCallback((values) => { | ||
const data = new FormData(); | ||
|
||
data.append('avatar', values.avatar); | ||
|
||
toast.promise( | ||
fetchApi(`/users/${user.cid}/`, { method: 'PUT', body: data }), | ||
{ | ||
pending: 'Saving changes', | ||
success: 'Successfully saved', | ||
error: 'Something went wrong, check console for more info', | ||
}, | ||
) | ||
.then(() => { | ||
router.refresh(); | ||
close?.(); | ||
}); | ||
}, [user, router, close]); | ||
|
||
const resetProfilePicture = () => putProfilePicture({ avatar: '' }); | ||
|
||
return ( | ||
<Modal show={show} title="Change Profile Picture" close={close}> | ||
<form onSubmit={handleSubmit(putProfilePicture)}> | ||
<Controller | ||
name="avatar" | ||
control={control} | ||
render={({ | ||
field: { | ||
value, | ||
onChange, | ||
}, | ||
}) => ( | ||
<FileInput | ||
className="mb-5" | ||
error={errors.avatar?.message} | ||
currentFile={value} | ||
onUpload={(acceptedFiles) => onChange(acceptedFiles[0])} | ||
/> | ||
)} | ||
/> | ||
|
||
<div className="flex justify-end gap-3"> | ||
<Button color="red-400" className="mr-auto" onClick={resetProfilePicture}> | ||
Reset | ||
</Button> | ||
<Button color="gray-300" onClick={close}> | ||
Close | ||
</Button> | ||
<Button type="submit" disabled={isSubmitting}> | ||
Save | ||
</Button> | ||
</div> | ||
</form> | ||
</Modal> | ||
); | ||
}; | ||
|
||
export const ChangeProfilePictureButton: React.FC<ChangeProfilePictureModalProps> = ({ ...props }) => ( | ||
<ModalButton modal={<ChangeProfilePictureModal {...props} />}> | ||
Change | ||
</ModalButton> | ||
); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,41 @@ | ||
import React from 'react'; | ||
import Image from 'next/image'; | ||
import classNames from 'classnames'; | ||
import { LuUser } from 'react-icons/lu'; | ||
import { type BasicUser } from '@/types/users'; | ||
|
||
interface ProfilePictureProps { | ||
user?: BasicUser; | ||
alt?: string; | ||
size: number; | ||
className?: string; | ||
} | ||
|
||
export const ProfilePicture: React.FC<ProfilePictureProps> = ({ user, size, alt = '', className }) => { | ||
if (!user) { | ||
export const ProfilePicture: React.FC<ProfilePictureProps> = ({ user, size, className }) => { | ||
if (user?.profile) { | ||
return ( | ||
<Image | ||
className={classNames('rounded-full bg-slate-300', className)} | ||
src="/img/profile.png" | ||
alt={alt} | ||
src={`${process.env.NEXT_PUBLIC_API_URL}${user.profile}`} | ||
alt={`${user.first_name} ${user.last_name}`} | ||
height={size} | ||
width={size} | ||
/> | ||
); | ||
} | ||
|
||
if (!user.profile) { | ||
return ( | ||
<div | ||
className={classNames( | ||
'flex items-center justify-center rounded-full bg-slate-300', | ||
className, | ||
)} | ||
style={{ | ||
width: size, | ||
height: size, | ||
fontSize: size / 2, | ||
}} | ||
> | ||
<span className="font-medium text-slate-600">{user.initials}</span> | ||
</div> | ||
); | ||
} | ||
|
||
return ( | ||
<Image | ||
className={classNames('rounded-full bg-slate-300', className)} | ||
src={`${process.env.NEXT_PUBLIC_API_URL}${user.profile}`} | ||
alt={`${user.first_name} ${user.last_name}`} | ||
height={size} | ||
width={size} | ||
/> | ||
<div | ||
className={classNames( | ||
'flex items-center justify-center rounded-full bg-slate-300 font-medium text-slate-600', | ||
className, | ||
)} | ||
style={{ | ||
width: size, | ||
height: size, | ||
fontSize: size / 2, | ||
}} | ||
> | ||
{user ? user.initials : <LuUser />} | ||
</div> | ||
); | ||
}; |