refactoring: migrating to react-query to manage service-side state.

This commit is contained in:
a.bouhuolia
2021-02-07 08:10:21 +02:00
parent e093be0663
commit adac2386bb
284 changed files with 8255 additions and 6610 deletions

View File

@@ -0,0 +1,132 @@
import { useMutation, useQuery, useQueryClient } from 'react-query';
import ApiService from 'services/ApiService';
const transformAccount = (response) => {
return response.data.account;
};
/**
* Retrieve accounts list.
*/
export function useAccounts(query, props) {
return useQuery(
['ACCOUNTS', query],
() =>
ApiService.get('accounts', { params: query }).then(
(response) => response.data.accounts,
),
{
initialData: [],
...props
},
);
}
/**
* Retrieve the given account details.
* @param {number} id -
*/
export function useAccount(id, props) {
return useQuery(
['ACCOUNT', id],
() => ApiService.get(`accounts/${id}`).then(transformAccount),
{
initialData: {},
...props,
},
);
}
/**
* Retrieve accounts types list.
*/
export function useAccountsTypes() {
return useQuery(['ACCOUNTS_TYPES'], () => ApiService.get('account_types'), {
initialData: [],
});
}
/**
* Creates account.
*/
export function useCreateAccount(props) {
const client = useQueryClient();
return useMutation((values) => ApiService.post('accounts', values), {
onSuccess: () => {
client.invalidateQueries('ACCOUNTS');
},
...props
});
}
/**
* Edits the given account.
*/
export function useEditAccount(props) {
const query = useQueryClient();
return useMutation(
(values, id) => ApiService.post(`accounts/${id}`, values),
{
onSuccess: () => {
query.invalidateQueries('ACCOUNTS');
},
...props
},
);
}
/**
* Edits the given account.
*/
export function useDeleteAccount(props) {
const query = useQueryClient();
return useMutation(
(id) =>
ApiService.delete(`accounts/${id}`).catch((error) => {
throw new Error(error.response.data);
}),
{
onSuccess: () => {
query.invalidateQueries('ACCOUNTS');
},
...props,
},
);
}
/**
* Actiavte the give account.
*/
export function useActivateAccount(props) {
const query = useQueryClient();
return useMutation(
(id) => ApiService.post(`accounts/${id}/activate`),
{
onSuccess: () => {
},
...props
}
);
}
/**
* Inactivate the given account.
*/
export function useInactivateAccount(props) {
const query = useQueryClient();
return useMutation(
(id) => ApiService.post(`accounts/${id}/inactivate`),
{
onSuccess: () => {
},
...props
},
);
}