diff --git a/apps/researcher/src/app/[locale]/communities/[slug]/actions.ts b/apps/researcher/src/app/[locale]/communities/[slug]/actions.ts index 0c1695542..774cdee73 100644 --- a/apps/researcher/src/app/[locale]/communities/[slug]/actions.ts +++ b/apps/researcher/src/app/[locale]/communities/[slug]/actions.ts @@ -1,6 +1,29 @@ 'use server'; -import {joinCommunity} from '@/lib/community'; +import {joinCommunity, editDescription} from '@/lib/community'; +import {revalidatePath} from 'next/cache'; + +interface editDescriptionActionProps { + communityId: string; + communitySlug: string; + description: string; +} + +async function editDescriptionAction({ + communityId, + communitySlug, + description, +}: editDescriptionActionProps) { + try { + await editDescription({communityId, description}); + revalidatePath(`/[locale]/communities/${communitySlug}`, 'page'); + revalidatePath('/[locale]/communities', 'page'); + + return {statusCode: 200}; + } catch (err) { + return {statusCode: 500}; + } +} // Export as server actions. -export {joinCommunity}; +export {joinCommunity, editDescriptionAction}; diff --git a/apps/researcher/src/app/[locale]/communities/[slug]/edit-description-form.tsx b/apps/researcher/src/app/[locale]/communities/[slug]/edit-description-form.tsx new file mode 100644 index 000000000..8a871dfb7 --- /dev/null +++ b/apps/researcher/src/app/[locale]/communities/[slug]/edit-description-form.tsx @@ -0,0 +1,113 @@ +'use client'; + +import {useForm, SubmitHandler} from 'react-hook-form'; +import {useTranslations} from 'next-intl'; +import {useSlideOut, useNotifications} from 'ui'; +import {editDescriptionAction} from './actions'; + +interface Props { + communityId: string; + communitySlug: string; + slideOutId: string; + description?: string; +} + +interface FormValues { + description: string; + communityId: string; + communitySlug: string; +} + +export default function EditDescriptionForm({ + slideOutId, + communityId, + communitySlug, + description, +}: Props) { + const { + register, + handleSubmit, + setError, + formState: {errors, isSubmitting}, + } = useForm({ + defaultValues: { + description: description ?? '', + communityId, + communitySlug, + }, + }); + + const t = useTranslations('Community'); + const {setIsVisible} = useSlideOut(); + const {addNotification} = useNotifications(); + + const onSubmit: SubmitHandler = async data => { + const response = await editDescriptionAction(data); + + if (response.statusCode > 200) { + setError('root.serverError', { + message: t('serverError'), + }); + } else { + addNotification({ + id: 'add-object-list-success', + message: <>{t('descriptionSuccessfullyEdited')}, + type: 'success', + }); + setIsVisible(slideOutId, false); + } + }; + + return ( +
+ {errors.root?.serverError.message && ( +
+
+

+ {errors.root.serverError.message} +

+
+
+ )} +
+
+ +