Skip to content

Commit

Permalink
feat: add client hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
olexh committed Aug 21, 2024
1 parent a8c9395 commit 1cd4fa6
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/services/hooks/client/use-client-info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { QueryKey, useQuery } from '@tanstack/react-query';

import getFullClient, { Params } from '@/services/api/client/getFullClient';
import getQueryClient from '@/utils/get-query-client';

export const paramsBuilder = (props: Params): Params => ({
client_reference: props.client_reference,
});

export const key = (params: Params): QueryKey => [
'full-client',
params.client_reference,
];

const useClientInfo = (props: Params, options?: Hikka.QueryOptions) => {
const params = paramsBuilder(props);

return useQuery({
queryKey: key(params),
queryFn: () =>
getFullClient({
params,
}),
...options,
refetchOnWindowFocus: false,
});
};

export const prefetchClientInfo = (props: Params) => {
const queryClient = getQueryClient();
const params = paramsBuilder(props);

return queryClient.prefetchQuery({
queryKey: key(params),
queryFn: () =>
getFullClient({
params,
}),
});
};

export default useClientInfo;
42 changes: 42 additions & 0 deletions src/services/hooks/client/use-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { QueryKey, useQuery } from '@tanstack/react-query';

import getClient, { Params } from '@/services/api/client/getClient';
import getQueryClient from '@/utils/get-query-client';

export const paramsBuilder = (props: Params): Params => ({
client_reference: props.client_reference,
});

export const key = (params: Params): QueryKey => [
'client',
params.client_reference,
];

const useClient = (props: Params, options?: Hikka.QueryOptions) => {
const params = paramsBuilder(props);

return useQuery({
queryKey: key(params),
queryFn: () =>
getClient({
params,
}),
...options,
refetchOnWindowFocus: false,
});
};

export const prefetchClient = (props: Params) => {
const queryClient = getQueryClient();
const params = paramsBuilder(props);

return queryClient.prefetchQuery({
queryKey: key(params),
queryFn: () =>
getClient({
params,
}),
});
};

export default useClient;
26 changes: 26 additions & 0 deletions src/services/hooks/client/use-clients.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { QueryKey, useQuery } from '@tanstack/react-query';

import getClients from '@/services/api/client/getClients';
import getQueryClient from '@/utils/get-query-client';

export const key = (): QueryKey => ['clients'];

const useClients = (options?: Hikka.QueryOptions) => {
return useQuery({
queryKey: key(),
queryFn: () => getClients(),
...options,
refetchOnWindowFocus: false,
});
};

export const prefetchClientInfo = () => {
const queryClient = getQueryClient();

return queryClient.prefetchQuery({
queryKey: key(),
queryFn: () => getClients(),
});
};

export default useClients;
20 changes: 20 additions & 0 deletions src/services/hooks/client/use-create-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';

import createClient from '@/services/api/client/createClient';
import { useModalContext } from '@/services/providers/modal-provider';

const useCreateClient = () => {
const { closeModal } = useModalContext();
const queryClient = useQueryClient();

return useMutation({
mutationKey: ['create-client'],
mutationFn: createClient,
onSettled: async () => {
await queryClient.invalidateQueries({ queryKey: ['clients'] });
closeModal();
},
});
};

export default useCreateClient;
20 changes: 20 additions & 0 deletions src/services/hooks/client/use-delete-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';

import deleteClient from '@/services/api/client/deleteClient';
import { useModalContext } from '@/services/providers/modal-provider';

const useDeleteClient = () => {
const { closeModal } = useModalContext();
const queryClient = useQueryClient();

return useMutation({
mutationKey: ['delete-client'],
mutationFn: deleteClient,
onSettled: async () => {
await queryClient.invalidateQueries({ queryKey: ['clients'] });
closeModal();
},
});
};

export default useDeleteClient;
20 changes: 20 additions & 0 deletions src/services/hooks/client/use-update-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';

import updateClient from '@/services/api/client/updateClient';
import { useModalContext } from '@/services/providers/modal-provider';

const useUpdateClient = () => {
const { closeModal } = useModalContext();
const queryClient = useQueryClient();

return useMutation({
mutationKey: ['update-client'],
mutationFn: updateClient,
onSettled: async () => {
await queryClient.invalidateQueries({ queryKey: ['clients'] });
closeModal();
},
});
};

export default useUpdateClient;
20 changes: 20 additions & 0 deletions src/services/hooks/client/use-verify-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';

import verifyClient from '@/services/api/client/verifyClient';
import { useModalContext } from '@/services/providers/modal-provider';

const useVerifyClient = () => {
const { closeModal } = useModalContext();
const queryClient = useQueryClient();

return useMutation({
mutationKey: ['verify-client'],
mutationFn: verifyClient,
onSettled: async () => {
await queryClient.invalidateQueries({ queryKey: ['clients'] });
closeModal();
},
});
};

export default useVerifyClient;

0 comments on commit 1cd4fa6

Please sign in to comment.