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