@instockng/api-client 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +263 -0
- package/package.json +45 -0
- package/src/client.ts +57 -0
- package/src/fetchers/carts.ts +202 -0
- package/src/fetchers/delivery-zones.ts +29 -0
- package/src/fetchers/index.ts +22 -0
- package/src/fetchers/orders.ts +48 -0
- package/src/fetchers/products.ts +46 -0
- package/src/hooks/admin/abandoned-carts.ts +102 -0
- package/src/hooks/admin/brands.ts +134 -0
- package/src/hooks/admin/customers.ts +31 -0
- package/src/hooks/admin/delivery-zones.ts +236 -0
- package/src/hooks/admin/discount-codes.ts +222 -0
- package/src/hooks/admin/index.ts +17 -0
- package/src/hooks/admin/inventory.ts +137 -0
- package/src/hooks/admin/orders.ts +229 -0
- package/src/hooks/admin/products.ts +116 -0
- package/src/hooks/admin/stats.ts +30 -0
- package/src/hooks/admin/variants.ts +173 -0
- package/src/hooks/admin/warehouses.ts +143 -0
- package/src/hooks/public/carts.ts +298 -0
- package/src/hooks/public/delivery-zones.ts +34 -0
- package/src/hooks/public/index.ts +10 -0
- package/src/hooks/public/orders.ts +66 -0
- package/src/hooks/public/products.ts +57 -0
- package/src/hooks/use-query-unwrapped.ts +30 -0
- package/src/hooks/useApiConfig.ts +22 -0
- package/src/index.ts +42 -0
- package/src/provider.tsx +89 -0
- package/src/rpc-client.ts +106 -0
- package/src/rpc-types.ts +121 -0
- package/src/types.ts +39 -0
- package/src/utils/query-keys.ts +121 -0
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-safe admin RPC hooks for abandoned cart management
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { useMutation, useQueryClient, UseQueryOptions, UseMutationOptions } from '@tanstack/react-query';
|
|
6
|
+
import { useQueryUnwrapped } from '../use-query-unwrapped';
|
|
7
|
+
import { createAdminRpcClients, authHeaders } from '../../rpc-client';
|
|
8
|
+
import { queryKeys } from '../../utils/query-keys';
|
|
9
|
+
import { useApiConfig } from '../useApiConfig';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Hook to list abandoned carts using admin RPC
|
|
13
|
+
*/
|
|
14
|
+
export function useListAbandonedCarts(
|
|
15
|
+
params?: { brandId?: string; hoursInactive?: number },
|
|
16
|
+
options?: Omit<UseQueryOptions<Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['abandonedCarts']['index']['$get']>>['json']>>, Error>, 'queryKey' | 'queryFn'>
|
|
17
|
+
) {
|
|
18
|
+
const { baseURL, authToken } = useApiConfig();
|
|
19
|
+
|
|
20
|
+
return useQueryUnwrapped({
|
|
21
|
+
queryKey: queryKeys.admin.abandonedCarts.list(params),
|
|
22
|
+
queryFn: async () => {
|
|
23
|
+
const clients = createAdminRpcClients(baseURL);
|
|
24
|
+
const res = await clients.abandonedCarts.index.$get({ query: params as any }, authHeaders(authToken));
|
|
25
|
+
if (!res.ok) throw new Error(`Failed to fetch abandoned carts: ${res.statusText}`);
|
|
26
|
+
return res.json();
|
|
27
|
+
},
|
|
28
|
+
...options,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Hook to get abandoned cart statistics using admin RPC
|
|
34
|
+
*/
|
|
35
|
+
export function useGetAbandonedCartStats(
|
|
36
|
+
params?: { brandId?: string },
|
|
37
|
+
options?: Omit<UseQueryOptions<Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['abandonedCarts']['stats']['$get']>>['json']>>, Error>, 'queryKey' | 'queryFn'>
|
|
38
|
+
) {
|
|
39
|
+
const { baseURL, authToken } = useApiConfig();
|
|
40
|
+
|
|
41
|
+
return useQueryUnwrapped({
|
|
42
|
+
queryKey: queryKeys.admin.abandonedCarts.stats(params?.brandId),
|
|
43
|
+
queryFn: async () => {
|
|
44
|
+
const clients = createAdminRpcClients(baseURL);
|
|
45
|
+
const res = await clients.abandonedCarts.stats.$get({ query: params }, authHeaders(authToken));
|
|
46
|
+
if (!res.ok) throw new Error(`Failed to fetch abandoned cart stats: ${res.statusText}`);
|
|
47
|
+
return res.json();
|
|
48
|
+
},
|
|
49
|
+
...options,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Hook to get specific abandoned cart details using admin RPC
|
|
55
|
+
*/
|
|
56
|
+
export function useGetAbandonedCart(
|
|
57
|
+
cartId: string,
|
|
58
|
+
options?: Omit<UseQueryOptions<Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['abandonedCarts'][':id']['$get']>>['json']>>, Error>, 'queryKey' | 'queryFn'>
|
|
59
|
+
) {
|
|
60
|
+
const { baseURL, authToken } = useApiConfig();
|
|
61
|
+
|
|
62
|
+
return useQueryUnwrapped({
|
|
63
|
+
queryKey: queryKeys.admin.abandonedCarts.detail(cartId),
|
|
64
|
+
queryFn: async () => {
|
|
65
|
+
const clients = createAdminRpcClients(baseURL);
|
|
66
|
+
const res = await clients.abandonedCarts[':id'].$get(
|
|
67
|
+
{ param: { id: cartId } },
|
|
68
|
+
authHeaders(authToken)
|
|
69
|
+
);
|
|
70
|
+
if (!res.ok) throw new Error(`Failed to fetch abandoned cart: ${res.statusText}`);
|
|
71
|
+
return res.json();
|
|
72
|
+
},
|
|
73
|
+
...options,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Hook to clean up expired carts using admin RPC
|
|
79
|
+
*/
|
|
80
|
+
export function useCleanupExpiredCarts(
|
|
81
|
+
options?: UseMutationOptions<
|
|
82
|
+
Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['abandonedCarts']['cleanup']['$post']>>['json']>>,
|
|
83
|
+
Error,
|
|
84
|
+
void
|
|
85
|
+
>
|
|
86
|
+
) {
|
|
87
|
+
const { baseURL, authToken } = useApiConfig();
|
|
88
|
+
const queryClient = useQueryClient();
|
|
89
|
+
|
|
90
|
+
return useMutation({
|
|
91
|
+
mutationFn: async () => {
|
|
92
|
+
const clients = createAdminRpcClients(baseURL);
|
|
93
|
+
const res = await clients.abandonedCarts.cleanup.$post({}, authHeaders(authToken));
|
|
94
|
+
if (!res.ok) throw new Error(`Failed to cleanup expired carts: ${res.statusText}`);
|
|
95
|
+
return res.json();
|
|
96
|
+
},
|
|
97
|
+
onSuccess: () => {
|
|
98
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.admin.abandonedCarts.all });
|
|
99
|
+
},
|
|
100
|
+
...options,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-safe admin RPC hooks for brand management
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { useMutation, useQueryClient, UseQueryOptions, UseMutationOptions } from '@tanstack/react-query';
|
|
6
|
+
import { useQueryUnwrapped } from '../use-query-unwrapped';
|
|
7
|
+
import { createAdminRpcClients, authHeaders } from '../../rpc-client';
|
|
8
|
+
import { queryKeys } from '../../utils/query-keys';
|
|
9
|
+
import { useApiConfig } from '../useApiConfig';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Hook to list all brands using admin RPC
|
|
13
|
+
*/
|
|
14
|
+
export function useListBrands(
|
|
15
|
+
options?: Omit<UseQueryOptions<Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['brands']['index']['$get']>>['json']>>, Error>, 'queryKey' | 'queryFn'>
|
|
16
|
+
) {
|
|
17
|
+
const { baseURL, authToken } = useApiConfig();
|
|
18
|
+
|
|
19
|
+
return useQueryUnwrapped({
|
|
20
|
+
queryKey: queryKeys.admin.brands.list(),
|
|
21
|
+
queryFn: async () => {
|
|
22
|
+
const clients = createAdminRpcClients(baseURL);
|
|
23
|
+
const res = await clients.brands.index.$get({}, authHeaders(authToken));
|
|
24
|
+
if (!res.ok) throw new Error(`Failed to fetch brands: ${res.statusText}`);
|
|
25
|
+
return res.json();
|
|
26
|
+
},
|
|
27
|
+
...options,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Hook to get brand by ID using admin RPC
|
|
33
|
+
*/
|
|
34
|
+
export function useGetBrand(
|
|
35
|
+
brandId: string,
|
|
36
|
+
options?: Omit<UseQueryOptions<Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['brands'][':id']['$get']>>['json']>>, Error>, 'queryKey' | 'queryFn'>
|
|
37
|
+
) {
|
|
38
|
+
const { baseURL, authToken } = useApiConfig();
|
|
39
|
+
|
|
40
|
+
return useQueryUnwrapped({
|
|
41
|
+
queryKey: queryKeys.admin.brands.detail(brandId),
|
|
42
|
+
queryFn: async () => {
|
|
43
|
+
const clients = createAdminRpcClients(baseURL);
|
|
44
|
+
const res = await clients.brands[':id'].$get({ param: { id: brandId } }, authHeaders(authToken));
|
|
45
|
+
if (!res.ok) throw new Error(`Failed to fetch brand: ${res.statusText}`);
|
|
46
|
+
return res.json();
|
|
47
|
+
},
|
|
48
|
+
...options,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Hook to create a brand using admin RPC
|
|
54
|
+
*/
|
|
55
|
+
export function useCreateBrand(
|
|
56
|
+
options?: UseMutationOptions<
|
|
57
|
+
Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['brands']['index']['$post']>>['json']>>,
|
|
58
|
+
Error,
|
|
59
|
+
any
|
|
60
|
+
>
|
|
61
|
+
) {
|
|
62
|
+
const { baseURL, authToken } = useApiConfig();
|
|
63
|
+
const queryClient = useQueryClient();
|
|
64
|
+
|
|
65
|
+
return useMutation({
|
|
66
|
+
mutationFn: async (data) => {
|
|
67
|
+
const clients = createAdminRpcClients(baseURL);
|
|
68
|
+
const res = await clients.brands.index.$post({ json: data }, authHeaders(authToken));
|
|
69
|
+
if (!res.ok) throw new Error(`Failed to create brand: ${res.statusText}`);
|
|
70
|
+
return res.json();
|
|
71
|
+
},
|
|
72
|
+
onSuccess: () => {
|
|
73
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.admin.brands.all });
|
|
74
|
+
},
|
|
75
|
+
...options,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Hook to update a brand using admin RPC
|
|
81
|
+
*/
|
|
82
|
+
export function useUpdateBrand(
|
|
83
|
+
brandId: string,
|
|
84
|
+
options?: UseMutationOptions<
|
|
85
|
+
Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['brands'][':id']['$patch']>>['json']>>,
|
|
86
|
+
Error,
|
|
87
|
+
any
|
|
88
|
+
>
|
|
89
|
+
) {
|
|
90
|
+
const { baseURL, authToken } = useApiConfig();
|
|
91
|
+
const queryClient = useQueryClient();
|
|
92
|
+
|
|
93
|
+
return useMutation({
|
|
94
|
+
mutationFn: async (data) => {
|
|
95
|
+
const clients = createAdminRpcClients(baseURL);
|
|
96
|
+
const res = await clients.brands[':id'].$patch({ json: data, param: { id: brandId } }, authHeaders(authToken));
|
|
97
|
+
if (!res.ok) throw new Error(`Failed to update brand: ${res.statusText}`);
|
|
98
|
+
return res.json();
|
|
99
|
+
},
|
|
100
|
+
onSuccess: () => {
|
|
101
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.admin.brands.detail(brandId) });
|
|
102
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.admin.brands.all });
|
|
103
|
+
},
|
|
104
|
+
...options,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Hook to delete a brand using admin RPC
|
|
110
|
+
*/
|
|
111
|
+
export function useDeleteBrand(
|
|
112
|
+
brandId: string,
|
|
113
|
+
options?: UseMutationOptions<
|
|
114
|
+
Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['brands'][':id']['$delete']>>['json']>>,
|
|
115
|
+
Error,
|
|
116
|
+
void
|
|
117
|
+
>
|
|
118
|
+
) {
|
|
119
|
+
const { baseURL, authToken } = useApiConfig();
|
|
120
|
+
const queryClient = useQueryClient();
|
|
121
|
+
|
|
122
|
+
return useMutation({
|
|
123
|
+
mutationFn: async () => {
|
|
124
|
+
const clients = createAdminRpcClients(baseURL);
|
|
125
|
+
const res = await clients.brands[':id'].$delete({ param: { id: brandId } }, authHeaders(authToken));
|
|
126
|
+
if (!res.ok) throw new Error(`Failed to delete brand: ${res.statusText}`);
|
|
127
|
+
return res.json();
|
|
128
|
+
},
|
|
129
|
+
onSuccess: () => {
|
|
130
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.admin.brands.all });
|
|
131
|
+
},
|
|
132
|
+
...options,
|
|
133
|
+
});
|
|
134
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-safe admin RPC hooks for customer management
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { UseQueryOptions } from '@tanstack/react-query';
|
|
6
|
+
import { useQueryUnwrapped } from '../use-query-unwrapped';
|
|
7
|
+
import { createAdminRpcClients, authHeaders } from '../../rpc-client';
|
|
8
|
+
import { queryKeys } from '../../utils/query-keys';
|
|
9
|
+
import { useApiConfig } from '../useApiConfig';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Hook to get customer order history by phone using admin RPC
|
|
13
|
+
*/
|
|
14
|
+
export function useGetCustomerHistory(
|
|
15
|
+
phone: string,
|
|
16
|
+
options?: Omit<UseQueryOptions<Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['customers']['history']['$get']>>['json']>>, Error>, 'queryKey' | 'queryFn'>
|
|
17
|
+
) {
|
|
18
|
+
const { baseURL, authToken } = useApiConfig();
|
|
19
|
+
|
|
20
|
+
return useQueryUnwrapped({
|
|
21
|
+
queryKey: queryKeys.admin.customers.history(phone),
|
|
22
|
+
queryFn: async () => {
|
|
23
|
+
const clients = createAdminRpcClients(baseURL);
|
|
24
|
+
const res = await clients.customers.history.$get({ query: { phone } }, authHeaders(authToken));
|
|
25
|
+
if (!res.ok) throw new Error(`Failed to fetch customer history: ${res.statusText}`);
|
|
26
|
+
return res.json();
|
|
27
|
+
},
|
|
28
|
+
enabled: !!phone,
|
|
29
|
+
...options,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-safe admin RPC hooks for delivery zone and state management
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { useMutation, useQueryClient, UseQueryOptions, UseMutationOptions } from '@tanstack/react-query';
|
|
6
|
+
import { useQueryUnwrapped } from '../use-query-unwrapped';
|
|
7
|
+
import { createAdminRpcClients, authHeaders } from '../../rpc-client';
|
|
8
|
+
import { queryKeys } from '../../utils/query-keys';
|
|
9
|
+
import { useApiConfig } from '../useApiConfig';
|
|
10
|
+
|
|
11
|
+
// ============ STATES ============
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Hook to list all states using admin RPC
|
|
15
|
+
*/
|
|
16
|
+
export function useListStates(
|
|
17
|
+
options?: Omit<UseQueryOptions<Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['deliveryZones']['states']['$get']>>['json']>>, Error>, 'queryKey' | 'queryFn'>
|
|
18
|
+
) {
|
|
19
|
+
const { baseURL, authToken } = useApiConfig();
|
|
20
|
+
|
|
21
|
+
return useQueryUnwrapped({
|
|
22
|
+
queryKey: queryKeys.admin.deliveryZones.states.list(),
|
|
23
|
+
queryFn: async () => {
|
|
24
|
+
const clients = createAdminRpcClients(baseURL);
|
|
25
|
+
const res = await clients.deliveryZones.states.$get({}, authHeaders(authToken));
|
|
26
|
+
if (!res.ok) throw new Error(`Failed to fetch states: ${res.statusText}`);
|
|
27
|
+
return res.json();
|
|
28
|
+
},
|
|
29
|
+
...options,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Hook to create a state using admin RPC
|
|
35
|
+
*/
|
|
36
|
+
export function useCreateState(
|
|
37
|
+
options?: UseMutationOptions<
|
|
38
|
+
Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['deliveryZones']['states']['$post']>>['json']>>,
|
|
39
|
+
Error,
|
|
40
|
+
any
|
|
41
|
+
>
|
|
42
|
+
) {
|
|
43
|
+
const { baseURL, authToken } = useApiConfig();
|
|
44
|
+
const queryClient = useQueryClient();
|
|
45
|
+
|
|
46
|
+
return useMutation({
|
|
47
|
+
mutationFn: async (data) => {
|
|
48
|
+
const clients = createAdminRpcClients(baseURL);
|
|
49
|
+
const res = await clients.deliveryZones.states.$post({ json: data }, authHeaders(authToken));
|
|
50
|
+
if (!res.ok) throw new Error(`Failed to create state: ${res.statusText}`);
|
|
51
|
+
return res.json();
|
|
52
|
+
},
|
|
53
|
+
onSuccess: () => {
|
|
54
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.admin.deliveryZones.states.all });
|
|
55
|
+
},
|
|
56
|
+
...options,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Hook to update a state using admin RPC
|
|
62
|
+
*/
|
|
63
|
+
export function useUpdateState(
|
|
64
|
+
stateId: string,
|
|
65
|
+
options?: UseMutationOptions<
|
|
66
|
+
Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['deliveryZones']['states'][':id']['$patch']>>['json']>>,
|
|
67
|
+
Error,
|
|
68
|
+
any
|
|
69
|
+
>
|
|
70
|
+
) {
|
|
71
|
+
const { baseURL, authToken } = useApiConfig();
|
|
72
|
+
const queryClient = useQueryClient();
|
|
73
|
+
|
|
74
|
+
return useMutation({
|
|
75
|
+
mutationFn: async (data) => {
|
|
76
|
+
const clients = createAdminRpcClients(baseURL);
|
|
77
|
+
const res = await clients.deliveryZones.states[':id'].$patch(
|
|
78
|
+
{ json: data, param: { id: stateId } },
|
|
79
|
+
authHeaders(authToken)
|
|
80
|
+
);
|
|
81
|
+
if (!res.ok) throw new Error(`Failed to update state: ${res.statusText}`);
|
|
82
|
+
return res.json();
|
|
83
|
+
},
|
|
84
|
+
onSuccess: () => {
|
|
85
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.admin.deliveryZones.states.detail(stateId) });
|
|
86
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.admin.deliveryZones.states.all });
|
|
87
|
+
},
|
|
88
|
+
...options,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Hook to delete a state using admin RPC
|
|
94
|
+
*/
|
|
95
|
+
export function useDeleteState(
|
|
96
|
+
stateId: string,
|
|
97
|
+
options?: UseMutationOptions<
|
|
98
|
+
Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['deliveryZones']['states'][':id']['$delete']>>['json']>>,
|
|
99
|
+
Error,
|
|
100
|
+
void
|
|
101
|
+
>
|
|
102
|
+
) {
|
|
103
|
+
const { baseURL, authToken } = useApiConfig();
|
|
104
|
+
const queryClient = useQueryClient();
|
|
105
|
+
|
|
106
|
+
return useMutation({
|
|
107
|
+
mutationFn: async () => {
|
|
108
|
+
const clients = createAdminRpcClients(baseURL);
|
|
109
|
+
const res = await clients.deliveryZones.states[':id'].$delete(
|
|
110
|
+
{ param: { id: stateId } },
|
|
111
|
+
authHeaders(authToken)
|
|
112
|
+
);
|
|
113
|
+
if (!res.ok) throw new Error(`Failed to delete state: ${res.statusText}`);
|
|
114
|
+
return res.json();
|
|
115
|
+
},
|
|
116
|
+
onSuccess: () => {
|
|
117
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.admin.deliveryZones.states.all });
|
|
118
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.admin.deliveryZones.zones.all });
|
|
119
|
+
},
|
|
120
|
+
...options,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// ============ DELIVERY ZONES ============
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Hook to list delivery zones using admin RPC
|
|
128
|
+
*/
|
|
129
|
+
export function useListDeliveryZones(
|
|
130
|
+
params?: { stateId?: string; brandId?: string },
|
|
131
|
+
options?: Omit<UseQueryOptions<Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['deliveryZones']['zones']['$get']>>['json']>>, Error>, 'queryKey' | 'queryFn'>
|
|
132
|
+
) {
|
|
133
|
+
const { baseURL, authToken } = useApiConfig();
|
|
134
|
+
|
|
135
|
+
return useQueryUnwrapped({
|
|
136
|
+
queryKey: queryKeys.admin.deliveryZones.zones.list(params),
|
|
137
|
+
queryFn: async () => {
|
|
138
|
+
const clients = createAdminRpcClients(baseURL);
|
|
139
|
+
const res = await clients.deliveryZones.zones.$get({ query: params as any }, authHeaders(authToken));
|
|
140
|
+
if (!res.ok) throw new Error(`Failed to fetch delivery zones: ${res.statusText}`);
|
|
141
|
+
return res.json();
|
|
142
|
+
},
|
|
143
|
+
...options,
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Hook to create a delivery zone using admin RPC
|
|
149
|
+
*/
|
|
150
|
+
export function useCreateDeliveryZone(
|
|
151
|
+
options?: UseMutationOptions<
|
|
152
|
+
Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['deliveryZones']['zones']['$post']>>['json']>>,
|
|
153
|
+
Error,
|
|
154
|
+
any
|
|
155
|
+
>
|
|
156
|
+
) {
|
|
157
|
+
const { baseURL, authToken } = useApiConfig();
|
|
158
|
+
const queryClient = useQueryClient();
|
|
159
|
+
|
|
160
|
+
return useMutation({
|
|
161
|
+
mutationFn: async (data) => {
|
|
162
|
+
const clients = createAdminRpcClients(baseURL);
|
|
163
|
+
const res = await clients.deliveryZones.zones.$post({ json: data }, authHeaders(authToken));
|
|
164
|
+
if (!res.ok) throw new Error(`Failed to create delivery zone: ${res.statusText}`);
|
|
165
|
+
return res.json();
|
|
166
|
+
},
|
|
167
|
+
onSuccess: () => {
|
|
168
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.admin.deliveryZones.zones.all });
|
|
169
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.admin.deliveryZones.states.all });
|
|
170
|
+
},
|
|
171
|
+
...options,
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Hook to update a delivery zone using admin RPC
|
|
177
|
+
*/
|
|
178
|
+
export function useUpdateDeliveryZone(
|
|
179
|
+
zoneId: string,
|
|
180
|
+
options?: UseMutationOptions<
|
|
181
|
+
Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['deliveryZones']['zones'][':id']['$patch']>>['json']>>,
|
|
182
|
+
Error,
|
|
183
|
+
any
|
|
184
|
+
>
|
|
185
|
+
) {
|
|
186
|
+
const { baseURL, authToken } = useApiConfig();
|
|
187
|
+
const queryClient = useQueryClient();
|
|
188
|
+
|
|
189
|
+
return useMutation({
|
|
190
|
+
mutationFn: async (data) => {
|
|
191
|
+
const clients = createAdminRpcClients(baseURL);
|
|
192
|
+
const res = await clients.deliveryZones.zones[':id'].$patch(
|
|
193
|
+
{ json: data, param: { id: zoneId } },
|
|
194
|
+
authHeaders(authToken)
|
|
195
|
+
);
|
|
196
|
+
if (!res.ok) throw new Error(`Failed to update delivery zone: ${res.statusText}`);
|
|
197
|
+
return res.json();
|
|
198
|
+
},
|
|
199
|
+
onSuccess: () => {
|
|
200
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.admin.deliveryZones.zones.detail(zoneId) });
|
|
201
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.admin.deliveryZones.zones.all });
|
|
202
|
+
},
|
|
203
|
+
...options,
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Hook to delete a delivery zone using admin RPC
|
|
209
|
+
*/
|
|
210
|
+
export function useDeleteDeliveryZone(
|
|
211
|
+
zoneId: string,
|
|
212
|
+
options?: UseMutationOptions<
|
|
213
|
+
Awaited<ReturnType<Awaited<ReturnType<ReturnType<typeof createAdminRpcClients>['deliveryZones']['zones'][':id']['$delete']>>['json']>>,
|
|
214
|
+
Error,
|
|
215
|
+
void
|
|
216
|
+
>
|
|
217
|
+
) {
|
|
218
|
+
const { baseURL, authToken } = useApiConfig();
|
|
219
|
+
const queryClient = useQueryClient();
|
|
220
|
+
|
|
221
|
+
return useMutation({
|
|
222
|
+
mutationFn: async () => {
|
|
223
|
+
const clients = createAdminRpcClients(baseURL);
|
|
224
|
+
const res = await clients.deliveryZones.zones[':id'].$delete(
|
|
225
|
+
{ param: { id: zoneId } },
|
|
226
|
+
authHeaders(authToken)
|
|
227
|
+
);
|
|
228
|
+
if (!res.ok) throw new Error(`Failed to delete delivery zone: ${res.statusText}`);
|
|
229
|
+
return res.json();
|
|
230
|
+
},
|
|
231
|
+
onSuccess: () => {
|
|
232
|
+
queryClient.invalidateQueries({ queryKey: queryKeys.admin.deliveryZones.zones.all });
|
|
233
|
+
},
|
|
234
|
+
...options,
|
|
235
|
+
});
|
|
236
|
+
}
|