feat(branche): add crud branches.

This commit is contained in:
elforjani13
2022-02-01 23:18:32 +02:00
committed by a.bouhuolia
parent defa5bb8de
commit 4f5dcb3609
17 changed files with 366 additions and 74 deletions

View 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,
},
);
}

View File

@@ -34,3 +34,4 @@ export * from './creditNote';
export * from './vendorCredit';
export * from './transactionsLocking';
export * from './warehouses'
export * from './branches';

View File

@@ -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,
};