Raw
1 import { FetchError } from "@medusajs/js-sdk"
2 import { HttpTypes } from "@medusajs/types"
3 import {
4 QueryKey,
5 useMutation,
6 UseMutationOptions,
7 useQuery,
8 UseQueryOptions,
9 } from "@tanstack/react-query"
10
11 import { sdk } from "../../lib/client"
12 import { queryClient } from "../../lib/query-client"
13 import { queryKeysFactory } from "../../lib/query-key-factory"
14 import { viewsQueryKeys } from "./views"
15
16 const PROPERTY_LABELS_QUERY_KEY = "property_labels" as const
17 export const propertyLabelsQueryKeys = queryKeysFactory(
18 PROPERTY_LABELS_QUERY_KEY
19 )
20
21 export const usePropertyLabels = (
22 query?: HttpTypes.AdminGetPropertyLabelsParams,
23 options?: Omit<
24 UseQueryOptions<
25 HttpTypes.AdminPropertyLabelListResponse,
26 FetchError,
27 HttpTypes.AdminPropertyLabelListResponse,
28 QueryKey
29 >,
30 "queryFn" | "queryKey"
31 >
32 ) => {
33 const { data, ...rest } = useQuery({
34 queryFn: () => sdk.admin.propertyLabel.list(query),
35 queryKey: propertyLabelsQueryKeys.list(query),
36 ...options,
37 })
38
39 return { ...data, ...rest }
40 }
41
42 export const usePropertyLabel = (
43 id: string,
44 query?: HttpTypes.AdminGetPropertyLabelParams,
45 options?: Omit<
46 UseQueryOptions<
47 HttpTypes.AdminPropertyLabelResponse,
48 FetchError,
49 HttpTypes.AdminPropertyLabelResponse,
50 QueryKey
51 >,
52 "queryFn" | "queryKey"
53 >
54 ) => {
55 const { data, ...rest } = useQuery({
56 queryFn: () => sdk.admin.propertyLabel.retrieve(id, query),
57 queryKey: propertyLabelsQueryKeys.detail(id, query),
58 ...options,
59 })
60
61 return { ...data, ...rest }
62 }
63
64 export const useCreatePropertyLabel = (
65 options?: UseMutationOptions<
66 HttpTypes.AdminPropertyLabelResponse,
67 FetchError,
68 HttpTypes.AdminCreatePropertyLabel
69 >
70 ) => {
71 return useMutation({
72 mutationFn: (payload) => sdk.admin.propertyLabel.create(payload),
73 onSuccess: (data, variables, context) => {
74 queryClient.invalidateQueries({ queryKey: propertyLabelsQueryKeys.all })
75 queryClient.invalidateQueries({
76 queryKey: viewsQueryKeys.columns(data.property_label.entity),
77 })
78 queryClient.invalidateQueries({ queryKey: viewsQueryKeys.entities() })
79 options?.onSuccess?.(data, variables, context)
80 },
81 ...options,
82 })
83 }
84
85 export const useUpdatePropertyLabel = (
86 id: string,
87 options?: UseMutationOptions<
88 HttpTypes.AdminPropertyLabelResponse,
89 FetchError,
90 Omit<HttpTypes.AdminUpdatePropertyLabel, "id">
91 >
92 ) => {
93 return useMutation({
94 mutationFn: (payload) => sdk.admin.propertyLabel.update(id, payload),
95 onSuccess: (data, variables, context) => {
96 queryClient.invalidateQueries({ queryKey: propertyLabelsQueryKeys.all })
97 queryClient.invalidateQueries({
98 queryKey: viewsQueryKeys.columns(data.property_label.entity),
99 })
100 options?.onSuccess?.(data, variables, context)
101 },
102 ...options,
103 })
104 }
105
106 export const useDeletePropertyLabel = (
107 id: string,
108 options?: UseMutationOptions<
109 HttpTypes.AdminPropertyLabelDeleteResponse,
110 FetchError,
111 void
112 >
113 ) => {
114 return useMutation({
115 mutationFn: () => sdk.admin.propertyLabel.delete(id),
116 onSuccess: (data, variables, context) => {
117 queryClient.invalidateQueries({ queryKey: propertyLabelsQueryKeys.all })
118 queryClient.invalidateQueries({
119 queryKey: viewsQueryKeys.columns(),
120 })
121 queryClient.invalidateQueries({ queryKey: viewsQueryKeys.entities() })
122 options?.onSuccess?.(data, variables, context)
123 },
124 ...options,
125 })
126 }
127
128 export const useBatchPropertyLabels = (
129 options?: UseMutationOptions<
130 HttpTypes.AdminBatchPropertyLabelResponse,
131 FetchError,
132 HttpTypes.AdminBatchPropertyLabels
133 >
134 ) => {
135 return useMutation({
136 mutationFn: (payload) => sdk.admin.propertyLabel.batch(payload),
137 onSuccess: (data, variables, context) => {
138 queryClient.invalidateQueries({ queryKey: propertyLabelsQueryKeys.all })
139 queryClient.invalidateQueries({
140 queryKey: viewsQueryKeys.columns(),
141 })
142 queryClient.invalidateQueries({ queryKey: viewsQueryKeys.entities() })
143 options?.onSuccess?.(data, variables, context)
144 },
145 ...options,
146 })
147 }