-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
460bc52
commit 2330e8c
Showing
15 changed files
with
361 additions
and
44 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
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,50 @@ | ||
import Image from "next/image"; | ||
import { useCallback, useState } from "react"; | ||
import { useDropzone } from "react-dropzone"; | ||
|
||
interface DropzoneProps { | ||
onChange: (base64: string) => void; | ||
label: string; | ||
value?: string; | ||
disabled?: boolean; | ||
} | ||
|
||
const Dropzone: React.FC<DropzoneProps> = ({ onChange, label, value, disabled }) => { | ||
const [base64, setBase64] = useState(value); | ||
|
||
const handleChange = useCallback((base64: string) => { | ||
onChange(base64); | ||
}, [onChange]); | ||
|
||
const handleDrop = useCallback((files: any) => { | ||
const file = files[0] | ||
const reader = new FileReader(); | ||
reader.onload = (event: any) => { | ||
setBase64(event.target.result); | ||
handleChange(event.target.result); | ||
}; | ||
reader.readAsDataURL(file); | ||
}, [handleChange]) | ||
|
||
const { getRootProps, getInputProps } = useDropzone({ maxFiles: 1, onDrop: handleDrop, disabled }); | ||
|
||
return ( | ||
<div {...getRootProps({className: 'w-full p-4 text-white text-center border-2 border-dotted rounded-md border-neutral-700'})}> | ||
<input {...getInputProps()} /> | ||
{base64 ? ( | ||
<div className="flex items-center justify-center"> | ||
<Image | ||
src={base64} | ||
height="100" | ||
width="100" | ||
alt="Uploaded image" | ||
/> | ||
</div> | ||
) : ( | ||
<p className="text-white">{label}</p> | ||
)} | ||
</div> | ||
); | ||
} | ||
|
||
export default Dropzone; |
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
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,89 @@ | ||
import axios from "axios"; | ||
import { useCallback, useEffect, useState } from "react"; | ||
import { toast } from "react-hot-toast"; | ||
|
||
import useCurrentUser from "@/hooks/useCurrentUser"; | ||
import useEditModal from "@/hooks/useEditModal"; | ||
import useUser from "@/hooks/useUser"; | ||
|
||
import Input from "../Input"; | ||
import Modal from "../Modal"; | ||
import Dropzone from "../Dropzone"; | ||
|
||
const EditModal = () => { | ||
const { data: currentUser } = useCurrentUser(); | ||
const { mutate: mutateFetchedUser } = useUser(currentUser?.id); | ||
const editModal = useEditModal(); | ||
|
||
const [image, setImage] = useState(''); | ||
const [coverImage, setCoverImage] = useState(''); | ||
const [name, setName] = useState(''); | ||
const [username, setUsername] = useState(''); | ||
const [bio, setBio] = useState(''); | ||
|
||
useEffect(() => { | ||
setImage(currentUser?.image) | ||
setCoverImage(currentUser?.coverImage) | ||
setName(currentUser?.name) | ||
setUsername(currentUser?.username) | ||
setBio(currentUser?.bio) | ||
}, [currentUser?.name, currentUser?.username, currentUser?.bio, currentUser?.image, currentUser?.coverImage]); | ||
|
||
const [isLoading, setIsLoading] = useState(false); | ||
|
||
const onSubmit = useCallback(async () => { | ||
try { | ||
setIsLoading(true); | ||
|
||
await axios.patch('/api/edit', { name, username, bio, image, coverImage }); | ||
mutateFetchedUser(); | ||
|
||
toast.success('Updated'); | ||
|
||
editModal.onClose(); | ||
} catch (error) { | ||
toast.error('Something went wrong'); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}, [editModal, name, username, bio, mutateFetchedUser, image, coverImage]); | ||
|
||
const bodyContent = ( | ||
<div className="flex flex-col gap-4"> | ||
<Dropzone value={image} disabled={isLoading} onChange={(image) => setImage(image)} label="Upload profile image" /> | ||
<Dropzone value={coverImage} disabled={isLoading} onChange={(image) => setCoverImage(image)} label="Upload cover image" /> | ||
<Input | ||
placeholder="Name" | ||
onChange={(e) => setName(e.target.value)} | ||
value={name} | ||
disabled={isLoading} | ||
/> | ||
<Input | ||
placeholder="Username" | ||
onChange={(e) => setUsername(e.target.value)} | ||
value={username} | ||
disabled={isLoading} | ||
/> | ||
<Input | ||
placeholder="Bio" | ||
onChange={(e) => setBio(e.target.value)} | ||
value={bio} | ||
disabled={isLoading} | ||
/> | ||
</div> | ||
) | ||
|
||
return ( | ||
<Modal | ||
disabled={isLoading} | ||
isOpen={editModal.isOpen} | ||
title="Edit your profile" | ||
actionLabel="Save" | ||
onClose={editModal.onClose} | ||
onSubmit={onSubmit} | ||
body={bodyContent} | ||
/> | ||
); | ||
} | ||
|
||
export default EditModal; |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { create } from 'zustand'; | ||
|
||
interface EditModalStore { | ||
isOpen: boolean; | ||
onOpen: () => void; | ||
onClose: () => void; | ||
} | ||
|
||
const useEditModal = create<EditModalStore>((set) => ({ | ||
isOpen: false, | ||
onOpen: () => set({ isOpen: true }), | ||
onClose: () => set({ isOpen: false }) | ||
})); | ||
|
||
|
||
export default useEditModal; |
Oops, something went wrong.