|
|
@@ -1,6 +1,6 @@ |
|
|
|
"use client"; |
|
|
|
import { useRouter, useSearchParams } from "next/navigation"; |
|
|
|
import { useCallback, useEffect, useMemo, useState } from "react"; |
|
|
|
import React, { useCallback, useEffect, useMemo, useState } from "react"; |
|
|
|
import SearchResults, { Column } from "../SearchResults"; |
|
|
|
// import { TeamResult } from "@/app/api/team"; |
|
|
|
import { useTranslation } from "react-i18next"; |
|
|
@@ -13,40 +13,27 @@ import { |
|
|
|
useForm, |
|
|
|
useFormContext, |
|
|
|
} from "react-hook-form"; |
|
|
|
import { Check, Close, Error } from "@mui/icons-material"; |
|
|
|
import { Check, Close, Error, RestartAlt } from "@mui/icons-material"; |
|
|
|
import TeamInfo from "../EditTeam/TeamInfo"; |
|
|
|
import Allocation from "./Allocation"; |
|
|
|
import { StaffResult } from "@/app/api/staff"; |
|
|
|
|
|
|
|
interface desc { |
|
|
|
id: number; |
|
|
|
name: string; |
|
|
|
description: string; |
|
|
|
teamLead: number; |
|
|
|
} |
|
|
|
import { IndivTeam } from "@/app/api/team"; |
|
|
|
|
|
|
|
interface Props { |
|
|
|
staff: StaffResult[]; |
|
|
|
desc: desc[]; |
|
|
|
// teamLead: StaffResult[] |
|
|
|
teamInfo: IndivTeam; |
|
|
|
} |
|
|
|
|
|
|
|
const EditTeam: React.FC<Props> = async ({ staff, desc }) => { |
|
|
|
// console.log(desc) |
|
|
|
const EditTeam: React.FC<Props> = async ({ staff, teamInfo }) => { |
|
|
|
const { team, staffIds } = teamInfo; |
|
|
|
const { t } = useTranslation(); |
|
|
|
const formProps = useForm<CreateTeamInputs>(); |
|
|
|
const searchParams = useSearchParams(); |
|
|
|
const idString = searchParams.get("id"); |
|
|
|
const [filteredItems, setFilteredItems] = useState<StaffResult[]>(); |
|
|
|
const [allStaffs, setAllStaffs] = useState<StaffResult[]>(); |
|
|
|
const [filteredDesc, setFilteredDesc] = useState<string>(); |
|
|
|
const [filteredName, setFilteredName] = useState<string>(); |
|
|
|
const [teamLead, setTeamLead] = useState<number>(); |
|
|
|
const [tabIndex, setTabIndex] = useState(0); |
|
|
|
const router = useRouter(); |
|
|
|
// const [selectedStaff, setSelectedStaff] = useState<typeof filteredItems>( |
|
|
|
// initialStaffs.filter((s) => getValues("addStaffIds")?.includes(s.id)) |
|
|
|
// ); |
|
|
|
|
|
|
|
const errors = formProps.formState.errors; |
|
|
|
|
|
|
@@ -58,64 +45,36 @@ const EditTeam: React.FC<Props> = async ({ staff, desc }) => { |
|
|
|
); |
|
|
|
|
|
|
|
const [serverError, setServerError] = useState(""); |
|
|
|
const cols = useMemo<Column<StaffResult>[]>( |
|
|
|
() => [ |
|
|
|
{ label: t("Staff Id"), name: "staffId" }, |
|
|
|
{ label: t("Name"), name: "staffName" }, |
|
|
|
// { label: t("Current Position"), name: "posCode" }, |
|
|
|
], |
|
|
|
[t] |
|
|
|
); |
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
let idList: number[] = [] |
|
|
|
let idList: number[] = []; |
|
|
|
// console.log(desc) |
|
|
|
if (idString) { |
|
|
|
const filteredTeam = staff.filter( |
|
|
|
(item) => { |
|
|
|
return (item.teamId === parseInt(idString))} |
|
|
|
); |
|
|
|
// console.log(filteredTeam) |
|
|
|
const tempDesc = desc.filter( |
|
|
|
(item) => item.id === parseInt(idString) |
|
|
|
) |
|
|
|
// const leader = teamLead.filter( |
|
|
|
// (staff) => staff.teamId === parseInt(idString) |
|
|
|
// ) |
|
|
|
// console.log(leader) |
|
|
|
console.log(tempDesc[0].teamLead) |
|
|
|
setTeamLead(tempDesc[0].teamLead) |
|
|
|
const filteredTeam = staff.filter((item) => { |
|
|
|
return item.teamId === parseInt(idString); |
|
|
|
}); |
|
|
|
console.log(team.teamLead); |
|
|
|
setTeamLead(team.teamLead); |
|
|
|
if (filteredTeam.length > 0) { |
|
|
|
const filteredIds: number[] = filteredTeam.map((i) => ( |
|
|
|
i.id |
|
|
|
)) |
|
|
|
|
|
|
|
// const teamLead = tempDesc[0].teamLead |
|
|
|
// const index = filteredIds.indexOf(teamLead); |
|
|
|
|
|
|
|
// if (index !== -1) { |
|
|
|
// filteredIds.splice(index, 1); |
|
|
|
// filteredIds.unshift(teamLead); |
|
|
|
// } |
|
|
|
const filteredIds: number[] = filteredTeam.map((i) => i.id); |
|
|
|
|
|
|
|
// const teamLead = tempDesc[0].teamLead |
|
|
|
// const index = filteredIds.indexOf(teamLead); |
|
|
|
|
|
|
|
// if (index !== -1) { |
|
|
|
// filteredIds.splice(index, 1); |
|
|
|
// filteredIds.unshift(teamLead); |
|
|
|
// } |
|
|
|
|
|
|
|
idList = filteredIds |
|
|
|
// console.log(filteredIds) |
|
|
|
idList = filteredIds; |
|
|
|
} |
|
|
|
// console.log(idList) |
|
|
|
setFilteredItems(filteredTeam); |
|
|
|
formProps.reset({description: tempDesc[0].description, addStaffIds: idList}) |
|
|
|
setFilteredDesc(tempDesc[0].description) |
|
|
|
setFilteredName(tempDesc[0].name) |
|
|
|
formProps.reset({ description: team.description, addStaffIds: idList }); |
|
|
|
} |
|
|
|
// console.log(staff) |
|
|
|
|
|
|
|
setAllStaffs(staff) |
|
|
|
|
|
|
|
setAllStaffs(staff); |
|
|
|
}, [searchParams]); |
|
|
|
|
|
|
|
// useEffect(() => { |
|
|
|
// console.log(allStaffs) |
|
|
|
// }, [allStaffs]); |
|
|
|
|
|
|
|
const hasErrorsInTab = ( |
|
|
|
tabIndex: number, |
|
|
|
errors: FieldErrors<CreateTeamInputs> |
|
|
@@ -133,14 +92,14 @@ const EditTeam: React.FC<Props> = async ({ staff, desc }) => { |
|
|
|
try { |
|
|
|
// console.log(data); |
|
|
|
const tempData = { |
|
|
|
description: data.description, |
|
|
|
addStaffIds: data.addStaffIds, |
|
|
|
deleteStaffIds: data.deleteStaffIds, |
|
|
|
id: parseInt(idString!!) |
|
|
|
} |
|
|
|
console.log(tempData) |
|
|
|
await saveTeam(tempData); |
|
|
|
router.replace("/settings/team"); |
|
|
|
description: data.description, |
|
|
|
addStaffIds: data.addStaffIds, |
|
|
|
deleteStaffIds: data.deleteStaffIds, |
|
|
|
id: parseInt(idString!!), |
|
|
|
}; |
|
|
|
console.log(tempData); |
|
|
|
// await saveTeam(tempData); |
|
|
|
// router.replace("/settings/team"); |
|
|
|
} catch (e) { |
|
|
|
console.log(e); |
|
|
|
setServerError(t("An error has occurred. Please try again later.")); |
|
|
@@ -149,6 +108,20 @@ const EditTeam: React.FC<Props> = async ({ staff, desc }) => { |
|
|
|
[router] |
|
|
|
); |
|
|
|
|
|
|
|
const resetTeam = React.useCallback(() => { |
|
|
|
formProps.reset({ |
|
|
|
name: team.name, |
|
|
|
code: team.code, |
|
|
|
description: team.description, |
|
|
|
addStaffIds: staffIds, |
|
|
|
deleteStaffIds: [] |
|
|
|
}); |
|
|
|
}, []); |
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
resetTeam(); |
|
|
|
}, [team]); |
|
|
|
|
|
|
|
return ( |
|
|
|
<> |
|
|
|
{serverError && ( |
|
|
@@ -157,41 +130,50 @@ const EditTeam: React.FC<Props> = async ({ staff, desc }) => { |
|
|
|
</Typography> |
|
|
|
)} |
|
|
|
<FormProvider {...formProps}> |
|
|
|
<Stack |
|
|
|
<Stack |
|
|
|
spacing={2} |
|
|
|
component="form" |
|
|
|
onSubmit={formProps.handleSubmit(onSubmit)} |
|
|
|
> |
|
|
|
|
|
|
|
<Typography variant="h4" marginInlineEnd={2}> |
|
|
|
{t("Edit Team")} - {filteredName} |
|
|
|
</Typography> |
|
|
|
<Stack |
|
|
|
direction="row" |
|
|
|
justifyContent="space-between" |
|
|
|
flexWrap="wrap" |
|
|
|
rowGap={2} |
|
|
|
> |
|
|
|
<Tabs |
|
|
|
value={tabIndex} |
|
|
|
onChange={handleTabChange} |
|
|
|
variant="scrollable" |
|
|
|
<Typography variant="h4" marginInlineEnd={2}> |
|
|
|
{t("Edit Team")} |
|
|
|
{/* - {team.name} */} |
|
|
|
</Typography> |
|
|
|
<Stack |
|
|
|
direction="row" |
|
|
|
justifyContent="space-between" |
|
|
|
flexWrap="wrap" |
|
|
|
rowGap={2} |
|
|
|
> |
|
|
|
<Tab |
|
|
|
label={t("Team Info")} |
|
|
|
icon={ |
|
|
|
hasErrorsInTab(0, errors) ? ( |
|
|
|
<Error sx={{ marginInlineEnd: 1 }} color="error" /> |
|
|
|
) : undefined |
|
|
|
} |
|
|
|
iconPosition="end" |
|
|
|
/> |
|
|
|
<Tab label={t("Staff Allocation")} iconPosition="end" /> |
|
|
|
</Tabs> |
|
|
|
</Stack> |
|
|
|
{tabIndex === 0 && <TeamInfo value={filteredDesc!!} />} |
|
|
|
{tabIndex === 1 && <Allocation allStaffs={allStaffs!!} teamLead={teamLead!!}/>} |
|
|
|
<Stack direction="row" justifyContent="flex-end" gap={1}> |
|
|
|
<Tabs |
|
|
|
value={tabIndex} |
|
|
|
onChange={handleTabChange} |
|
|
|
variant="scrollable" |
|
|
|
> |
|
|
|
<Tab |
|
|
|
label={t("Team Info")} |
|
|
|
icon={ |
|
|
|
hasErrorsInTab(0, errors) ? ( |
|
|
|
<Error sx={{ marginInlineEnd: 1 }} color="error" /> |
|
|
|
) : undefined |
|
|
|
} |
|
|
|
iconPosition="end" |
|
|
|
/> |
|
|
|
<Tab label={t("Staff Allocation")} iconPosition="end" /> |
|
|
|
</Tabs> |
|
|
|
</Stack> |
|
|
|
{tabIndex === 0 && <TeamInfo />} |
|
|
|
{tabIndex === 1 && ( |
|
|
|
<Allocation allStaffs={allStaffs!!} teamLead={teamLead!!} /> |
|
|
|
)} |
|
|
|
<Stack direction="row" justifyContent="flex-end" gap={1}> |
|
|
|
<Button |
|
|
|
variant="text" |
|
|
|
startIcon={<RestartAlt />} |
|
|
|
onClick={resetTeam} |
|
|
|
> |
|
|
|
{t("Reset")} |
|
|
|
</Button> |
|
|
|
<Button |
|
|
|
variant="outlined" |
|
|
|
startIcon={<Close />} |
|
|
@@ -208,7 +190,7 @@ const EditTeam: React.FC<Props> = async ({ staff, desc }) => { |
|
|
|
{t("Confirm")} |
|
|
|
</Button> |
|
|
|
</Stack> |
|
|
|
</Stack> |
|
|
|
</Stack> |
|
|
|
</FormProvider> |
|
|
|
</> |
|
|
|
); |
|
|
|