diff --git a/src/components/Project/Infrastructure/TaskModel.jsx b/src/components/Project/Infrastructure/TaskModel.jsx index e69a6434..d3f3cc28 100644 --- a/src/components/Project/Infrastructure/TaskModel.jsx +++ b/src/components/Project/Infrastructure/TaskModel.jsx @@ -40,24 +40,19 @@ const TaskModel = ({ const [selectedWorkArea, setSelectedWorkArea] = useState(null); const [selectedActivity, setSelectedActivity] = useState(null); const [isSubmitting, setIsSubmitting] = useState(false); + const [activityData, setActivityData] = useState([]); const { register, handleSubmit, formState: { errors }, reset, + setValue, } = useForm({ resolver: zodResolver(taskSchema), defaultValues: defaultModel, }); - useEffect(() => { - if (clearTrigger) { - resetForm(); - onClearComplete(); - } - }, [clearTrigger, onClearComplete]); - useEffect(() => { dispatch(changeMaster("Activity")); resetForm(); @@ -104,7 +99,7 @@ const TaskModel = ({ const handleActivityChange = (e) => { const { value } = e.target; - const activity = activities.find((b) => b.id === Number(value)); + const activity = activityData.find((b) => b.id === Number(value)); setSelectedActivity(activity); reset((prev) => ({ ...prev, @@ -115,14 +110,9 @@ const TaskModel = ({ const onSubmitForm = async (data) => { setIsSubmitting(true); await onSubmit(data); - reset({ - buildingID: data.buildingID, - floorId: data.floorId, - workAreaId: data.workAreaId, - activityID: data.activityID, - plannedWork: 0, - completedWork: 0, - }); + setValue("plannedWork", 0); + setValue("completedWork", 0); + dispatch(changeMaster("Activity")); setIsSubmitting(false); }; @@ -135,6 +125,11 @@ const TaskModel = ({ reset(defaultModel); }; + useEffect(() => { + if (activities && activities.length > 0) { + setActivityData(activities); + } + }, [activities]); return (
@@ -265,18 +260,25 @@ const TaskModel = ({ onChange={handleActivityChange} > - {activities - ?.slice() - ?.sort((a, b) => { - const nameA = a?.activityName || ""; - const nameB = b?.activityName || ""; - return nameA.localeCompare(nameB); - }) - ?.map((activity) => ( - - ))} + {activityData && activityData.length > 0 ? ( + activityData + ?.slice() + ?.sort((a, b) => { + const nameA = a?.activityName || ""; + const nameB = b?.activityName || ""; + return nameA.localeCompare(nameB); + }) + ?.map((activity) => ( + + )) + ) : ( + // Fallback if activities are empty + )} {errors.activityID && (