diff --git a/src/components/master/EditActivityGroup.jsx b/src/components/master/EditActivityGroup.jsx new file mode 100644 index 00000000..8ff4bc10 --- /dev/null +++ b/src/components/master/EditActivityGroup.jsx @@ -0,0 +1,187 @@ +import React, { useEffect, useState } from "react"; +import { useForm, Controller } from "react-hook-form"; +import { set, z } from "zod"; +import { zodResolver } from "@hookform/resolvers/zod"; +import { MasterRespository } from "../../repositories/MastersRepository"; +import { cacheData, getCachedData } from "../../slices/apiDataManager"; +import showToast from "../../services/toastService"; + +const schema = z.object({ + name: z.string().min(1, { message: "Service Name is required" }), + description: z + .string() + .min(1, { message: "Description is required" }) + .max(255, { message: "Description cannot exceed 255 characters" }), + serviceId: z.string().min(1, { message: "A service selection is required." }), +}); + +const EditActivityGroup = ({ data, onClose }) => { + const [isLoading, setIsLoading] = useState(false); + const [services, setServices] = useState(getCachedData("Services")); + const [formData, setFormData] = useState({ + name: data?.name || "", + description: data?.description || "", + serviceId: data.serviceId || "", + }); + const { + register, + handleSubmit, + formState: { errors }, + reset, + watch, + } = useForm({ + resolver: zodResolver(schema), + defaultValues: { + name: data?.name || "", + description: data?.description || "", + serviceId: data.serviceId || "", + }, + }); + const selectedServiceId = watch("serviceId"); + const selectedName = watch("name"); + const selectedDescription = watch("description"); + + // const handleServicesChange = (e) => { + // const { value } = e.target; + // const service = services.find((b) => b.id === String(value)); + // reset((prev) => ({ + // ...prev, + // serviceId: String(value), + // })); + // }; + + const onSubmit = (formdata) => { + setIsLoading(true); + const result = { + id: data?.id, + name: formdata?.name, + description: formdata.description, + serviceId: formdata?.serviceId, + }; + + MasterRespository.updateActivityGroup(data?.id, result) + .then((resp) => { + setIsLoading(false); + showToast("Activity Group Update successfully.", "success"); + const cachedData = getCachedData("Activity Group"); + if (cachedData) { + const updatedData = cachedData.map((service) => + service.id === data?.id ? { ...service, ...resp.data } : service + ); + cacheData("Activity Group", updatedData); + } + + onClose(); + }) + .catch((error) => { + showToast(error.message, "error"); + setIsLoading(false); + }); + }; + + useEffect(() => { + if (!services) { + MasterRespository.getServices().then((res) => { + setServices(res?.data); + cacheData("Services", res?.data); + }); + } + reset({ + name: data?.name, + description: data?.description, + serviceId: data?.serviceId, + }); + setDescriptionLength(data?.description?.length || 0); + }, [data, reset]); + + const [descriptionLength, setDescriptionLength] = useState( + data?.description?.length || 0 + ); + const maxDescriptionLength = 255; + + return ( + <> +
+ > + ); +}; + +export default EditActivityGroup;