diff --git a/src/components/ServiceProject/ServiceProjectBranch/ManageBranch.jsx b/src/components/ServiceProject/ServiceProjectBranch/ManageBranch.jsx index 34cc0530..11bcdafd 100644 --- a/src/components/ServiceProject/ServiceProjectBranch/ManageBranch.jsx +++ b/src/components/ServiceProject/ServiceProjectBranch/ManageBranch.jsx @@ -4,6 +4,7 @@ import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import Label from "../../common/Label"; import { + useBranchDetails, useCreateBranch, useServiceProjects, useUpdateBranch, @@ -13,7 +14,13 @@ import { useParams } from "react-router-dom"; import { BranchSchema, defaultBranches } from "../ServiceProjectSchema"; const ManageBranch = ({ closeModal, BranchToEdit = null }) => { - const { data } = {}; + const { + data, + isLoading, + isError, + error: requestError, + } = useBranchDetails(BranchToEdit); + const { projectId } = useParams(); const schema = BranchSchema(); const { @@ -112,7 +119,7 @@ const ManageBranch = ({ closeModal, BranchToEdit = null }) => {
- +
-
+
@@ -146,7 +153,7 @@ const ManageBranch = ({ closeModal, BranchToEdit = null }) => {
- +
diff --git a/src/components/ServiceProject/ServiceProjectBranch/ServiceBranch.jsx b/src/components/ServiceProject/ServiceProjectBranch/ServiceBranch.jsx index 0cae102a..e168778b 100644 --- a/src/components/ServiceProject/ServiceProjectBranch/ServiceBranch.jsx +++ b/src/components/ServiceProject/ServiceProjectBranch/ServiceBranch.jsx @@ -1,19 +1,24 @@ import React, { useState } from "react"; import GlobalModel from "../../common/GlobalModel"; import ManageBranch from "./ManageBranch"; -import { useBranches } from "../../../hooks/useServiceProject"; +import { useBranches, useDeleteBranch } from "../../../hooks/useServiceProject"; import { ITEMS_PER_PAGE } from "../../../utils/constants"; import { useDebounce } from "../../../utils/appUtils"; import { useParams } from "react-router-dom"; import Pagination from "../../common/Pagination"; +import ConfirmModal from "../../common/ConfirmModal"; +import { SpinnerLoader } from "../../common/Loader"; const ServiceBranch = () => { const { projectId } = useParams(); - + const [IsDeleteModalOpen, setIsDeleteModalOpen] = useState(false); + const [showInactive, setShowInactive] = useState(false); const [manageState, setManageState] = useState({ IsOpen: false, branchId: null, }); + const { mutate: DeleteBranch, isPending } = useDeleteBranch(); + const [deletingId, setDeletingId] = useState(null); const [search, setSearch] = useState(""); const [currentPage, setCurrentPage] = useState(1); @@ -21,7 +26,8 @@ const ServiceBranch = () => { const { data, isLoading, isError, error } = useBranches( projectId, - true, + // true, + !showInactive, ITEMS_PER_PAGE - 10, currentPage, debouncedSearch @@ -42,132 +48,222 @@ const ServiceBranch = () => { }, ]; + const handleDelete = (id) => { + setDeletingId(id); + DeleteBranch( + { id, isActive: false }, + { + onSettled: () => { + setDeletingId(null); + setIsDeleteModalOpen(false); + }, + } + ); + }; return ( -
-
- {/* Header Section */} -
-
-
- - Branches -
+ <> + {IsDeleteModalOpen && ( + setIsDeleteModalOpen(false)} + loading={isPending} + paramData={deletingId} + /> + )} +
+
+ {/* Header Section */} +
+
+
+ + Branch +
+
+ + {/* Flex container for toggle + button */} +
+
+ + {/* Toggle Switch */} +
+ setShowInactive(!showInactive)} + /> + +
+ + {/* Add Branch Button */} + + +
+
+
-
- -
-
- -
- - - - {columns.map((col) => ( - - ))} - - - - - - {isLoading && ( +
+
- {col.label} - Action
+ - + {columns.map((col) => ( + + ))} + - )} + - {isError && ( - - - - )} - - {!isLoading && - !isError && - data?.data?.length > 0 && - data.data.map((branch) => ( - - {columns.map((col) => ( - - ))} - - - ))} - - {!isLoading && - !isError && - (!data?.data || data.data.length === 0) && ( + + {isLoading && ( )} - -
- Loading... - + {col.label} + Action
- {error?.message || "Error loading branches"} -
- {col.getValue(branch)} - - - setManageState({ - IsOpen: true, - branchId: branch.id, - }) - } - /> -
- No Branch Found +
+ +
- {data?.data?.length > 0 && ( - + + {isError && ( + + + {error?.message || "Error loading branches"} + + + )} + + {!isLoading && + !isError && + data?.data?.length > 0 && + data.data.map((branch) => ( + + {columns.map((col) => ( + + {col.getValue(branch)} + + ))} + +
+ + +
    + {/* Modify */} +
  • + setManageState({ + IsOpen: true, + branchId: branch.id, + }) + } + > + + + Modify + +
  • + + {/* Delete */} +
  • { + setIsDeleteModalOpen(true); + setDeletingId(branch.id); + }} + > + + + Delete + +
  • +
+
+ + + + ))} + + {!isLoading && + !isError && + (!data?.data || data.data.length === 0) && ( + + + No Branch Found + + + )} + + + + {data?.data?.length > 0 && ( + + )} +
+ + {manageState.IsOpen && ( + setManageState({ IsOpen: false, branchId: null })} + > + + setManageState({ IsOpen: false, branchId: null }) + } + /> + + )}
- - {manageState.IsOpen && ( - setManageState({ IsOpen: false, branchId: null })} - > - - setManageState({ IsOpen: false, branchId: null }) - } - /> - - )}
-
+ ); }; diff --git a/src/hooks/useServiceProject.jsx b/src/hooks/useServiceProject.jsx index 3278fa2a..93817800 100644 --- a/src/hooks/useServiceProject.jsx +++ b/src/hooks/useServiceProject.jsx @@ -315,12 +315,12 @@ export const useBranches = ( }; -export const useBranch = (id)=>{ +export const useBranchDetails = (id) => { return useQuery({ queryKey: ["branch", id], queryFn: async () => { const resp = await ServiceProjectRepository.GetBranchDetail(id); - return resp.data ?? resp; + return resp.data; }, enabled: !!id }) @@ -346,37 +346,42 @@ export const useCreateBranch = (onSuccessCallBack) => { }, }); }; - -export const useUpdateBranch = () => { +export const useUpdateBranch = (onSuccessCallBack) => { const queryClient = useQueryClient(); return useMutation({ - mutationFn: async ({ id, payload }) => await ServiceProjectRepository.UpdateBranch(id, payload), + mutationFn: async ({ id, payload }) => + await ServiceProjectRepository.UpdateBranch(id, payload), + onSuccess: (_, variables) => { + // remove old single-branch cache + queryClient.removeQueries({ queryKey: ["branch", variables.id] }); + + // refresh list queryClient.invalidateQueries({ queryKey: ["branches"] }); - queryClient.invalidateQueries({ queryKey: ["branch", variables.id] }); - if (onSuccessCallback) onSuccessCallback(); - showToast("Branch Updated successfully", "success"); + + showToast("Branch updated successfully", "success"); + onSuccessCallBack?.(); }, - onError: (error) => { - showToast( - error?.response?.data?.message || - error.message || - "Failed to update branch", - "error" - ); + + onError: () => { + showToast("Something went wrong. Please try again later.", "error"); }, - }) -} + }); +}; + export const useDeleteBranch = () => { const queryClient = useQueryClient(); + return useMutation({ - mutationFn: async (id) => await ServiceProjectRepository.DeleteBranch(id), - onSuccess: (_, variables) => { + mutationFn: async ({ id, isActive}) => + await ServiceProjectRepository.DeleteBranch(id, isActive), + + onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["branches"] }); - if (onSuccessCallback) onSuccessCallback(); - showToast("Branch Deleted successfully", "success"); + showToast("Branch deleted successfully", "success"); }, + onError: (error) => { showToast( error?.response?.data?.message || @@ -385,5 +390,5 @@ export const useDeleteBranch = () => { "error" ); }, - }) -} \ No newline at end of file + }); +}; diff --git a/src/repositories/ServiceProjectRepository.jsx b/src/repositories/ServiceProjectRepository.jsx index 1fa29fa9..23fe8b38 100644 --- a/src/repositories/ServiceProjectRepository.jsx +++ b/src/repositories/ServiceProjectRepository.jsx @@ -1,3 +1,4 @@ +import { isAction } from "@reduxjs/toolkit"; import { api } from "../utils/axiosClient"; export const ServiceProjectRepository = { @@ -43,14 +44,17 @@ export const ServiceProjectRepository = { //#region Project Branch CreateBranch: (data) => api.post(`/api/ServiceProject/branch/create`, data), UpdateBranch: (id, data) => - api.put("/api/ServiceProject/branch/edit/${id}", data), + api.put(`/api/ServiceProject/branch/edit/${id}`, data), + GetBranchList: (projectId, isActive, pageSize, pageNumber, searchString) => { return api.get( `/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}`), + DeleteBranch: (id, isActive = false) => + api.delete(`/api/ServiceProject/branch/delete/${id}?isActive=${isActive}`), + };