Skip to content

Commit

Permalink
add the files...
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderallen-aa committed Jan 3, 2024
1 parent a93b15f commit b43a30a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useProjectInviteCodeHandler'
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { useEffect } from 'react';
import { useRouter } from 'next/router';
import axios from 'axios';
import { useUserStore } from '../../stores/user';
import { openErrorToast, openSuccessToast } from '../utils/CustomToast';
import { useRedirectToAuth } from '../layout/RedirectToAuthModal';

const toasts = {
Success: () => openSuccessToast({
title: `Registration successful!`,
description: "You're all set! 🚀",
}) ,
Invalid: () => openErrorToast({
title: 'Invalid invite code',
description: 'Please check your invite code and try again.',
}),
ProjectExists: () => openErrorToast({
title: 'Project already exists',
description: 'You already have a project.',
}),
}

const useProjectInviteCodeHandler = () => {
const router = useRouter();
const { triggerRedirect } = useRedirectToAuth();
const { user , doneLoading:userLoaded } = useUserStore();
const { projectInviteCode } = router.query;

useEffect(() => {
if (!userLoaded || !projectInviteCode) return;
if (!user) {
triggerRedirect({returnTo:`/?projectInviteCode=${projectInviteCode}`});
return;
}
if (user?.project) {
toasts.ProjectExists();
return;
}

void ( async () => {
let project;
try {
project = (await axios.put(`/api/project/contributors`, { inviteCode:projectInviteCode } )).data
} catch {
toasts.Invalid();
return;
}
toasts.Success();

await useUserStore.getState().fetchUser();
void router.push(`/project/${project.id}`);
})();
}, [projectInviteCode , userLoaded, triggerRedirect, user, router]);
}

export { useProjectInviteCodeHandler };

0 comments on commit b43a30a

Please sign in to comment.