|
| 1 | +import React, { useState, useEffect } from 'react'; |
| 2 | +import { useMutation } from '@apollo/client'; |
| 3 | +import { toast } from 'react-toastify'; |
| 4 | +import { IoIosCloseCircleOutline, IoIosArrowDown, IoIosArrowUp } from 'react-icons/io'; |
| 5 | +import { BsExclamationCircleFill } from "react-icons/bs"; |
| 6 | +import {SEND_INVITATION} from '../Mutations/invitationMutation' |
| 7 | +import ButtonLoading from './ButtonLoading'; |
| 8 | +import validateEmail from '../utils/emailValidation'; |
| 9 | + |
| 10 | +const roles: ('trainee' | 'admin' | 'ttl' | 'coordinator')[] = ['trainee', 'admin', 'ttl', 'coordinator']; |
| 11 | +interface InviteFormProps { |
| 12 | + onClose: () => void; |
| 13 | +} |
| 14 | +function InviteForm({ onClose }: InviteFormProps) { |
| 15 | + const [email, setEmail] = useState<string>(''); |
| 16 | + const [role, setRole] = useState<string>('Role'); |
| 17 | + const [orgToken, setOrgToken] = useState<string>(''); |
| 18 | + const [isDropdownOpen, setIsDropdownOpen] = useState<boolean>(false); |
| 19 | + const [emailError, setEmailError] = useState<string>(''); |
| 20 | + const [sendInvitation, { loading, error }] = useMutation(SEND_INVITATION); |
| 21 | + |
| 22 | + const organisationToken = localStorage.getItem('orgToken'); |
| 23 | + |
| 24 | + useEffect(() => { |
| 25 | + if (organisationToken) { |
| 26 | + setOrgToken(organisationToken); |
| 27 | + } |
| 28 | + }, [organisationToken]); |
| 29 | + |
| 30 | + const handleSubmit = async (e: React.FormEvent) => { |
| 31 | + e.preventDefault(); |
| 32 | + |
| 33 | + if (!validateEmail(email)) { |
| 34 | + setEmailError('Please enter a valid email address.'); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + setEmailError(''); |
| 39 | + |
| 40 | + try { |
| 41 | + await sendInvitation({ |
| 42 | + variables: { |
| 43 | + invitees: [{ email, role }], |
| 44 | + orgToken, |
| 45 | + }, |
| 46 | + }); |
| 47 | + toast.success('Invitation sent successfully!'); |
| 48 | + setEmail(''); |
| 49 | + setRole('Role'); |
| 50 | + setOrgToken(''); |
| 51 | + } catch (e: any) { |
| 52 | + toast.error(`Error sending invitation: ${e.message}`); |
| 53 | + } |
| 54 | + }; |
| 55 | + |
| 56 | + const toggleDropdown = () => { |
| 57 | + setIsDropdownOpen(!isDropdownOpen); |
| 58 | + }; |
| 59 | + |
| 60 | + const handleRoleSelect = (selectedRole: string) => { |
| 61 | + setRole(selectedRole); |
| 62 | + setIsDropdownOpen(true); |
| 63 | + }; |
| 64 | + |
| 65 | + const handleEmailChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 66 | + setEmail(e.target.value); |
| 67 | + setEmailError(''); |
| 68 | + }; |
| 69 | + |
| 70 | + return ( |
| 71 | + <div className="relative bg-white dark:bg-[#020917] p-6 rounded-lg shadow-lg max-w-sm w-full"> |
| 72 | + <h2 className="text-xl font-bold mb-4">Invite users</h2> |
| 73 | + <button |
| 74 | + type='button' |
| 75 | + className="absolute top-2 right-2 text-black dark:text-white hover:text-gray-800" |
| 76 | + onClick={onClose} |
| 77 | + aria-label="Toggle role dropdown" |
| 78 | + > |
| 79 | + <IoIosCloseCircleOutline size={24} /> |
| 80 | + </button> |
| 81 | + <form onSubmit={handleSubmit} className=" pb-12 sm:pb-16"> |
| 82 | + <div className="text-black dark:text-white py-2 mb-2"> |
| 83 | + Anyone you invite will be able to access the dashboard of this application. |
| 84 | + </div> |
| 85 | + <input |
| 86 | + value={email} |
| 87 | + type='emai' |
| 88 | + onChange={handleEmailChange} |
| 89 | + placeholder="Email address" |
| 90 | + className="border border-[#d9d0fb] px-1 py-1 text-sm dark:bg-[#020917] rounded-md w-full mb-1 focus:outline-none focus:border-[#d9d0fb]" |
| 91 | + /> |
| 92 | + {emailError && ( |
| 93 | + <p className="text-red-600 text-sm mb-4">{emailError}</p> |
| 94 | + )} |
| 95 | + <div> |
| 96 | + <label className="pb-2"> |
| 97 | + <div className='flex justify-between pt-2 mb-8'> |
| 98 | + <div className="relative w-[50%]"> |
| 99 | + <button |
| 100 | + type="button" |
| 101 | + aria-label="Toggle role dropdown" |
| 102 | + onClick={toggleDropdown} |
| 103 | + className="border border-[#d9d0fb] dark:bg-[#020917] py-1 rounded-md w-full focus:outline-none focus:border-[#d9d0fb] bg-white px-2 text-sm flex justify-between items-center" |
| 104 | + > |
| 105 | + {role} |
| 106 | + {isDropdownOpen ? ( |
| 107 | + <IoIosArrowUp size={20} /> |
| 108 | + ) : ( |
| 109 | + <IoIosArrowDown size={20} /> |
| 110 | + )} |
| 111 | + </button> |
| 112 | + {isDropdownOpen && ( |
| 113 | + <div className="absolute mt-1 w-full bg-[#f3f0fe] dark:bg-[#04122F] border border-[#d9d0fb] dark:border-[#020917] rounded-md shadow-lg z-10"> |
| 114 | + {roles.map((roleOption) => ( |
| 115 | + <div |
| 116 | + key={roleOption} |
| 117 | + onClick={() => handleRoleSelect(roleOption)} |
| 118 | + className="px-4 hover:bg-[#8667f2] cursor-pointer" |
| 119 | + > |
| 120 | + {roleOption.charAt(0).toUpperCase() + roleOption.slice(1)} |
| 121 | + </div> |
| 122 | + ))} |
| 123 | + </div> |
| 124 | + )} |
| 125 | + </div> |
| 126 | + <div> |
| 127 | + <button |
| 128 | + type="submit" |
| 129 | + disabled={loading} |
| 130 | + className="ml-4 py-1 flex justify-center bg-[#7a5edc] text-white px-4 rounded" |
| 131 | + > |
| 132 | + {loading ? <ButtonLoading |
| 133 | + style="rounded-md inline-block w-full sm:px-4 py-1 sm:py-0 opacity-50" |
| 134 | + /> : 'Invite'} |
| 135 | + </button> |
| 136 | + </div> |
| 137 | + </div> |
| 138 | + </label> |
| 139 | + </div> |
| 140 | + </form> |
| 141 | + <div className="border-t-[1px] border-[#d9d0fb] m-0" /> |
| 142 | + <div className="flex justify-between pt-2"> |
| 143 | + <p className='flex justify-center items-center text-gray-400 gap-1'><BsExclamationCircleFill />Learn more</p> |
| 144 | + <button |
| 145 | + type="button" |
| 146 | + aria-label="Toggle role dropdown" |
| 147 | + disabled={loading} |
| 148 | + className="flex justify-center bg-[#7a5edc] text-white px-4 py-1 rounded" |
| 149 | + > |
| 150 | + Upload users file |
| 151 | + </button> |
| 152 | + </div> |
| 153 | + </div> |
| 154 | + ); |
| 155 | +}; |
| 156 | + |
| 157 | +export default InviteForm; |
0 commit comments