-
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.
feat: Add hook to handle invite code (#625)
* linting * add hook to handle invite code * add the files... * linting * fix tests * fix tests * fix tests * undo accidental stash pop * linting * ignore lint rule to fix over triggering of useEffect
- Loading branch information
1 parent
0c2f16f
commit 294bf26
Showing
7 changed files
with
80 additions
and
36 deletions.
There are no files selected for viewing
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
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
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 |
---|---|---|
@@ -1,6 +1,5 @@ | ||
import { z } from 'zod'; | ||
|
||
export const put = z.object({ | ||
projectId: z.string(), | ||
inviteCode: z.string().uuid(), | ||
}); |
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
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'; |
60 changes: 60 additions & 0 deletions
60
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,60 @@ | ||
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]); // eslint-disable-line react-hooks/exhaustive-deps | ||
}; | ||
|
||
export { useProjectInviteCodeHandler }; |
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