mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 13:20:31 +00:00
Merge branch 'feature/react-query' of https://github.com/abouolia/Bigcapital into feature/react-query
This commit is contained in:
@@ -1,5 +1,14 @@
|
||||
import { useMutation, useQuery, useQueryClient } from 'react-query';
|
||||
import { defaultTo } from 'lodash';
|
||||
import ApiService from 'services/ApiService';
|
||||
import { transformPagination } from 'utils';
|
||||
|
||||
const invalidateQueries = (queryClient) => {
|
||||
queryClient.invalidateQueries('INVENTORY_ADJUSTMENTS');
|
||||
|
||||
queryClient.invalidateQueries('ITEMS');
|
||||
queryClient.invalidateQueries('ITEM');
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates the inventory adjustment to the given item.
|
||||
@@ -11,7 +20,7 @@ export function useCreateInventoryAdjustment(props) {
|
||||
(values) => ApiService.post('inventory_adjustments/quick', values),
|
||||
{
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries('INVENTORY_ADJUSTMENTS');
|
||||
invalidateQueries(queryClient)
|
||||
},
|
||||
...props,
|
||||
},
|
||||
@@ -28,7 +37,7 @@ export function useDeleteInventoryAdjustment(props) {
|
||||
(id) => ApiService.delete(`inventory_adjustments/${id}`),
|
||||
{
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries('INVENTORY_ADJUSTMENTS');
|
||||
invalidateQueries(queryClient)
|
||||
},
|
||||
...props
|
||||
},
|
||||
@@ -38,7 +47,7 @@ export function useDeleteInventoryAdjustment(props) {
|
||||
const inventoryAdjustmentsTransformer = (response) => {
|
||||
return {
|
||||
transactions: response.data.inventoy_adjustments,
|
||||
pagination: response.data.pagination,
|
||||
pagination: transformPagination(response.data.pagination),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -46,20 +55,22 @@ const inventoryAdjustmentsTransformer = (response) => {
|
||||
* Retrieve inventory adjustment list with pagination meta.
|
||||
*/
|
||||
export function useInventoryAdjustments(query, props) {
|
||||
return useQuery(
|
||||
const states = useQuery(
|
||||
['INVENTORY_ADJUSTMENTS', query],
|
||||
() => ApiService.get('inventory_adjustments', { params: query })
|
||||
.then(inventoryAdjustmentsTransformer),
|
||||
{
|
||||
initialData: {
|
||||
transactions: [],
|
||||
pagination: {
|
||||
page: 1,
|
||||
page_size: 12,
|
||||
total: 0
|
||||
},
|
||||
},
|
||||
...props,
|
||||
},
|
||||
props,
|
||||
);
|
||||
return {
|
||||
...states,
|
||||
data: defaultTo(states.data, {
|
||||
transactions: [],
|
||||
pagination: {
|
||||
page: 1,
|
||||
pageSize: 12,
|
||||
total: 0,
|
||||
pagesCount: 0,
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,7 @@ export function useCreateItem(props) {
|
||||
return useMutation((values) => ApiService.post('items', values), {
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries('ITEMS');
|
||||
queryClient.invalidateQueries('ITEMS_CATEGORIES');
|
||||
},
|
||||
...props,
|
||||
});
|
||||
@@ -33,6 +34,7 @@ export function useEditItem(props) {
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries('ITEMS');
|
||||
queryClient.invalidateQueries('ITEM');
|
||||
queryClient.invalidateQueries('ITEMS_CATEGORIES');
|
||||
},
|
||||
...props,
|
||||
});
|
||||
@@ -48,6 +50,7 @@ export function useDeleteItem(props) {
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries('ITEMS');
|
||||
queryClient.invalidateQueries('ITEM');
|
||||
queryClient.invalidateQueries('ITEMS_CATEGORIES');
|
||||
},
|
||||
...props,
|
||||
});
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { useQuery, useMutation, useQueryClient } from 'react-query';
|
||||
import { defaultTo } from 'lodash';
|
||||
import ApiService from 'services/ApiService';
|
||||
|
||||
/**
|
||||
@@ -7,15 +8,12 @@ import ApiService from 'services/ApiService';
|
||||
export function useCreateItemCategory(props) {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation(
|
||||
(values) => ApiService.post('item_categories', values),
|
||||
{
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries('ITEMS_CATEGORIES');
|
||||
},
|
||||
...props
|
||||
}
|
||||
);
|
||||
return useMutation((values) => ApiService.post('item_categories', values), {
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries('ITEMS_CATEGORIES');
|
||||
},
|
||||
...props,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -29,9 +27,10 @@ export function useEditItemCategory(props) {
|
||||
{
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries('ITEMS_CATEGORIES');
|
||||
queryClient.invalidateQueries('ITEMS');
|
||||
},
|
||||
...props
|
||||
}
|
||||
...props,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@@ -41,14 +40,13 @@ export function useEditItemCategory(props) {
|
||||
export function useDeleteItemCategory(props) {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation(
|
||||
(id) => ApiService.delete(`item_categories/${id}`),
|
||||
{
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries('ITEMS_CATEGORIES');
|
||||
},
|
||||
...props
|
||||
});
|
||||
return useMutation((id) => ApiService.delete(`item_categories/${id}`), {
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries('ITEMS_CATEGORIES');
|
||||
queryClient.invalidateQueries('ITEMS');
|
||||
},
|
||||
...props,
|
||||
});
|
||||
}
|
||||
|
||||
// Transforms items categories.
|
||||
@@ -63,18 +61,22 @@ const transformItemsCategories = (response) => {
|
||||
* Retrieve the items categories.
|
||||
*/
|
||||
export function useItemsCategories(query, props) {
|
||||
return useQuery(
|
||||
const states = useQuery(
|
||||
['ITEMS_CATEGORIES', query],
|
||||
() => ApiService.get(`item_categories`, { params: query })
|
||||
.then(transformItemsCategories),
|
||||
{
|
||||
initialData: {
|
||||
itemsCategories: [],
|
||||
pagination: {}
|
||||
},
|
||||
...props,
|
||||
},
|
||||
() =>
|
||||
ApiService.get(`item_categories`, { params: query }).then(
|
||||
transformItemsCategories,
|
||||
),
|
||||
props,
|
||||
);
|
||||
|
||||
return {
|
||||
...states,
|
||||
data: defaultTo(states.data, {
|
||||
itemsCategories: [],
|
||||
pagination: {},
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -82,13 +84,15 @@ export function useItemsCategories(query, props) {
|
||||
* @param {number} id - Item category.
|
||||
*/
|
||||
export function useItemCategory(id, props) {
|
||||
return useQuery(
|
||||
const states = useQuery(
|
||||
['ITEMS_CATEGORY', id],
|
||||
() => ApiService.get(`item_categories/${id}`)
|
||||
.then(res => res.data.category),
|
||||
{
|
||||
initialData: {},
|
||||
...props,
|
||||
},
|
||||
() =>
|
||||
ApiService.get(`item_categories/${id}`).then((res) => res.data.category),
|
||||
props,
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
...states,
|
||||
data: defaultTo(states.data, {}),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user