import Button from "@/components/Low/Button"; import Input from "@/components/Low/Input"; import {Grading, Step} from "@/interfaces"; import {User} from "@/interfaces/user"; import {CEFR_STEPS, GENERAL_STEPS, IELTS_STEPS, TOFEL_STEPS} from "@/resources/grading"; import axios from "axios"; import {useEffect, useState} from "react"; import {BsPlusCircle, BsTrash} from "react-icons/bs"; import {toast} from "react-toastify"; const areStepsOverlapped = (steps: Step[]) => { for (let i = 0; i < steps.length; i++) { if (i === 0) continue; const step = steps[i]; const previous = steps[i - 1]; if (previous.max >= step.min) return true; } return false; }; export default function CorporateGradingSystem({user, defaultSteps, mutate}: {user: User; defaultSteps: Step[]; mutate: (steps: Step[]) => void}) { const [isLoading, setIsLoading] = useState(false); const [steps, setSteps] = useState(defaultSteps || []); const saveGradingSystem = () => { if (!steps.every((x) => x.min < x.max)) return toast.error("One of your steps has a minimum threshold inferior to its superior threshold."); if (areStepsOverlapped(steps)) return toast.error("There seems to be an overlap in one of your steps."); if ( steps.reduce((acc, curr) => { return acc - (curr.max - curr.min + 1); }, 100) > 0 ) return toast.error("There seems to be an open interval in your steps."); setIsLoading(true); axios .post("/api/grading", {user: user.id, steps}) .then(() => toast.success("Your grading system has been saved!")) .then(() => mutate(steps)) .catch(() => toast.error("Something went wrong, please try again later")) .finally(() => setIsLoading(false)); }; return (
{steps.map((step, index) => ( <>
setSteps((prev) => prev.map((x, i) => (i === index ? {...x, min: parseInt(e)} : x)))} name="min" /> setSteps((prev) => prev.map((x, i) => (i === index ? {...x, label: e} : x)))} name="min" /> setSteps((prev) => prev.map((x, i) => (i === index ? {...x, max: parseInt(e)} : x)))} name="max" />
{index !== 0 && index !== steps.length - 1 && ( )}
{index < steps.length - 1 && ( )} ))}
); }