Skip to content

Commit

Permalink
Revalidate pages after joining and some small code refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
barbarah committed Oct 13, 2023
1 parent 952da80 commit ed42d00
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 18 deletions.
23 changes: 18 additions & 5 deletions apps/researcher/src/app/[locale]/communities/[slug]/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
10 changes: 6 additions & 4 deletions apps/researcher/src/app/[locale]/communities/[slug]/buttons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -43,7 +43,7 @@ export default function EditDescriptionForm({

const onSubmit: SubmitHandler<FormValues> = async descriptionFormValues => {
try {
await updateDescriptionAction(descriptionFormValues);
await updateDescriptionAndRevalidate(descriptionFormValues);
addNotification({
id: 'add-object-list-success',
message: <>{t('descriptionUpdated')}</>,
Expand Down
5 changes: 4 additions & 1 deletion apps/researcher/src/app/[locale]/communities/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ export default async function CommunityPage({params}: Props) {
</SlideOut>
</div>
<div className="flex flex-col items-start md:justify-center md:items-center w-full mb-4">
<JoinCommunityButton communityId={community.id} />
<JoinCommunityButton
communityId={community.id}
communitySlug={params.slug}
/>
<SlideOutButton
hideIfOpen
id={slideOutDescriptionId}
Expand Down
9 changes: 3 additions & 6 deletions apps/researcher/src/lib/community.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,13 @@ export function isAdmin(memberships: ReadonlyArray<Membership>): 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',
});
Expand Down

0 comments on commit ed42d00

Please sign in to comment.