"use client"; import Stack from "@mui/material/Stack"; import Box from "@mui/material/Box"; import Card from "@mui/material/Card"; import CardContent from "@mui/material/CardContent"; import FormControl from "@mui/material/FormControl"; import Grid from "@mui/material/Grid"; import InputLabel from "@mui/material/InputLabel"; import MenuItem from "@mui/material/MenuItem"; import Select from "@mui/material/Select"; import TextField from "@mui/material/TextField"; import Typography from "@mui/material/Typography"; import { useTranslation } from "react-i18next"; import CardActions from "@mui/material/CardActions"; import RestartAlt from "@mui/icons-material/RestartAlt"; import Button from "@mui/material/Button"; import { Controller, useFormContext } from "react-hook-form"; import { CreateProjectInputs } from "@/app/api/projects/actions"; import { BuildingType, ContractType, FundingType, LocationType, ProjectCategory, ServiceType, WorkNature, } from "@/app/api/projects"; import { StaffResult } from "@/app/api/staff"; import { Contact, Customer, Subsidiary } from "@/app/api/customer"; import Link from "next/link"; import React, { useEffect, useMemo, useState } from "react"; import { fetchCustomer } from "@/app/api/customer/actions"; import { Checkbox, ListItemText } from "@mui/material"; import uniq from "lodash/uniq"; interface Props { isActive: boolean; projectCategories: ProjectCategory[]; teamLeads: StaffResult[]; allCustomers: Customer[]; allSubsidiaries: Subsidiary[]; serviceTypes: ServiceType[]; contractTypes: ContractType[]; fundingTypes: FundingType[]; locationTypes: LocationType[]; buildingTypes: BuildingType[]; workNatures: WorkNature[]; } const ProjectClientDetails: React.FC = ({ isActive, projectCategories, teamLeads, allCustomers, allSubsidiaries, serviceTypes, contractTypes, fundingTypes, locationTypes, buildingTypes, workNatures, }) => { const { t } = useTranslation(); const { register, formState: { errors }, watch, control, setValue, getValues, } = useFormContext(); const subsidiaryMap = useMemo<{ [id: Subsidiary["id"]]: Subsidiary; }>( () => allSubsidiaries.reduce((acc, sub) => ({ ...acc, [sub.id]: sub }), {}), [allSubsidiaries], ); const selectedCustomerId = watch("clientId"); const selectedCustomer = useMemo( () => allCustomers.find((c) => c.id === selectedCustomerId), [allCustomers, selectedCustomerId], ); const [customerContacts, setCustomerContacts] = useState([]); const [customerSubsidiaryIds, setCustomerSubsidiaryIds] = useState( [], ); const selectedCustomerContactId = watch("clientContactId"); const selectedCustomerContact = useMemo( () => customerContacts.find( (contact) => contact.id === selectedCustomerContactId, ), [customerContacts, selectedCustomerContactId], ); useEffect(() => { if (selectedCustomerId !== undefined) { fetchCustomer(selectedCustomerId).then(({ contacts, subsidiaryIds }) => { setCustomerContacts(contacts); setCustomerSubsidiaryIds(subsidiaryIds); }); } }, [selectedCustomerId]); // Automatically add the team lead to the allocated staff list const selectedTeamLeadId = watch("projectLeadId"); useEffect(() => { if (selectedTeamLeadId !== undefined) { const currentStaffIds = getValues("allocatedStaffIds"); const newList = uniq([...currentStaffIds, selectedTeamLeadId]); setValue("allocatedStaffIds", newList); } }, [getValues, selectedTeamLeadId, setValue]); const buildingTypeIdNameMap = buildingTypes.reduce<{ [id: number]: string }>( (acc, building) => ({ ...acc, [building.id]: building.name }), {}, ); const workNatureIdNameMap = workNatures.reduce<{ [id: number]: string }>( (acc, wn) => ({ ...acc, [wn.id]: wn.name }), {}, ); return ( {t("Project Details")} {t("Project Category")} ( )} /> {t("Team Lead")} ( )} /> {t("Service Type")} ( )} /> {t("Funding Type")} ( )} /> {t("Contract Type")} ( )} /> {t("Location")} ( )} /> {t("Building Types")} ( )} /> {t("Work Nature")} ( )} /> {t("Client Details")} {t("Client")} ( )} /> {customerContacts.length > 0 && ( <> {t("Client Lead")} { if ( !customerContacts.find( (contact) => contact.id === value, ) ) { return t("Please provide a valid contact"); } else return true; }, }} defaultValue={customerContacts[0].id} control={control} name="clientContactId" render={({ field }) => ( )} /> )} {customerSubsidiaryIds.length > 0 && ( {t("Client Subsidiary")} { if ( !customerSubsidiaryIds.find( (subsidiaryId) => subsidiaryId === value, ) ) { return t("Please choose a valid subsidiary"); } else return true; }, }} defaultValue={customerSubsidiaryIds[0]} control={control} name="clientSubsidiaryId" render={({ field }) => ( )} /> )} ); }; export default ProjectClientDetails;