mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-19 22:30:31 +00:00
refactoring: migrating to react-query to manage service-side state.
This commit is contained in:
72
client/src/hooks/query/customers.js
Normal file
72
client/src/hooks/query/customers.js
Normal file
@@ -0,0 +1,72 @@
|
||||
|
||||
import { useMutation, useQuery } from 'react-query';
|
||||
import ApiService from 'services/ApiService';
|
||||
|
||||
|
||||
const transformCustomers = (response) => {
|
||||
return response.data;
|
||||
}
|
||||
|
||||
const transformCustomer = (response) => {
|
||||
return response.data;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {*} query
|
||||
* @param {*} props
|
||||
*/
|
||||
export function useCustomers(query, props) {
|
||||
return useQuery(
|
||||
['CUSTOMERS', query],
|
||||
() => ApiService
|
||||
.get(`customers`, { params: query })
|
||||
.then(transformCustomers),
|
||||
{
|
||||
initialData: {
|
||||
customers: [],
|
||||
pagination: {},
|
||||
},
|
||||
...props
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {*} props
|
||||
*/
|
||||
export function useEditCustomer(props) {
|
||||
return useMutation(
|
||||
(values, id) => ApiService.post(`customers/${id}`, values),
|
||||
props
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {*} props
|
||||
*/
|
||||
export function useDeleteCustomer(props) {
|
||||
return useMutation(
|
||||
(id) => ApiService.delete(`customers/${id}`),
|
||||
props
|
||||
);
|
||||
}
|
||||
|
||||
export function useCreateCustomer(props) {
|
||||
return useMutation(
|
||||
(values) => ApiService.post('customers', values),
|
||||
props
|
||||
);
|
||||
}
|
||||
|
||||
export function useCustomer(id, props) {
|
||||
return useQuery(
|
||||
['CUSTOMER', id],
|
||||
() => ApiService
|
||||
.get(`customers/${id}`)
|
||||
.then(transformCustomer),
|
||||
props
|
||||
)
|
||||
};
|
||||
Reference in New Issue
Block a user