diff --git a/src/components/Project/Infrastructure/FloorModel.jsx b/src/components/Project/Infrastructure/FloorModel.jsx index fb71d29e..c9ae9d43 100644 --- a/src/components/Project/Infrastructure/FloorModel.jsx +++ b/src/components/Project/Infrastructure/FloorModel.jsx @@ -144,31 +144,42 @@ const FloorModel = ({ project, onClose, onSubmit }) => { name="id" control={control} rules={{ required: "Floor is required" }} - render={({ field }) => ( - f.floorName) - .sort((a, b) => a.floorName.localeCompare(b.floorName)) - .map((f) => ({ id: f.id, name: f.floorName })) ?? []), - ]} - value={field.value || ""} - onChange={(value) => { - field.onChange(value); - handleFloorChange?.(value); - }} - required - noOptionsMessage={() => - !selectedBuilding?.floors || selectedBuilding.floors.length === 0 - ? "No floors found" - : null - } - className="m-0 form-select-sm w-100" - /> - )} + render={({ field }) => { + // Prepare options + const floorOptions = [ + { id: "0", name: "Add New Floor" }, + ...(selectedBuilding?.floors + ?.filter((f) => f.floorName) + .sort((a, b) => a.floorName.localeCompare(b.floorName)) + .map((f) => ({ id: f.id, name: f.floorName })) ?? []), + ]; + + return ( + { + field.onChange(val); // update react-hook-form + + if (val === "0") { + setValue("floorName", ""); // clear for new floor + } else { + const floor = selectedBuilding?.floors?.find(f => f.id === val); + setValue("floorName", floor?.floorName || ""); + } + }} + required + noOptionsMessage={() => + !selectedBuilding?.floors || selectedBuilding.floors.length === 0 + ? "No floors found" + : null + } + className="m-0 form-select-sm w-100" + /> + ); + }} /> {errors.id && ( diff --git a/src/components/Project/Infrastructure/TaskModel.jsx b/src/components/Project/Infrastructure/TaskModel.jsx index 6e4e7546..257dda24 100644 --- a/src/components/Project/Infrastructure/TaskModel.jsx +++ b/src/components/Project/Infrastructure/TaskModel.jsx @@ -8,10 +8,12 @@ import { useGroups, useWorkCategoriesMaster, } from "../../../hooks/masterHook/useMaster"; -import { useManageTask, useProjectAssignedOrganizationsName, useProjectAssignedServices } from "../../../hooks/useProjects"; +import { useManageTask, useProjectAssignedOrganizationsName, useProjectAssignedServices } from "../../../hooks/useProjects"; import showToast from "../../../services/toastService"; import Label from "../../common/Label"; import { useSelectedProject } from "../../../slices/apiDataManager"; +import { AppFormController, AppFormProvider } from "../../../hooks/appHooks/useAppForm"; +import SelectField from "../../common/Forms/SelectField"; const taskSchema = z.object({ buildingID: z.string().min(1, "Building is required"), @@ -76,19 +78,13 @@ const TaskModel = ({ project, onSubmit, onClose }) => { setValue("activityID", ""); }; - - const { - register, - handleSubmit, - watch, - setValue, - reset, - formState: { errors }, - } = useForm({ - resolver: zodResolver(taskSchema), + const methods = useForm({ defaultValues: defaultModel, + resolver: zodResolver(taskSchema), }); + const { register, control, watch, handleSubmit, reset, setValue, formState: { errors } } = methods; + const [isSubmitting, setIsSubmitting] = useState(false); const [activityData, setActivityData] = useState([]); const [categoryData, setCategoryData] = useState([]); @@ -112,10 +108,10 @@ const TaskModel = ({ project, onSubmit, onClose }) => { const { mutate: CreateTask, isPending } = useManageTask({ onSuccessCallback: (response) => { showToast(response?.message, "success"); - setValue("activityID",""), - setValue("plannedWork",0), - setValue("completedWork",0) - setValue("comment","") + setValue("activityID", ""), + setValue("plannedWork", 0), + setValue("completedWork", 0) + setValue("comment", "") }, }); useEffect(() => { @@ -148,224 +144,290 @@ const TaskModel = ({ project, onSubmit, onClose }) => { }; return ( -
-
-
Manage Task
-
-
- - - {errors.buildingID && ( -

{errors.buildingID.message}

- )} -
- - {selectedBuilding && ( + + +
+
Manage Task
+
+ {/* Select Building */}
- - - {errors.floorId && ( -

{errors.floorId.message}

- )} -
- )} - - {/* Work Area Selection */} - {selectedFloor && ( -
- - - {errors.workAreaId && ( -

{errors.workAreaId.message}

- )} -
- )} - - {/* Services Selection */} - {selectedWorkArea && ( -
- - -
- )} - - {/* Activity Group (Organization) Selection */} - {selectedService && ( -
- - - {errors.activityGroupId &&

{errors.activityGroupId.message}

} -
- )} - - - {/* Activity Selection */} - {selectedGroup && ( -
- - - {errors.activityID &&

{errors.activityID.message}

} -
- )} - - {watchActivityId && ( -
- - - {errors.workCategoryId && ( -

{errors.workCategoryId.message}

- )} -
- )} - - {selectedActivity && selectedCategory && ( - <> -
- - - {errors.plannedWork && ( -

{errors.plannedWork.message}

+ ( + { + field.onChange(value); + setValue("floorId", ""); + setValue("workAreaId", ""); + setSelectedService(""); + setSelectedGroup(""); + }} + className="m-0" + /> )} -
-
- - - {errors.completedWork && ( -

{errors.completedWork.message}

- )} -
-
- - -
- - )} - - {selectedActivity && selectedCategory && ( -
- -