"use client"; import Check from "@mui/icons-material/Check"; import Close from "@mui/icons-material/Close"; import Button from "@mui/material/Button"; import Stack from "@mui/material/Stack"; import Tab from "@mui/material/Tab"; import Tabs, { TabsProps } from "@mui/material/Tabs"; import { useRouter } from "next/navigation"; import React, { useCallback, useState } from "react"; import { useTranslation } from "react-i18next"; import ProjectClientDetails from "./ProjectClientDetails"; import TaskSetup from "./TaskSetup"; import StaffAllocation from "./StaffAllocation"; import Milestone from "./Milestone"; import { Task, TaskTemplate } from "@/app/api/tasks"; import { FieldErrors, FormProvider, SubmitErrorHandler, SubmitHandler, useForm, } from "react-hook-form"; import { CreateProjectInputs, saveProject } from "@/app/api/projects/actions"; import { Error } from "@mui/icons-material"; import { BuildingType, ContractType, FundingType, LocationType, ProjectCategory, ServiceType, WorkNature, } from "@/app/api/projects"; import { StaffResult } from "@/app/api/staff"; import { Typography } from "@mui/material"; import { Grade } from "@/app/api/grades"; import { Customer } from "@/app/api/customer"; export interface Props { allTasks: Task[]; projectCategories: ProjectCategory[]; taskTemplates: TaskTemplate[]; teamLeads: StaffResult[]; allCustomers: Customer[]; fundingTypes: FundingType[]; serviceTypes: ServiceType[]; contractTypes: ContractType[]; locationTypes: LocationType[]; buildingTypes: BuildingType[]; workNatures: WorkNature[]; allStaffs: StaffResult[]; // Mocked grades: Grade[]; } const hasErrorsInTab = ( tabIndex: number, errors: FieldErrors, ) => { switch (tabIndex) { case 0: return ( errors.projectName || errors.projectCode || errors.projectDescription ); default: false; } }; const CreateProject: React.FC = ({ allTasks, projectCategories, taskTemplates, teamLeads, grades, allCustomers, contractTypes, fundingTypes, locationTypes, serviceTypes, buildingTypes, workNatures, allStaffs, }) => { const [serverError, setServerError] = useState(""); const [tabIndex, setTabIndex] = useState(0); const { t } = useTranslation(); const router = useRouter(); const handleCancel = () => { router.back(); }; const handleTabChange = useCallback>( (_e, newValue) => { setTabIndex(newValue); }, [], ); const onSubmit = useCallback>( async (data) => { try { setServerError(""); await saveProject(data); router.replace("/projects"); } catch (e) { setServerError(t("An error has occurred. Please try again later.")); } }, [router, t], ); const onSubmitError = useCallback>( (errors) => { // Set the tab so that the focus will go there if ( errors.projectName || errors.projectDescription || errors.projectCode ) { setTabIndex(0); } }, [], ); const formProps = useForm({ defaultValues: { taskGroups: {}, allocatedStaffIds: [], milestones: {}, totalManhour: 0, manhourPercentageByGrade: grades.reduce((acc, grade) => { return { ...acc, [grade.id]: 1 / grades.length }; }, {}), }, }); const errors = formProps.formState.errors; return ( ) : undefined } iconPosition="end" /> { } { } { } {} {serverError && ( {serverError} )} ); }; export default CreateProject;