import Button from "@/components/Low/Button"; import Checkbox from "@/components/Low/Checkbox"; import { Type, User } from "@/interfaces/user"; import { USER_TYPE_LABELS } from "@/resources/user"; import axios from "axios"; import clsx from "clsx"; import moment from "moment"; import { useEffect, useState } from "react"; import ReactDatePicker from "react-datepicker"; import { toast } from "react-toastify"; import { checkAccess, getTypesOfUser } from "@/utils/permissions"; import { PermissionType } from "@/interfaces/permissions"; import Input from "@/components/Low/Input"; import CountrySelect from "@/components/Low/CountrySelect"; import Select from "@/components/Low/Select"; import { EntityWithRoles } from "@/interfaces/entity"; import useEntitiesGroups from "@/hooks/useEntitiesGroups"; const USER_TYPE_PERMISSIONS: { [key in Type]: { perm: PermissionType | undefined; list: Type[] }; } = { student: { perm: "createCodeStudent", list: [], }, teacher: { perm: "createCodeTeacher", list: [], }, agent: { perm: "createCodeCountryManager", list: ["student", "teacher", "corporate", "mastercorporate"], }, corporate: { perm: "createCodeCorporate", list: ["student", "teacher"], }, mastercorporate: { perm: undefined, list: ["student", "teacher", "corporate"], }, admin: { perm: "createCodeAdmin", list: [ "student", "teacher", "agent", "corporate", "admin", "mastercorporate", ], }, developer: { perm: undefined, list: [ "student", "teacher", "agent", "corporate", "admin", "developer", "mastercorporate", ], }, }; interface Props { user: User; users: User[]; entities: EntityWithRoles[]; permissions: PermissionType[]; onFinish: () => void; } export default function UserCreator({ user, users, entities = [], permissions, onFinish, }: Props) { const [name, setName] = useState(); const [email, setEmail] = useState(); const [phone, setPhone] = useState(); const [passportID, setPassportID] = useState(); const [studentID, setStudentID] = useState(); const [country, setCountry] = useState(user?.demographicInformation?.country); const [group, setGroup] = useState(); const [password, setPassword] = useState(); const [confirmPassword, setConfirmPassword] = useState(); const [expiryDate, setExpiryDate] = useState( user?.subscriptionExpirationDate ? moment(user?.subscriptionExpirationDate).toDate() : null ); const [isExpiryDateEnabled, setIsExpiryDateEnabled] = useState(true); const [isLoading, setIsLoading] = useState(false); const [type, setType] = useState("student"); const [position, setPosition] = useState(); const [entity, setEntity] = useState((entities || [])[0]?.id || undefined); const { groups } = useEntitiesGroups(); useEffect(() => { if (!isExpiryDateEnabled) setExpiryDate(null); }, [isExpiryDateEnabled]); const createUser = () => { if (!name || name.trim().length === 0) return toast.error("Please enter a valid name!"); if (!email || email.trim().length === 0) return toast.error("Please enter a valid e-mail address!"); if (users.map((x) => x.email).includes(email.trim())) return toast.error("That e-mail is already in use!"); if (!password || password.trim().length < 6) return toast.error("Please enter a valid password!"); if (password !== confirmPassword) return toast.error("The passwords do not match!"); setIsLoading(true); const body = { name, email, password, groupID: group, entity, type, studentID: type === "student" ? studentID : undefined, expiryDate, demographicInformation: { passport_id: type === "student" ? passportID : undefined, phone, country, position, }, }; axios .post("/api/make_user", body) .then(() => { toast.success("That user has been created!"); onFinish(); setName(""); setEmail(""); setPhone(""); setPassportID(""); setStudentID(""); setCountry(user?.demographicInformation?.country); setGroup(null); setEntity((entities || [])[0]?.id || undefined); setExpiryDate( user?.subscriptionExpirationDate ? moment(user?.subscriptionExpirationDate).toDate() : null ); setIsExpiryDateEnabled(true); setType("student"); setPosition(undefined); }) .catch((error) => { const data = error?.response?.data; if (!!data?.message) return toast.error(data.message); toast.error("Something went wrong! Please try again later!"); }) .finally(() => setIsLoading(false)); }; return (
{type === "student" && ( <> )}
)}
setType(e.target.value as Type)} className="p-6 w-full min-w-[350px] min-h-[70px] flex justify-center text-sm font-normal rounded-full border focus:outline-none cursor-pointer bg-white" > {Object.keys(USER_TYPE_LABELS).reduce((acc, x) => { const { list, perm } = USER_TYPE_PERMISSIONS[x as Type]; if (checkAccess(user, getTypesOfUser(list), permissions, perm)) acc.push(x); return acc; }, [])} )}
{user && checkAccess(user, [ "developer", "admin", "corporate", "mastercorporate", ]) && ( <>
Enabled
{isExpiryDateEnabled && ( moment(date).isAfter(new Date()) && (user?.subscriptionExpirationDate ? moment(date).isBefore( user?.subscriptionExpirationDate ) : true) } dateFormat="dd/MM/yyyy" selected={expiryDate} onChange={(date) => setExpiryDate(date)} /> )} )}
); }