diff --git a/studio/pages/join/[slug].tsx b/studio/pages/join/[slug].tsx index 5993eafbf1d..42b8cf732e3 100644 --- a/studio/pages/join/[slug].tsx +++ b/studio/pages/join/[slug].tsx @@ -1,126 +1,153 @@ import React from 'react' import { observer } from 'mobx-react-lite' import { useStore, withAuth } from 'hooks' -import { Button, Typography } from '@supabase/ui'; -import { useState } from 'react'; -import { useRouter } from 'next/router'; -import Link from 'next/link'; +import { Button, Typography } from '@supabase/ui' +import { useState } from 'react' +import { useRouter } from 'next/router' +import Link from 'next/link' import { API_URL } from 'lib/constants' import { get, post, delete_ } from 'lib/common/fetch' import { useEffect } from 'react' -interface tokenInfo { - organization_name: string | undefined, - token_does_not_exist: boolean, - email_match: boolean, - authorized_user: boolean, - expired_token: boolean, - invite_id: number +interface TokenInfo { + organization_name: string | undefined + token_does_not_exist: boolean + email_match: boolean + authorized_user: boolean + expired_token: boolean + invite_id: number } const User = () => { - const router = useRouter() - const {slug, token} = router.query - const { ui } = useStore() - const [isSubmitting, setIsSubmitting] = useState( false ) - const [tokenValidationInfo, setTokenValidationInfo] = useState() - const { - token_does_not_exist, - email_match, - authorized_user, - expired_token, - organization_name, - invite_id - } = tokenValidationInfo || {} + const router = useRouter() + const { slug, token } = router.query + const { ui } = useStore() + const [isSubmitting, setIsSubmitting] = useState(false) + const [tokenValidationInfo, setTokenValidationInfo] = useState() + const { + token_does_not_exist, + email_match, + authorized_user, + expired_token, + organization_name, + invite_id, + } = tokenValidationInfo || {} + + useEffect(() => { + let cancel = false async function fetchTokenInfo() { - const response = await get( `${API_URL}/organizations/${slug}/members/join?token=${token}`, {} ) - setTokenValidationInfo(response); + const response = await get(`${API_URL}/organizations/${slug}/members/join?token=${token}`, {}) + if (!cancel) setTokenValidationInfo(response) } - useEffect(() => { - if(router.query.token) { - fetchTokenInfo() - } - }, [] ) - - async function handleJoinOrganization() { - setIsSubmitting( true ) - const response = await post( `${API_URL}/organizations/${slug}/members/join?token=${token}`, {} ) - if ( response.error ) { - ui.setNotification( { - category: 'error', - message: `Failed to join organization: ${response.error.message}`, - } ) - setIsSubmitting( false ) - } else { - setIsSubmitting( false ) - router.push( '/' ) - } + if (router.query.token) { + fetchTokenInfo() } - async function handleDeclineJoinOrganization() { - setIsSubmitting( true ) - const response = await delete_(`${API_URL}/organizations/${slug}/members/invite?invited_id=${invite_id}`, {}) - if ( response.error ) { - ui.setNotification( { - category: 'error', - message: `Failed to decline invitation: ${response.error.message}`, - } ) - setIsSubmitting( false ) - } else { - setIsSubmitting( false ) - router.push( '/' ) - } + return () => { + cancel = true } + }, []) - return ( -
-
-
- - - Supabase - Supabase - - + async function handleJoinOrganization() { + setIsSubmitting(true) + const response = await post(`${API_URL}/organizations/${slug}/members/join?token=${token}`, {}) + if (response.error) { + ui.setNotification({ + category: 'error', + message: `Failed to join organization: ${response.error.message}`, + }) + setIsSubmitting(false) + } else { + setIsSubmitting(false) + router.push('/') + } + } -

Join {organization_name ? organization_name : 'a new organization'}

-

You have been invited to join {organization_name ? organization_name : 'a new organization'} organization at Supabase.

-
- - { authorized_user && !expired_token && ( -
- - - -
- )} - - {!tokenValidationInfo && ( -
That invite token is invalid.
- )} - - {tokenValidationInfo && !email_match && ( -
Sign in to that account to accept this invitation.
- )} - - {expired_token || token_does_not_exist && ( -

- That invite token is expired. Request a new one from the organization owner. -

- )} -
-
+ async function handleDeclineJoinOrganization() { + setIsSubmitting(true) + const response = await delete_( + `${API_URL}/organizations/${slug}/members/invite?invited_id=${invite_id}`, + {} ) + if (response.error) { + ui.setNotification({ + category: 'error', + message: `Failed to decline invitation: ${response.error.message}`, + }) + setIsSubmitting(false) + } else { + setIsSubmitting(false) + router.push('/') + } + } + + return ( +
+
+
+ + + Supabase + + Supabase + + + + +

+ Join {organization_name ? organization_name : 'a new organization'} +

+ {!token_does_not_exist ? ( +

+ You have been invited to join{' '} + {organization_name ? organization_name : 'a new organization'} organization at + Supabase. +

+ ) : ( +
+ )} +
+ + {authorized_user && !expired_token && email_match && ( +
+ + + +
+ )} + +
+ {token_does_not_exist + ? 'Invite token is invalid.' + : !email_match + ? 'Sign in to that account to accept this invitation.' + : expired_token + ? 'Invite token has expired, please request a new one from the organization owner.' + : ''} +
+
+
+ ) } -export default withAuth( observer( User ) ) \ No newline at end of file +export default withAuth(observer(User))