108 lines
2.9 KiB
JavaScript
108 lines
2.9 KiB
JavaScript
import React, { useEffect } from "react";
|
|
import Label from "../../common/Label";
|
|
import { useForm } from "react-hook-form";
|
|
import { useCreateService, useUpdateService } from "../../../hooks/masterHook/useMaster";
|
|
import { z } from "zod";
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
|
|
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" }),
|
|
});
|
|
|
|
const ManageServices = ({ data , onClose }) => {
|
|
const {
|
|
register,
|
|
handleSubmit,
|
|
reset,
|
|
formState: { errors },
|
|
} = useForm({
|
|
resolver: zodResolver(schema),
|
|
defaultValues: { name: "", description: "" },
|
|
});
|
|
|
|
const { mutate: CreateServices, isPending: Creating } = useCreateService(() =>
|
|
onClose?.()
|
|
);
|
|
const { mutate: UpdateServices, isPending: Updating } = useUpdateService(() =>
|
|
onClose?.()
|
|
);
|
|
|
|
const onSubmit = (payload) => {
|
|
if (data && data.id) {
|
|
UpdateServices({ id: data.id, payload: { ...payload, id: data.id } });
|
|
} else {
|
|
CreateServices(payload);
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
if (data) {
|
|
reset({
|
|
name: data.name ?? "",
|
|
description: data.description ?? "",
|
|
});
|
|
}
|
|
}, [data, reset]);
|
|
|
|
return (
|
|
<form className="row g-2" onSubmit={handleSubmit(onSubmit)}>
|
|
<div className="col-12 col-md-12 text-start">
|
|
<Label className="form-label" required>
|
|
Service Name
|
|
</Label>
|
|
<input
|
|
type="text"
|
|
{...register("name")}
|
|
className={`form-control ${errors.name ? "is-invalid" : ""}`}
|
|
/>
|
|
{errors.name && <p className="danger-text">{errors.name.message}</p>}
|
|
</div>
|
|
|
|
<div className="col-12 col-md-12 text-start">
|
|
<Label className="form-label" htmlFor="description" required>
|
|
Description
|
|
</Label>
|
|
<textarea
|
|
rows="3"
|
|
{...register("description")}
|
|
className={`form-control ${errors.description ? "is-invalid" : ""}`}
|
|
></textarea>
|
|
{errors.description && (
|
|
<p className="danger-text">{errors.description.message}</p>
|
|
)}
|
|
</div>
|
|
|
|
<div className="col-12 text-end">
|
|
<button
|
|
type="reset"
|
|
className="btn btn-sm btn-label-secondary me-3"
|
|
data-bs-dismiss="modal"
|
|
aria-label="Close"
|
|
disabled={Creating || Updating}
|
|
>
|
|
Cancel
|
|
</button>
|
|
<button
|
|
type="submit"
|
|
className="btn btn-sm btn-primary"
|
|
disabled={Creating || Updating}
|
|
>
|
|
{Creating
|
|
? "Please Wait..."
|
|
: Updating
|
|
? "Please Wait..."
|
|
: data
|
|
? "Update"
|
|
: "Submit"}
|
|
</button>
|
|
</div>
|
|
</form>
|
|
);
|
|
};
|
|
|
|
export default ManageServices;
|