Raw
1 import { FetchError } from "@medusajs/js-sdk"
2 import { HttpTypes } from "@medusajs/types"
3 import {
4 QueryKey,
5 UseMutationOptions,
6 UseQueryOptions,
7 useMutation,
8 useQuery,
9 } from "@tanstack/react-query"
10 import { sdk } from "../../lib/client"
11 import { queryClient } from "../../lib/query-client"
12 import { queryKeysFactory } from "../../lib/query-key-factory"
13 import { customersQueryKeys } from "./customers"
14
15 const CUSTOMER_GROUPS_QUERY_KEY = "customer_groups" as const
16 export const customerGroupsQueryKeys = queryKeysFactory(
17 CUSTOMER_GROUPS_QUERY_KEY
18 )
19
20 export const useCustomerGroup = (
21 id: string,
22 query?: HttpTypes.AdminGetCustomerGroupParams,
23 options?: Omit<
24 UseQueryOptions<
25 HttpTypes.AdminCustomerGroupResponse,
26 FetchError,
27 HttpTypes.AdminCustomerGroupResponse,
28 QueryKey
29 >,
30 "queryFn" | "queryKey"
31 >
32 ) => {
33 const { data, ...rest } = useQuery({
34 queryKey: customerGroupsQueryKeys.detail(id, query),
35 queryFn: async () => sdk.admin.customerGroup.retrieve(id, query),
36 ...options,
37 })
38
39 return { ...data, ...rest }
40 }
41
42 export const useCustomerGroups = (
43 query?: HttpTypes.AdminGetCustomerGroupsParams,
44 options?: Omit<
45 UseQueryOptions<
46 HttpTypes.AdminGetCustomerGroupsParams,
47 FetchError,
48 HttpTypes.AdminCustomerGroupListResponse,
49 QueryKey
50 >,
51 "queryFn" | "queryKey"
52 >
53 ) => {
54 const { data, ...rest } = useQuery({
55 queryFn: () => sdk.admin.customerGroup.list(query),
56 queryKey: customerGroupsQueryKeys.list(query),
57 ...options,
58 })
59
60 return { ...data, ...rest }
61 }
62
63 export const useCreateCustomerGroup = (
64 options?: UseMutationOptions<
65 HttpTypes.AdminCustomerGroupResponse,
66 FetchError,
67 HttpTypes.AdminCreateCustomerGroup
68 >
69 ) => {
70 return useMutation({
71 mutationFn: (payload) => sdk.admin.customerGroup.create(payload),
72 onSuccess: (data, variables, context) => {
73 queryClient.invalidateQueries({
74 queryKey: customerGroupsQueryKeys.lists(),
75 })
76 options?.onSuccess?.(data, variables, context)
77 },
78 ...options,
79 })
80 }
81
82 export const useUpdateCustomerGroup = (
83 id: string,
84 options?: UseMutationOptions<
85 HttpTypes.AdminCustomerGroupResponse,
86 FetchError,
87 HttpTypes.AdminUpdateCustomerGroup
88 >
89 ) => {
90 return useMutation({
91 mutationFn: (payload) => sdk.admin.customerGroup.update(id, payload),
92 onSuccess: (data, variables, context) => {
93 queryClient.invalidateQueries({
94 queryKey: customerGroupsQueryKeys.lists(),
95 })
96 queryClient.invalidateQueries({
97 queryKey: customerGroupsQueryKeys.detail(id),
98 })
99
100 options?.onSuccess?.(data, variables, context)
101 },
102 ...options,
103 })
104 }
105
106 export const useDeleteCustomerGroup = (
107 id: string,
108 options?: UseMutationOptions<
109 HttpTypes.AdminCustomerGroupDeleteResponse,
110 FetchError,
111 void
112 >
113 ) => {
114 return useMutation({
115 mutationFn: () => sdk.admin.customerGroup.delete(id),
116 onSuccess: (data, variables, context) => {
117 queryClient.invalidateQueries({
118 queryKey: customerGroupsQueryKeys.lists(),
119 })
120 queryClient.invalidateQueries({
121 queryKey: customerGroupsQueryKeys.detail(id),
122 })
123
124 options?.onSuccess?.(data, variables, context)
125 },
126 ...options,
127 })
128 }
129
130 export const useDeleteCustomerGroupLazy = (
131 options?: UseMutationOptions<
132 HttpTypes.AdminCustomerGroupDeleteResponse,
133 FetchError,
134 { id: string }
135 >
136 ) => {
137 return useMutation({
138 mutationFn: ({ id }) => sdk.admin.customerGroup.delete(id),
139 onSuccess: (data, variables, context) => {
140 queryClient.invalidateQueries({
141 queryKey: customerGroupsQueryKeys.lists(),
142 })
143 queryClient.invalidateQueries({
144 queryKey: customerGroupsQueryKeys.detail(variables.id),
145 })
146
147 options?.onSuccess?.(data, variables, context)
148 },
149 ...options,
150 })
151 }
152
153 export const useAddCustomersToGroup = (
154 id: string,
155 options?: UseMutationOptions<
156 HttpTypes.AdminCustomerGroupResponse,
157 FetchError,
158 HttpTypes.AdminBatchLink["add"]
159 >
160 ) => {
161 return useMutation({
162 mutationFn: (payload) =>
163 sdk.admin.customerGroup.batchCustomers(id, { add: payload }),
164 onSuccess: (data, variables, context) => {
165 queryClient.invalidateQueries({
166 queryKey: customerGroupsQueryKeys.lists(),
167 })
168 queryClient.invalidateQueries({
169 queryKey: customerGroupsQueryKeys.detail(id),
170 })
171 queryClient.invalidateQueries({
172 queryKey: customersQueryKeys.lists(),
173 })
174
175 options?.onSuccess?.(data, variables, context)
176 },
177 ...options,
178 })
179 }
180
181 export const useRemoveCustomersFromGroup = (
182 id: string,
183 options?: UseMutationOptions<
184 HttpTypes.AdminCustomerGroupResponse,
185 FetchError,
186 HttpTypes.AdminBatchLink["remove"]
187 >
188 ) => {
189 return useMutation({
190 mutationFn: (payload) =>
191 sdk.admin.customerGroup.batchCustomers(id, { remove: payload }),
192 onSuccess: (data, variables, context) => {
193 queryClient.invalidateQueries({
194 queryKey: customerGroupsQueryKeys.lists(),
195 })
196 queryClient.invalidateQueries({
197 queryKey: customerGroupsQueryKeys.detail(id),
198 })
199 queryClient.invalidateQueries({
200 queryKey: customersQueryKeys.lists(),
201 })
202
203 options?.onSuccess?.(data, variables, context)
204 },
205 ...options,
206 })
207 }