import { useOrg } from '@components/Contexts/OrgContext' import PageLoading from '@components/Objects/Loaders/PageLoading'; import ConfirmationModal from '@components/StyledElements/ConfirmationModal/ConfirmationModal'; import { getAPIUrl, getUriWithOrg } from '@services/config/config'; import { swrFetcher } from '@services/utils/ts/requests'; import { Globe, Shield, X } from 'lucide-react' import Link from 'next/link'; import React, { use, useEffect } from 'react' import useSWR, { mutate } from 'swr'; import dayjs from 'dayjs'; import { changeSignupMechanism, createInviteCode, deleteInviteCode } from '@services/organizations/invites'; import Toast from '@components/StyledElements/Toast/Toast'; import toast from 'react-hot-toast'; import { useRouter } from 'next/navigation'; function OrgAccess() { const org = useOrg() as any; const { data: invites } = useSWR(org ? `${getAPIUrl()}orgs/${org?.id}/invites` : null, swrFetcher); const [isLoading, setIsLoading] = React.useState(false) const [joinMethod, setJoinMethod] = React.useState('closed') const router = useRouter() async function getOrgJoinMethod() { if (org) { if (org.config.config.GeneralConfig.users.signup_mechanism == 'open') { setJoinMethod('open') } else { setJoinMethod('inviteOnly') } } } async function createInvite() { let res = await createInviteCode(org.id) if (res.status == 200) { mutate(`${getAPIUrl()}orgs/${org.id}/invites`) } else { toast.error('Error ' + res.status + ': ' + res.data.detail) } } async function deleteInvite(invite: any) { let res = await deleteInviteCode(org.id, invite.invite_code_uuid) if (res.status == 200) { mutate(`${getAPIUrl()}orgs/${org.id}/invites`) } else { toast.error('Error ' + res.status + ': ' + res.data.detail) } } async function changeJoinMethod(method: 'open' | 'inviteOnly') { let res = await changeSignupMechanism(org.id, method) if (res.status == 200) { router.refresh() mutate(`${getAPIUrl()}orgs/slug/${org?.slug}`) } else { toast.error('Error ' + res.status + ': ' + res.data.detail) } } useEffect(() => { if (invites && org) { getOrgJoinMethod() setIsLoading(false) } } , [org, invites]) return ( <> {!isLoading ? (<>

Join method

Choose how users can join your organization

{joinMethod == 'open' ?
Active
: null}
Open
Users can join freely from the signup page
} functionToExecute={() => { changeJoinMethod('open') }} status='info' > {joinMethod == 'inviteOnly' ?
Active
: null}
Closed
Users can join only by invitation
} functionToExecute={() => { changeJoinMethod('inviteOnly') }} status='info' >

Invite codes

Invite codes can be copied and used to join your organization

<> {invites?.map((invite: any) => ( ))}
Code Signup link Expiration date Actions
{invite.invite_code} {getUriWithOrg(org?.slug, `/signup?inviteCode=${invite.invite_code}`)} {dayjs(invite.expiration_date).add(1, 'year').format('DD/MM/YYYY')} Delete code } functionToExecute={() => { deleteInvite(invite) }} status='warning' >
) : } ) } export default OrgAccess