From 8a5d6158c2c5b27649fc7b6e851aba525d091bdb Mon Sep 17 00:00:00 2001 From: Kartik Sharma Date: Wed, 19 Nov 2025 18:31:17 +0530 Subject: [PATCH] Implementing Creating API. --- .../ServiceProject/ManageBranch.jsx | 120 +++++++------- .../ServiceProject/ServiceBranch.jsx | 155 ++++++++---------- src/hooks/useServiceProject.jsx | 83 +++++----- src/repositories/ServiceProjectRepository.jsx | 8 +- 4 files changed, 181 insertions(+), 185 deletions(-) diff --git a/src/components/ServiceProject/ManageBranch.jsx b/src/components/ServiceProject/ManageBranch.jsx index 2eae8c49..70257710 100644 --- a/src/components/ServiceProject/ManageBranch.jsx +++ b/src/components/ServiceProject/ManageBranch.jsx @@ -1,12 +1,16 @@ -import React from 'react' +import React, { useEffect } from 'react' import { useProjectName } from '../../hooks/useProjects'; import { BranchSchema, defaultBranches } from './BranchSchema'; import { useForm } from 'react-hook-form'; import { zodResolver } from '@hookform/resolvers/zod'; import Label from '../common/Label'; +import { useCreateBranch, useServiceProjects, useUpdateBranch } from '../../hooks/useServiceProject'; +import { useAppForm } from '../../hooks/appHooks/useAppForm'; +import { useParams } from 'react-router-dom'; -const ManageBranch = ({ closeModal,BranchToEdit = null }) => { - +const ManageBranch = ({ closeModal, BranchToEdit = null }) => { + const { data } = {} + const { projectId } = useParams(); const schema = BranchSchema(); const { register, @@ -16,23 +20,50 @@ const ManageBranch = ({ closeModal,BranchToEdit = null }) => { setValue, reset, formState: { errors }, - } = useForm({ + } = useAppForm({ resolver: zodResolver(schema), - defaultValues: defaultBranches, + defaultValues: { + ...defaultBranches, + projectId: projectId || "" + }, }); - const { - projectNames, - loading: projectLoading, - error, - isError: isProjectError, - } = useProjectName(); - const handleClose = () => { reset(); closeModal(); }; + useEffect(() => { + if (BranchToEdit && data) { + reset({ + branchName: data.branchName || "", + projectId: data.project?.id || projectId || "", + contactInformation: data.contactInformation || "", + address: data.address || "", + branchType: data.branchType || "", + googleMapUrl: data.googleMapUrl || "", + }); + } + }, [data, reset]); + + const { mutate: CreateServiceBranch, isPending: createPending } = + useCreateBranch(() => { + handleClose(); + }); + const { mutate: ServiceBranchUpdate, isPending } = + useUpdateBranch(() => handleClose()); + + const onSubmit = (fromdata) => { + let payload = { + ...fromdata, projectId, + }; + if (BranchToEdit) { + const editPayload = { ...payload, id: data.id }; + ServiceBranchUpdate({ id: data.id, payload: editPayload }); + } else { + CreateServiceBranch(payload); + } + }; return (
@@ -41,50 +72,24 @@ const ManageBranch = ({ closeModal,BranchToEdit = null }) => { ? "Update Branch" : "Create Branch"} -
+
+
-
- -
- - - {errors.branchName && ( - {errors.branchName.message} - )} -
-
- -
+
+ +
+
-
- - -
+
+ + +
+
-
- - - )) - ) : ( - - + + + setManageServiceBranch({ + IsOpen: true, + branchId: branch.id, + }) + } + > + - )} - */} + )) + ) : ( + + + No Branch Found + + + )} + */} + + {/* )} */} {/* {!filteredData || @@ -158,10 +139,16 @@ const ServiceBranch = () => { closeModal={() => setManageServiceBranch({ IsOpen: null, branchId: null }) } - // requestToEdit={ManageServiceBranch.branchId} + // requestToEdit={ManageServiceBranch.branchId} /> )} + + {/* */}
) diff --git a/src/hooks/useServiceProject.jsx b/src/hooks/useServiceProject.jsx index b0b98719..3dfd4115 100644 --- a/src/hooks/useServiceProject.jsx +++ b/src/hooks/useServiceProject.jsx @@ -59,8 +59,8 @@ export const useCreateServiceProject = (onSuccessCallback) => { onError: (error) => { showToast( error?.response?.data?.message || - error.message || - "Failed to delete task", + error.message || + "Failed to delete task", "error" ); }, @@ -84,8 +84,8 @@ export const useUpdateServiceProject = (onSuccessCallback) => { onError: (error) => { showToast( error?.response?.data?.message || - error.message || - "Failed to update project", + error.message || + "Failed to update project", "error" ); }, @@ -110,8 +110,8 @@ export const useActiveInActiveServiceProject = (onSuccessCallback) => { onError: (error) => { showToast( error?.response?.data?.message || - error.message || - "Failed to update project", + error.message || + "Failed to update project", "error" ); }, @@ -138,8 +138,8 @@ export const useAllocationServiceProjectTeam = (onSuccessCallback) => { onError: (error) => { showToast( error?.response?.data?.message || - error.message || - "Failed to update project", + error.message || + "Failed to update project", "error" ); }, @@ -223,8 +223,8 @@ export const useAddCommentJob = (onSuccessCallback) => { onError: (error) => { showToast( error?.response?.data?.message || - error.message || - "Failed to update project", + error.message || + "Failed to update project", "error" ); }, @@ -247,8 +247,8 @@ export const useCreateServiceProjectJob = (onSuccessCallback) => { onError: (error) => { showToast( error?.response?.data?.message || - error.message || - "Failed to update project", + error.message || + "Failed to update project", "error" ); }, @@ -273,8 +273,8 @@ export const useUpdateServiceProjectJob = (onSuccessCallback) => { onError: (error) => { showToast( error?.response?.data?.message || - error.message || - "Failed to update project", + error.message || + "Failed to update project", "error" ); }, @@ -314,65 +314,64 @@ export const useBranches = ( }); }; -export const useBranch = (id)=>{ +export const useBranch = (id) => { return useQuery({ - queryKey:["branch",id], - queryFn:async()=>{ + queryKey: ["branch", id], + queryFn: async () => { const resp = await ServiceProjectRepository.GetBranchDetail(id); return resp.data ?? resp; }, - enabled:!!id + enabled: !!id }) } -export const useCreateBranche =()=>{ +export const useCreateBranch = (onSuccessCallBack) => { const queryClient = useQueryClient(); return useMutation({ - mutationFn:async(payload)=> await ServiceProjectRepository.CreateBranch(payload), - onSuccess: (data, variables) => { - queryClient.invalidateQueries({ - queryKey: ["branches"], - }); - if (onSuccessCallback) onSuccessCallback(); - showToast("Branch created successfully", "success"); + mutationFn: async (payload) => { + await ServiceProjectRepository.CreateBranch(payload); + }, + + onSuccess: (_, variables) => { + queryClient.invalidateQueries({ queryKey: ["branches"] }); + showToast("Branches Created Successfully", "success"); + if (onSuccessCallBack) onSuccessCallBack(); }, onError: (error) => { showToast( - error?.response?.data?.message || - error.message || - "Failed to create branch", + error.message || "Something went wrong please try again !", "error" ); }, - }) -} + }); +}; -export const useUpdateBranch=()=>{ +export const useUpdateBranch = () => { const queryClient = useQueryClient(); return useMutation({ - mutationFn:async({id,payload})=> await ServiceProjectRepository.UpdateBranch(id,payload), - onSuccess: (_,variables) => { + mutationFn: async ({ id, payload }) => await ServiceProjectRepository.UpdateBranch(id, payload), + onSuccess: (_, variables) => { queryClient.invalidateQueries({ queryKey: ["branches"] }); - queryClient.invalidateQueries({ queryKey: ["branch",variables.id] }); + queryClient.invalidateQueries({ queryKey: ["branch", variables.id] }); if (onSuccessCallback) onSuccessCallback(); showToast("Branch Updated successfully", "success"); }, onError: (error) => { showToast( error?.response?.data?.message || - error.message || - "Failed to update branch", + error.message || + "Failed to update branch", "error" ); }, }) } -export const useDeleteBranch=()=>{ +export const useDeleteBranch = () => { const queryClient = useQueryClient(); return useMutation({ - mutationFn:async(id)=> await ServiceProjectRepository.DeleteBranch(id), - onSuccess: (_,variables) => { + mutationFn: async (id) => await ServiceProjectRepository.DeleteBranch(id), + onSuccess: (_, variables) => { queryClient.invalidateQueries({ queryKey: ["branches"] }); if (onSuccessCallback) onSuccessCallback(); showToast("Branch Deleted successfully", "success"); @@ -380,8 +379,8 @@ export const useDeleteBranch=()=>{ onError: (error) => { showToast( error?.response?.data?.message || - error.message || - "Failed to delete branch", + error.message || + "Failed to delete branch", "error" ); }, diff --git a/src/repositories/ServiceProjectRepository.jsx b/src/repositories/ServiceProjectRepository.jsx index b61505cd..e838a04b 100644 --- a/src/repositories/ServiceProjectRepository.jsx +++ b/src/repositories/ServiceProjectRepository.jsx @@ -41,16 +41,16 @@ export const ServiceProjectRepository = { //#endregion //#region Project Branch - CreateBranch: (data) => api.post(`/api/ServiceProject/create`, data), + CreateBranch: (data) => api.post(`/api/ServiceProject/branch/create`, data), UpdateBranch: (id, data) => api.put("/api/ServiceProject/branch/edit/${id}", data), GetBranchList: (projectId, isActive, pageSize, pageNumber, searchString) => { api.get( - `/api/ServiceProject/branch/list/${projectId}&isActive=${isActive}&pageSize=${pageSize}&pageNumber=${pageNumber}&searchString=${searchString}` + `/api/ServiceProject/branch/list/${projectId}/?isActive=${isActive}&pageSize=${pageSize}&pageNumber=${pageNumber}&searchString=${searchString}` ); - }, + }, + GetBranchDetail: (id) => api.get(`/api/ServiceProject/branch/details/${id}`), DeleteBranch: (id) => api.delete(`/api/ServiceProject/branch/delete/${id}`), }; -