api integrated
This commit is contained in:
parent
2b24351316
commit
ff5a772067
@ -16,59 +16,11 @@ const schema = z.object({
|
||||
.optional(),
|
||||
});
|
||||
|
||||
const CreateActivity = () =>
|
||||
const CreateActivity = ({onClose}) =>
|
||||
{
|
||||
|
||||
const [ isLoading, setIsLoading ] = useState( false )
|
||||
|
||||
// const {
|
||||
// register,
|
||||
// handleSubmit,
|
||||
// formState: { errors },reset
|
||||
// } = useForm({
|
||||
// resolver: zodResolver(schema),
|
||||
// defaultValues: {
|
||||
// activityName: "",
|
||||
// unitOfMeasurement: "",
|
||||
// checkList: ['']
|
||||
// },
|
||||
// });
|
||||
|
||||
// const onSubmit = (data) => {
|
||||
// setIsLoading(true)
|
||||
// const result = {
|
||||
// name: data.activityName,
|
||||
// description: data.unitOfMeasurement,
|
||||
// };
|
||||
// console.log( result )
|
||||
// reset()
|
||||
// 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({
|
||||
// activityName:"",
|
||||
// unitOfMeasurement:""
|
||||
// })
|
||||
// }
|
||||
|
||||
// useEffect(()=>{
|
||||
// return ()=>resetForm()
|
||||
// }, [] )
|
||||
|
||||
const {
|
||||
register,
|
||||
handleSubmit,
|
||||
@ -99,8 +51,25 @@ const CreateActivity = () =>
|
||||
});
|
||||
|
||||
// Form submission handler
|
||||
const onSubmit = (data) => {
|
||||
console.log('Submitted:', data);
|
||||
const onSubmit = ( data ) =>
|
||||
{
|
||||
console.log(data)
|
||||
setIsLoading(true)
|
||||
|
||||
|
||||
MasterRespository.updateActivity(data).then((resp)=>{
|
||||
setIsLoading(false)
|
||||
|
||||
const cachedData = getCachedData("Activity");
|
||||
const updatedData = [...cachedData, resp?.data];
|
||||
cacheData("Activity", updatedData);
|
||||
showToast("Activity Added successfully.", "success");
|
||||
|
||||
onClose()
|
||||
}).catch((error)=>{
|
||||
showToast(error.message, "error");
|
||||
setIsLoading(false)
|
||||
})
|
||||
};
|
||||
|
||||
// Add a new checklist item
|
||||
@ -130,6 +99,12 @@ const CreateActivity = () =>
|
||||
const handleChecklistChange = (index, value) => {
|
||||
setValue(`checkList.${index}`, value);
|
||||
};
|
||||
|
||||
const handleClose = () =>
|
||||
{
|
||||
reset()
|
||||
onClose()
|
||||
}
|
||||
return (
|
||||
<form onSubmit={handleSubmit(onSubmit)}>
|
||||
<div className="row">
|
||||
@ -187,12 +162,12 @@ const CreateActivity = () =>
|
||||
|
||||
<div className="col-12 text-center mt-3">
|
||||
<button type="submit" className="btn btn-sm btn-primary me-3">
|
||||
Submit
|
||||
{isLoading ? "Please Wait" : "Submit"}
|
||||
</button>
|
||||
<button
|
||||
type="reset"
|
||||
className="btn btn-sm btn-label-secondary"
|
||||
onClick={() => reset()}
|
||||
onClick={handleClose}
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
|
@ -2,6 +2,10 @@ import React, { useState, useEffect } from 'react';
|
||||
import { useFieldArray, useForm } from 'react-hook-form';
|
||||
import { z } from 'zod';
|
||||
import { zodResolver } from '@hookform/resolvers/zod';
|
||||
import {MasterRespository} from '../../repositories/MastersRepository';
|
||||
import axios from 'axios';
|
||||
import showToast from '../../services/toastService';
|
||||
import {cacheData, getCachedData} from '../../slices/apiDataManager';
|
||||
|
||||
// Zod Schema for validation
|
||||
const schema = z.object({
|
||||
@ -12,16 +16,8 @@ const schema = z.object({
|
||||
.optional(),
|
||||
} );
|
||||
|
||||
// for demo data
|
||||
let initialData = {
|
||||
activityName: "Item",
|
||||
unitOfMeasurement: "Item-2",
|
||||
checkList: [
|
||||
'item 3',
|
||||
'Item 4'
|
||||
]
|
||||
}
|
||||
const EditActivity = ({onClose}) => {
|
||||
let token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOlsiOGFmNjFlNDgtYzRkMy00MTYzLWI4NjAtMmEyZWNiNjQ3NDZiIiwiYjUxMzcwOWEtYmZiZS00OTM1LTlmNWMtOGVjN2IwMzFjNTFlIl0sInN1YiI6InZpa2FzQG1hcmNvYWlvdC5jb20iLCJUZW5hbnRJZCI6IjIiLCJleHAiOjE3NDQzNzQyNzAsImlzcyI6Imh0dHA6Ly9sb2NhbGhvc3Q6NTI0NiIsImF1ZCI6Imh0dHA6Ly9sb2NhbGhvc3Q6NTI0NiJ9.reQlePmwDpBL-_mcGOrWwADLJrxmUse5Gd7A-OgDi9s"
|
||||
const EditActivity = ({activityData,onClose}) => {
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
|
||||
const {
|
||||
@ -55,19 +51,45 @@ const EditActivity = ({onClose}) => {
|
||||
|
||||
// Pre-populating the form with initial data when component mounts or initialData changes
|
||||
useEffect(() => {
|
||||
if (initialData) {
|
||||
if (activityData) {
|
||||
reset({
|
||||
activityName: initialData.activityName,
|
||||
unitOfMeasurement: initialData.unitOfMeasurement,
|
||||
checkList: initialData.checkList || [],
|
||||
activityName: activityData.activityName,
|
||||
unitOfMeasurement: activityData.unitOfMeasurement,
|
||||
checkList: activityData.checkList || [],
|
||||
});
|
||||
}
|
||||
}, [initialData, reset]);
|
||||
}, [activityData, reset]);
|
||||
|
||||
// Form submission handler
|
||||
const onSubmit = (data) => {
|
||||
console.log('Submitted:', data);
|
||||
};
|
||||
const onSubmit = async( data ) =>
|
||||
{
|
||||
console.log(data)
|
||||
setIsLoading(true)
|
||||
MasterRespository.updateActivity(activityData?.id,data).then((resp)=>{
|
||||
setIsLoading(false)
|
||||
const cachedData = getCachedData("Activity");
|
||||
if (cachedData) {
|
||||
const updatedData = cachedData.map((activity) =>
|
||||
activity.id === activityData?.id
|
||||
? {
|
||||
...activity,
|
||||
activityName: resp.data.activityName,
|
||||
unitOfMeasurement: resp.data.unitOfMeasurement,
|
||||
}
|
||||
: activity
|
||||
);
|
||||
|
||||
cacheData("Activity", updatedData);
|
||||
}
|
||||
showToast("Activity Added successfully.", "success");
|
||||
onClose()
|
||||
}).catch((error)=>{
|
||||
showToast(error.message, "error");
|
||||
setIsLoading(false)
|
||||
})
|
||||
|
||||
|
||||
};
|
||||
|
||||
// Add a new checklist item
|
||||
const addChecklistItem = () => {
|
||||
|
@ -55,7 +55,7 @@ const MasterModal = ({ modaldata, closeModal }) => {
|
||||
<CreateActivity onClose={closeModal} /> )
|
||||
}
|
||||
{modaldata?.modalType === 'Edit-Activity' && (
|
||||
<EditActivity onClose={closeModal} />
|
||||
<EditActivity activityData={modaldata.item} onClose={closeModal} />
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
@ -30,8 +30,8 @@ export const MasterRespository = {
|
||||
updateJobRole: ( id, data ) => api.put( `/api/roles/jobrole/${ id }`, data ),
|
||||
|
||||
getActivites: () => api.get( 'api/master/activities' ),
|
||||
createActivities: () => api.post( 'api/master/activity' ),
|
||||
updateActivity:(id) =>api.post(`api/master/edit/${id}`),
|
||||
createActivity: (data) => api.post( 'api/master/activity',data ),
|
||||
updateActivity:(id,data) =>api.post(`api/master/edit/${id}`,data),
|
||||
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user