mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-15 20:30:33 +00:00
feat(branche): add crud branches.
This commit is contained in:
99
src/hooks/query/branches.js
Normal file
99
src/hooks/query/branches.js
Normal file
@@ -0,0 +1,99 @@
|
||||
import { useQueryClient, useMutation } from 'react-query';
|
||||
import { useRequestQuery } from '../useQueryRequest';
|
||||
import useApiRequest from '../useRequest';
|
||||
import t from './types';
|
||||
|
||||
// Common invalidate queries.
|
||||
const commonInvalidateQueries = (queryClient) => {
|
||||
// Invalidate warehouses.
|
||||
queryClient.invalidateQueries(t.BRANCHES);
|
||||
queryClient.invalidateQueries(t.BRANCH);
|
||||
};
|
||||
|
||||
/**
|
||||
* Create a new branch.
|
||||
*/
|
||||
export function useCreateBranch(props) {
|
||||
const queryClient = useQueryClient();
|
||||
const apiRequest = useApiRequest();
|
||||
|
||||
return useMutation((values) => apiRequest.post('branches', values), {
|
||||
onSuccess: (res, values) => {
|
||||
// Common invalidate queries.
|
||||
commonInvalidateQueries(queryClient);
|
||||
},
|
||||
...props,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Edits the given branch.
|
||||
*/
|
||||
export function useEditBranch(props) {
|
||||
const queryClient = useQueryClient();
|
||||
const apiRequest = useApiRequest();
|
||||
|
||||
return useMutation(
|
||||
([id, values]) => apiRequest.post(`branches/${id}`, values),
|
||||
{
|
||||
onSuccess: (res, [id, values]) => {
|
||||
// Invalidate specific branch.
|
||||
queryClient.invalidateQueries([t.BRANCH, id]);
|
||||
|
||||
// Common invalidate queries.
|
||||
commonInvalidateQueries(queryClient);
|
||||
},
|
||||
...props,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the given branch.
|
||||
*/
|
||||
export function useDeleteBranch(props) {
|
||||
const queryClient = useQueryClient();
|
||||
const apiRequest = useApiRequest();
|
||||
|
||||
return useMutation((id) => apiRequest.delete(`branches/${id}`), {
|
||||
onSuccess: (res, id) => {
|
||||
// Invalidate specific branch.
|
||||
queryClient.invalidateQueries([t.BRANCH, id]);
|
||||
|
||||
// Common invalidate queries.
|
||||
commonInvalidateQueries(queryClient);
|
||||
},
|
||||
...props,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve Branches list.
|
||||
*/
|
||||
export function useBranches(query, props) {
|
||||
return useRequestQuery(
|
||||
[t.BRANCHES, query],
|
||||
{ method: 'get', url: 'branches', params: query },
|
||||
{
|
||||
select: (res) => res.data.branches,
|
||||
defaultData: [],
|
||||
...props,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the branch details.
|
||||
* @param {number}
|
||||
*/
|
||||
export function useBranch(id, props, requestProps) {
|
||||
return useRequestQuery(
|
||||
[t.BRANCH, id],
|
||||
{ method: 'get', url: `branches/${id}`, ...requestProps },
|
||||
{
|
||||
select: (res) => res.data.branch,
|
||||
defaultData: {},
|
||||
...props,
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -34,3 +34,4 @@ export * from './creditNote';
|
||||
export * from './vendorCredit';
|
||||
export * from './transactionsLocking';
|
||||
export * from './warehouses'
|
||||
export * from './branches';
|
||||
|
||||
@@ -202,6 +202,11 @@ const WAREHOUSE_TRANSFERS = {
|
||||
WAREHOUSE_TRANSFERS: 'WAREHOUSE_TRANSFERS',
|
||||
};
|
||||
|
||||
const BRANCHES = {
|
||||
BRANCHES: 'BRANCHES',
|
||||
BRANCH: 'BRANCH',
|
||||
};
|
||||
|
||||
export default {
|
||||
...ACCOUNTS,
|
||||
...BILLS,
|
||||
@@ -231,4 +236,5 @@ export default {
|
||||
...TARNSACTIONS_LOCKING,
|
||||
...WAREHOUSES,
|
||||
...WAREHOUSE_TRANSFERS,
|
||||
...BRANCHES,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user