112 lines
3.1 KiB
JavaScript
112 lines
3.1 KiB
JavaScript
import React, { useEffect,useState } from 'react'
|
|
import { useForm } from 'react-hook-form';
|
|
import { z } from 'zod';
|
|
import { zodResolver } from '@hookform/resolvers/zod';
|
|
import { MasterRespository } from '../../repositories/MastersRepository';
|
|
import { clearApiCacheKey } from '../../slices/apiCacheSlice';
|
|
import { getCachedData,cacheData } from '../../slices/apiDataManager';
|
|
import showToast from '../../services/toastService';
|
|
|
|
|
|
const schema = z.object({
|
|
role: z.string().min(1, { message: "Role is required" }),
|
|
description: z.string().min(1, { message: "Description is required" })
|
|
.max(255, { message: "Description cannot exceed 255 characters" }),
|
|
});
|
|
|
|
const CreateJobRole = ({onClose}) => {
|
|
|
|
const[isLoading,setIsLoading] = useState(false)
|
|
const {
|
|
register,
|
|
handleSubmit,
|
|
formState: { errors },reset
|
|
|
|
} = useForm({
|
|
resolver: zodResolver(schema),
|
|
defaultValues: {
|
|
role: "",
|
|
description: "",
|
|
|
|
},
|
|
});
|
|
|
|
const onSubmit = (data) => {
|
|
setIsLoading(true)
|
|
const result = {
|
|
name: data.role,
|
|
description: data.description,
|
|
};
|
|
|
|
MasterRespository.createJobRole(result).then((resp)=>{
|
|
setIsLoading(false)
|
|
resetForm()
|
|
const cachedData = getCachedData("Job Role");
|
|
const updatedData = [...cachedData, resp?.data];
|
|
cacheData("Job Role", updatedData);
|
|
showToast("JobRole Added successfully.", "success");
|
|
|
|
onClose()
|
|
}).catch((error)=>{
|
|
showToast(error.message, "error");
|
|
setIsLoading(false)
|
|
})
|
|
|
|
|
|
};
|
|
const resetForm =()=>{
|
|
reset({
|
|
role:"",
|
|
description:""
|
|
})
|
|
}
|
|
|
|
useEffect(()=>{
|
|
return ()=>resetForm()
|
|
},[])
|
|
|
|
|
|
return (<>
|
|
<form className="row g-2" onSubmit={handleSubmit(onSubmit)}>
|
|
|
|
<div className="col-12 col-md-12">
|
|
<label className="form-label">Role</label>
|
|
<input type="text"
|
|
{...register("role")}
|
|
className={`form-control ${errors.role ? 'is-invalids' : ''}`}
|
|
/>
|
|
{errors.role && <p className="text-danger">{errors.role.message}</p>}
|
|
</div>
|
|
<div className="col-12 col-md-12">
|
|
<label className="form-label" htmlFor="description">Description</label>
|
|
<textarea
|
|
rows="3"
|
|
{...register("description")}
|
|
className={`form-control ${errors.description ? 'is-invalids' : ''}`}
|
|
></textarea>
|
|
{errors.description && (
|
|
<p className="text-danger">{errors.description.message}</p>
|
|
)}
|
|
</div>
|
|
|
|
<div className="col-12 text-center">
|
|
<button type="submit" className="btn btn-sm btn-primary me-3">
|
|
{isLoading? "Please Wait...":"Submit"}
|
|
</button>
|
|
<button
|
|
type="reset"
|
|
className="btn btn-sm btn-label-secondary "
|
|
data-bs-dismiss="modal"
|
|
aria-label="Close"
|
|
>
|
|
Cancel
|
|
</button>
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</>
|
|
)
|
|
}
|
|
|
|
export default CreateJobRole; |