From ed42d00ddfe730e0e685ee5755109aa981350ce0 Mon Sep 17 00:00:00 2001 From: Barbara Honhoff Date: Fri, 13 Oct 2023 10:04:09 +0200 Subject: [PATCH] Revalidate pages after joining and some small code refactors --- .../[locale]/communities/[slug]/actions.ts | 23 +++++++++++++++---- .../[locale]/communities/[slug]/buttons.tsx | 10 ++++---- .../[slug]/edit-description-form.tsx | 4 ++-- .../app/[locale]/communities/[slug]/page.tsx | 5 +++- apps/researcher/src/lib/community.ts | 9 +++----- 5 files changed, 33 insertions(+), 18 deletions(-) diff --git a/apps/researcher/src/app/[locale]/communities/[slug]/actions.ts b/apps/researcher/src/app/[locale]/communities/[slug]/actions.ts index b5b14b6bc..bc2831096 100644 --- a/apps/researcher/src/app/[locale]/communities/[slug]/actions.ts +++ b/apps/researcher/src/app/[locale]/communities/[slug]/actions.ts @@ -3,21 +3,34 @@ import {joinCommunity, updateDescription} from '@/lib/community'; import {revalidatePath} from 'next/cache'; -interface UpdateDescriptionActionProps { +interface UpdateDescriptionAndRevalidateProps { communityId: string; communitySlug: string; description: string; } -export async function updateDescriptionAction({ +export async function updateDescriptionAndRevalidate({ communityId, communitySlug, description, -}: UpdateDescriptionActionProps) { +}: UpdateDescriptionAndRevalidateProps) { await updateDescription({communityId, description}); revalidatePath(`/[locale]/communities/${communitySlug}`, 'page'); revalidatePath('/[locale]/communities', 'page'); } -// Export as server actions. -export {joinCommunity as joinCommunityAction}; +interface JoinCommunityAndRevalidateProps { + communityId: string; + communitySlug: string; + userId: string; +} + +export async function joinCommunityAndRevalidate({ + communityId, + communitySlug, + userId, +}: JoinCommunityAndRevalidateProps) { + await joinCommunity({communityId, userId}); + revalidatePath(`/[locale]/communities/${communitySlug}`, 'page'); + revalidatePath('/[locale]/communities', 'page'); +} diff --git a/apps/researcher/src/app/[locale]/communities/[slug]/buttons.tsx b/apps/researcher/src/app/[locale]/communities/[slug]/buttons.tsx index 6f0c57427..db626a4ff 100644 --- a/apps/researcher/src/app/[locale]/communities/[slug]/buttons.tsx +++ b/apps/researcher/src/app/[locale]/communities/[slug]/buttons.tsx @@ -4,14 +4,15 @@ import {useState} from 'react'; import {useTranslations} from 'next-intl'; import {useClerk, useUser} from '@clerk/nextjs'; import {useTransition} from 'react'; -import {joinCommunityAction} from './actions'; +import {joinCommunityAndRevalidate} from './actions'; interface Props { communityId: string; + communitySlug: string; } // If logged in and not part of the community, show the join button -export function JoinCommunityButton({communityId}: Props) { +export function JoinCommunityButton({communityId, communitySlug}: Props) { const {isLoaded, isSignedIn, user} = useUser(); const [isClicked, setIsClicked] = useState(false); const [hasError, setHasError] = useState(false); @@ -34,8 +35,9 @@ export function JoinCommunityButton({communityId}: Props) { setIsClicked(true); startTransition(async () => { try { - await joinCommunityAction({ - organizationId: communityId, + await joinCommunityAndRevalidate({ + communityId, + communitySlug, userId: user!.id, }); } catch (err) { 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 index dc07417ee..8ddb6fc92 100644 --- a/apps/researcher/src/app/[locale]/communities/[slug]/edit-description-form.tsx +++ b/apps/researcher/src/app/[locale]/communities/[slug]/edit-description-form.tsx @@ -3,7 +3,7 @@ import {useForm, SubmitHandler} from 'react-hook-form'; import {useTranslations} from 'next-intl'; import {useSlideOut, useNotifications} from 'ui'; -import {updateDescriptionAction} from './actions'; +import {updateDescriptionAndRevalidate} from './actions'; interface Props { communityId: string; @@ -43,7 +43,7 @@ export default function EditDescriptionForm({ const onSubmit: SubmitHandler = async descriptionFormValues => { try { - await updateDescriptionAction(descriptionFormValues); + await updateDescriptionAndRevalidate(descriptionFormValues); addNotification({ id: 'add-object-list-success', message: <>{t('descriptionUpdated')}, diff --git a/apps/researcher/src/app/[locale]/communities/[slug]/page.tsx b/apps/researcher/src/app/[locale]/communities/[slug]/page.tsx index 03f430a18..ccdcfdb49 100644 --- a/apps/researcher/src/app/[locale]/communities/[slug]/page.tsx +++ b/apps/researcher/src/app/[locale]/communities/[slug]/page.tsx @@ -109,7 +109,10 @@ export default async function CommunityPage({params}: Props) {
- + ): boolean { } interface JoinCommunityProps { - organizationId: string; + communityId: string; userId: string; } -export async function joinCommunity({ - organizationId, - userId, -}: JoinCommunityProps) { +export async function joinCommunity({communityId, userId}: JoinCommunityProps) { await clerkClient.organizations.createOrganizationMembership({ - organizationId, + organizationId: communityId, userId, role: 'basic_member', });