-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add text block edit functionality (#12)
* feat: add text block edit functionality * feat(EditTextBlockDialog): use the right title and placeholders * feat(AddTextBlockDialog): change button label from "Save" to "Add" to better convey the intent * Update src/components/digests/dialog/EditTextBlockDialog.tsx Co-authored-by: Baptiste Adrien <[email protected]> * Update src/components/digests/dialog/EditTextBlockDialog.tsx Co-authored-by: Baptiste Adrien <[email protected]> * feat: add use effect to register dialog text area default value change --------- Co-authored-by: Baptiste Adrien <[email protected]>
- Loading branch information
Showing
4 changed files
with
215 additions
and
42 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
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,135 @@ | ||
import { useTeam } from '@/contexts/TeamContext'; | ||
import useCustomToast from '@/hooks/useCustomToast'; | ||
import useTransitionRefresh from '@/hooks/useTransitionRefresh'; | ||
import api from '@/lib/api'; | ||
import { AxiosError, AxiosResponse } from 'axios'; | ||
import { useParams } from 'next/navigation'; | ||
import React, { useEffect } from 'react'; | ||
import { SubmitHandler, useForm } from 'react-hook-form'; | ||
import { useMutation } from 'react-query'; | ||
import Button from '../../Button'; | ||
import { Dialog, DialogContent } from '../../Dialog'; | ||
import { TextArea } from '../../Input'; | ||
import { Props as BookmarkCardProps } from '../block-card/BlockCard'; | ||
|
||
interface FormValues { | ||
text: string; | ||
} | ||
interface Props { | ||
isOpen: boolean; | ||
setIsOpen: (isOpen: boolean) => void; | ||
bookmarkDigest: BookmarkCardProps['block']; | ||
defaultValues?: Partial<FormValues>; | ||
} | ||
|
||
export default function EditTextBlockDialog({ | ||
isOpen, | ||
setIsOpen, | ||
bookmarkDigest, | ||
defaultValues, | ||
}: Props) { | ||
const { | ||
register, | ||
handleSubmit, | ||
formState: { errors, isDirty }, | ||
reset, | ||
} = useForm<FormValues>({ | ||
defaultValues, | ||
}); | ||
const { successToast, errorToast } = useCustomToast(); | ||
const { isRefreshing, refresh } = useTransitionRefresh(); | ||
|
||
useEffect(() => { | ||
reset(defaultValues); | ||
}, [defaultValues, reset]); | ||
|
||
const params = useParams(); | ||
const { id: teamId } = useTeam(); | ||
|
||
const { mutate: updateBookDigest, isLoading: isRemoving } = useMutation< | ||
AxiosResponse, | ||
AxiosError<ErrorResponse>, | ||
FormValues | ||
>( | ||
'update-bookmarkdigest', | ||
({ text }) => { | ||
return api.patch( | ||
`/teams/${teamId}/digests/${params?.digestId}/block/${bookmarkDigest.id}`, | ||
{ text } | ||
); | ||
}, | ||
{ | ||
onSuccess: () => { | ||
successToast('Digest updated'); | ||
closeDialog(); | ||
refresh(); | ||
}, | ||
onError: (error: AxiosError<ErrorResponse>) => { | ||
errorToast( | ||
error.response?.data?.error || | ||
error.response?.statusText || | ||
error.message || | ||
'Something went wrong' | ||
); | ||
}, | ||
} | ||
); | ||
|
||
const onSubmit: SubmitHandler<FormValues> = (data: FormValues) => { | ||
updateBookDigest(data); | ||
}; | ||
|
||
function closeDialog() { | ||
reset(); | ||
setIsOpen(false); | ||
} | ||
|
||
useEffect(() => { | ||
if (Object.keys(errors).length > 0) { | ||
Object.values(errors).forEach((error) => { | ||
if (error.message) { | ||
errorToast(error.message); | ||
} | ||
}); | ||
} | ||
}, [errors, errorToast]); | ||
|
||
return ( | ||
<Dialog open={isOpen} onOpenChange={() => setIsOpen(!open)}> | ||
<DialogContent | ||
title="Edit a text block" | ||
description="Write something to add to your digest, markdown is supported." | ||
closeIcon | ||
containerClassName="w-full sm:max-w-3xl" | ||
> | ||
<form | ||
className="flex flex-col items-end mt-4 gap-6 w-full max-w-3xl" | ||
onSubmit={handleSubmit(onSubmit)} | ||
> | ||
<TextArea | ||
className="min-h-[10rem]" | ||
placeholder="Write something..." | ||
{...register('text')} | ||
rows={10} | ||
/> | ||
<div className="flex justify-start gap-4 w-full items-center"> | ||
<Button | ||
isLoading={isRemoving || isRefreshing} | ||
type="submit" | ||
disabled={!isDirty} | ||
> | ||
Save | ||
</Button> | ||
<Button | ||
type="button" | ||
variant="destructiveGhost" | ||
onClick={closeDialog} | ||
> | ||
Cancel | ||
</Button> | ||
</div> | ||
</form> | ||
</DialogContent> | ||
</Dialog> | ||
); | ||
} |
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
49eedba
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
digestclub – ./
digestclub-git-main-premieroctet.vercel.app
digestclub-premieroctet.vercel.app
digestclub.vercel.app