-
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.
- Loading branch information
Showing
4 changed files
with
130 additions
and
16 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,89 @@ | ||
import useUpdateUserMutation from '@hooks/mutations/useUpdateUserMutation'; | ||
import { useRef, useState } from 'react'; | ||
import toast from 'react-hot-toast/headless'; | ||
|
||
interface DescriptionProps { | ||
value: string | null; | ||
} | ||
|
||
const Description = ({ value }: DescriptionProps) => { | ||
const ref = useRef<HTMLTextAreaElement>(null); | ||
const [isEditing, setIsEditing] = useState(false); | ||
const [description, setDescription] = useState<string | undefined>( | ||
value ?? undefined, | ||
); | ||
|
||
const { mutateAsync, isLoading } = useUpdateUserMutation(); | ||
|
||
const handleEdit = () => { | ||
setIsEditing(true); | ||
}; | ||
|
||
const handleCancel = (event: React.KeyboardEvent) => { | ||
if (event.key === 'Escape') { | ||
setIsEditing(false); | ||
} | ||
}; | ||
|
||
const handleSave = async () => { | ||
const value = ref.current?.value.trim(); | ||
|
||
if (value) { | ||
try { | ||
await mutateAsync({ description: value }).then(() => { | ||
setDescription(value); | ||
setIsEditing(false); | ||
}); | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
toast('Something went wrong!'); | ||
return; | ||
} | ||
} | ||
} | ||
}; | ||
|
||
if (!isEditing) { | ||
return ( | ||
<div | ||
className="-mx-2 -my-1 cursor-pointer rounded-lg px-2 py-1 text-sm transition-colors hover:bg-black-pearl-800" | ||
onClick={handleEdit} | ||
> | ||
{!description ? ( | ||
<p className="text-slate-400">Tell something about yourself...</p> | ||
) : ( | ||
<p>{description}</p> | ||
)} | ||
</div> | ||
); | ||
} | ||
|
||
return ( | ||
<div className="flex flex-col items-end"> | ||
<textarea | ||
ref={ref} | ||
defaultValue={description} | ||
className="textarea textarea-bordered h-32 w-full" | ||
onKeyDown={handleCancel} | ||
autoFocus | ||
onFocus={(e) => { | ||
const val = e.target.value; | ||
e.target.value = ''; | ||
e.target.value = val; | ||
}} | ||
/> | ||
<button | ||
className="btn btn-sm mt-2 bg-primary text-neutral hover:bg-anzac-300" | ||
onClick={handleSave} | ||
> | ||
{!isLoading ? ( | ||
'Save' | ||
) : ( | ||
<span className="loading loading-spinner"></span> | ||
)} | ||
</button> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Description; |
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