|
1
|
import { FetchError } from "@medusajs/js-sdk" |
|
2
|
import { HttpTypes, LinkMethodRequest } 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 { promotionsQueryKeys } from "./promotions" |
|
14
|
|
|
15
|
const REGIONS_QUERY_KEY = "campaigns" as const |
|
16
|
export const campaignsQueryKeys = queryKeysFactory(REGIONS_QUERY_KEY) |
|
17
|
|
|
18
|
export const useCampaign = ( |
|
19
|
id: string, |
|
20
|
query?: HttpTypes.AdminGetCampaignParams, |
|
21
|
options?: Omit< |
|
22
|
UseQueryOptions< |
|
23
|
HttpTypes.AdminCampaignResponse, |
|
24
|
FetchError, |
|
25
|
HttpTypes.AdminCampaignResponse, |
|
26
|
QueryKey |
|
27
|
>, |
|
28
|
"queryFn" | "queryKey" |
|
29
|
> |
|
30
|
) => { |
|
31
|
const { data, ...rest } = useQuery({ |
|
32
|
queryKey: campaignsQueryKeys.detail(id), |
|
33
|
queryFn: async () => sdk.admin.campaign.retrieve(id, query), |
|
34
|
...options, |
|
35
|
}) |
|
36
|
|
|
37
|
return { ...data, ...rest } |
|
38
|
} |
|
39
|
|
|
40
|
export const useCampaigns = ( |
|
41
|
query?: HttpTypes.AdminGetCampaignsParams, |
|
42
|
options?: Omit< |
|
43
|
UseQueryOptions< |
|
44
|
HttpTypes.AdminCampaignListResponse, |
|
45
|
FetchError, |
|
46
|
HttpTypes.AdminCampaignListResponse, |
|
47
|
QueryKey |
|
48
|
>, |
|
49
|
"queryFn" | "queryKey" |
|
50
|
> |
|
51
|
) => { |
|
52
|
const { data, ...rest } = useQuery({ |
|
53
|
queryFn: () => sdk.admin.campaign.list(query), |
|
54
|
queryKey: campaignsQueryKeys.list(query), |
|
55
|
...options, |
|
56
|
}) |
|
57
|
|
|
58
|
return { ...data, ...rest } |
|
59
|
} |
|
60
|
|
|
61
|
export const useCreateCampaign = ( |
|
62
|
options?: UseMutationOptions< |
|
63
|
HttpTypes.AdminCampaignResponse, |
|
64
|
FetchError, |
|
65
|
HttpTypes.AdminCreateCampaign |
|
66
|
> |
|
67
|
) => { |
|
68
|
return useMutation({ |
|
69
|
mutationFn: (payload) => sdk.admin.campaign.create(payload), |
|
70
|
onSuccess: (data, variables, context) => { |
|
71
|
queryClient.invalidateQueries({ queryKey: campaignsQueryKeys.lists() }) |
|
72
|
options?.onSuccess?.(data, variables, context) |
|
73
|
}, |
|
74
|
...options, |
|
75
|
}) |
|
76
|
} |
|
77
|
|
|
78
|
export const useUpdateCampaign = ( |
|
79
|
id: string, |
|
80
|
options?: UseMutationOptions< |
|
81
|
HttpTypes.AdminCampaignResponse, |
|
82
|
FetchError, |
|
83
|
HttpTypes.AdminUpdateCampaign |
|
84
|
> |
|
85
|
) => { |
|
86
|
return useMutation({ |
|
87
|
mutationFn: (payload) => sdk.admin.campaign.update(id, payload), |
|
88
|
onSuccess: (data, variables, context) => { |
|
89
|
queryClient.invalidateQueries({ queryKey: campaignsQueryKeys.lists() }) |
|
90
|
queryClient.invalidateQueries({ queryKey: campaignsQueryKeys.details() }) |
|
91
|
queryClient.invalidateQueries({ queryKey: promotionsQueryKeys.lists() }) |
|
92
|
queryClient.invalidateQueries({ queryKey: promotionsQueryKeys.details() }) |
|
93
|
|
|
94
|
options?.onSuccess?.(data, variables, context) |
|
95
|
}, |
|
96
|
...options, |
|
97
|
}) |
|
98
|
} |
|
99
|
|
|
100
|
export const useDeleteCampaign = ( |
|
101
|
id: string, |
|
102
|
options?: UseMutationOptions< |
|
103
|
HttpTypes.DeleteResponse<"campaign">, |
|
104
|
FetchError, |
|
105
|
void |
|
106
|
> |
|
107
|
) => { |
|
108
|
return useMutation({ |
|
109
|
mutationFn: () => sdk.admin.campaign.delete(id), |
|
110
|
onSuccess: (data, variables, context) => { |
|
111
|
queryClient.invalidateQueries({ queryKey: campaignsQueryKeys.lists() }) |
|
112
|
queryClient.invalidateQueries({ queryKey: campaignsQueryKeys.details() }) |
|
113
|
|
|
114
|
options?.onSuccess?.(data, variables, context) |
|
115
|
}, |
|
116
|
...options, |
|
117
|
}) |
|
118
|
} |
|
119
|
|
|
120
|
export const useAddOrRemoveCampaignPromotions = ( |
|
121
|
id: string, |
|
122
|
options?: UseMutationOptions< |
|
123
|
HttpTypes.AdminCampaignResponse, |
|
124
|
FetchError, |
|
125
|
LinkMethodRequest |
|
126
|
> |
|
127
|
) => { |
|
128
|
return useMutation({ |
|
129
|
mutationFn: (payload) => sdk.admin.campaign.batchPromotions(id, payload), |
|
130
|
onSuccess: (data, variables, context) => { |
|
131
|
queryClient.invalidateQueries({ queryKey: campaignsQueryKeys.details() }) |
|
132
|
queryClient.invalidateQueries({ queryKey: promotionsQueryKeys.lists() }) |
|
133
|
queryClient.invalidateQueries({ queryKey: promotionsQueryKeys.details() }) |
|
134
|
options?.onSuccess?.(data, variables, context) |
|
135
|
}, |
|
136
|
...options, |
|
137
|
}) |
|
138
|
} |