/* eslint-disable react-hooks/rules-of-hooks */ import { useRef, useState } from 'react'; import { Button, FormControl, FormHelperText, FormLabel, HStack, Input, InputGroup, Modal, ModalBody, ModalCloseButton, ModalContent, ModalFooter, ModalHeader, ModalOverlay, NumberDecrementStepper, NumberIncrementStepper, NumberInput, NumberInputField, NumberInputStepper, VStack, useClipboard, useColorModeValue, useToast } from '@chakra-ui/react'; import { AsyncSelect } from 'chakra-react-select'; import { Field, Form, Formik } from 'formik'; import { useAuthContext } from '@/contexts/AuthContext'; export default function InvitePlatformModal({ isOpen, onOpen, onClose, onCreate }: { isOpen: boolean; onOpen: () => void; onClose: () => void; onCreate: (location: any) => void; }) { const toast = useToast(); const { currentUser, user } = useAuthContext(); const [inviteCode, setInviteCode] = useState(null); const senderRef = useRef(null); const { setValue: setClipboardValue, onCopy: onClipboardCopy, hasCopied: clipboardHasCopied } = useClipboard(''); const onModalClose = () => { onClose(); setInviteCode(null); }; const copyInviteLink = () => { onClipboardCopy(); toast({ title: 'Copied invite link to clipboard!', status: 'success', duration: 5000, isClosable: true }); }; const getUserSearchResults = async (inputValue: string, callback: any) => { if (!inputValue) { callback([]); return; } await user.getIdToken().then((token: any) => { fetch(`/api/v1/admin/search-users/${encodeURIComponent(inputValue)}`, { method: 'GET', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` } }) .then((res) => { if (res.status === 200) { return res.json(); } else { return res.json().then((json) => { throw new Error(json.message); }); } }) .then((data) => { callback( data.map((user: any) => ({ label: `${user.displayName} (${user.username})`, value: user.id })) ); }) .catch((error) => { toast({ title: 'There was an error searching for users.', description: error.message, status: 'error', duration: 5000, isClosable: true }); }); }); }; return ( <> { user.getIdToken().then((token: any) => { fetch(`/api/v1/admin/invite-link`, { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` }, body: JSON.stringify({ maxUses: values.maxUses, code: values.code, senderId: values.senderId?.value, referrals: values.referrals || 0, comment: values.comment }) }) .then((res) => { if (res.status === 200) { return res.json(); } else { return res.json().then((json) => { throw new Error(json.message); }); } }) .then((data) => { toast({ title: data.message, status: 'success', duration: 5000, isClosable: true }); setInviteCode(data.code); setClipboardValue(`${process.env.NEXT_PUBLIC_ROOT_URL}/invitation/${data.code}`); actions.resetForm(); onCreate(data.code); // onClose(); }) .catch((error) => { toast({ title: 'There was an error creating the invitation.', description: error.message, status: 'error', duration: 5000, isClosable: true }); }) .finally(() => { actions.setSubmitting(false); }); }); }} > {(props) => ( { props.setFieldValue('code', ''); props.setFieldValue('senderId', null); }} >
Create Invitation Link {!inviteCode ? ( <> {({ field, form }: any) => ( Custom Invite Code )} {({ field, form }: any) => ( Maximum Uses { form.setFieldValue('maxUses', value); }} > )} {({ field, form }: any) => ( Sender 'No search results found.'} loadOptions={(inputValue, callback) => { getUserSearchResults(inputValue, callback); }} onChange={(value) => { form.setFieldValue('senderId', value); }} value={field.value || []} selectedOptionStyle='check' /> If left blank, you will be the sender. )} {({ field, form }: any) => ( Starting Referrals { form.setFieldValue('referrals', value); }} > If you want to give the user referrals to invite others, enter the number of referrals you want to give them here. )} {({ field, form }: any) => ( Comment All invitation links expire after 14 days. )} ) : ( <> Invitation Link e.target.select()} /> Share this link with the people you want to invite to the platform. )} {!inviteCode ? ( ) : ( )}
)}
); }