-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a93b15f
commit b43a30a
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
packages/web/src/components/useProjectInviteCodeHandler/index.ts
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 @@ | ||
export * from './useProjectInviteCodeHandler' |
56 changes: 56 additions & 0 deletions
56
packages/web/src/components/useProjectInviteCodeHandler/useProjectInviteCodeHandler.tsx
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,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 }; |