-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dashboard): Migrate to new hooks and API client (#6963)
- Loading branch information
1 parent
5ba74ec
commit 8a5c692
Showing
195 changed files
with
3,932 additions
and
6,041 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...xt/dashboard/src/components/table/table-cells/product/collection-cell/collection-cell.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 2 additions & 3 deletions
5
...min-next/dashboard/src/components/table/table-cells/product/product-cell/product-cell.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...oard/src/components/table/table-cells/product/sales-channels-cell/sales-channels-cell.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...min-next/dashboard/src/components/table/table-cells/product/variant-cell/variant-cell.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
packages/admin-next/dashboard/src/hooks/api/api-keys.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { | ||
MutationOptions, | ||
QueryKey, | ||
UseMutationOptions, | ||
UseQueryOptions, | ||
useMutation, | ||
useQuery, | ||
} from "@tanstack/react-query" | ||
import { client } from "../../lib/client" | ||
import { queryClient } from "../../lib/medusa" | ||
import { queryKeysFactory } from "../../lib/query-key-factory" | ||
import { CreateApiKeyReq, UpdateApiKeyReq } from "../../types/api-payloads" | ||
import { | ||
ApiKeyDeleteRes, | ||
ApiKeyListRes, | ||
ApiKeyRes, | ||
} from "../../types/api-responses" | ||
|
||
const API_KEYS_QUERY_KEY = "api_keys" as const | ||
export const apiKeysQueryKeys = queryKeysFactory(API_KEYS_QUERY_KEY) | ||
|
||
export const useApiKey = ( | ||
id: string, | ||
query?: Record<string, any>, | ||
options?: Omit< | ||
UseQueryOptions<ApiKeyRes, Error, ApiKeyRes, QueryKey>, | ||
"queryKey" | "queryFn" | ||
> | ||
) => { | ||
const { data, ...rest } = useQuery({ | ||
queryFn: () => client.apiKeys.retrieve(id, query), | ||
queryKey: apiKeysQueryKeys.detail(id), | ||
...options, | ||
}) | ||
|
||
return { ...data, ...rest } | ||
} | ||
|
||
export const useApiKeys = ( | ||
query?: Record<string, any>, | ||
options?: Omit< | ||
UseQueryOptions<ApiKeyListRes, Error, ApiKeyListRes, QueryKey>, | ||
"queryKey" | "queryFn" | ||
> | ||
) => { | ||
const { data, ...rest } = useQuery({ | ||
queryFn: () => client.apiKeys.list(query), | ||
queryKey: apiKeysQueryKeys.list(query), | ||
...options, | ||
}) | ||
|
||
return { ...data, ...rest } | ||
} | ||
|
||
export const useCreateApiKey = ( | ||
options?: UseMutationOptions<ApiKeyRes, Error, CreateApiKeyReq> | ||
) => { | ||
return useMutation({ | ||
mutationFn: (payload) => client.apiKeys.create(payload), | ||
onSuccess: (data, variables, context) => { | ||
queryClient.invalidateQueries({ queryKey: apiKeysQueryKeys.lists() }) | ||
|
||
options?.onSuccess?.(data, variables, context) | ||
}, | ||
...options, | ||
}) | ||
} | ||
|
||
export const useUpdateApiKey = ( | ||
id: string, | ||
options?: UseMutationOptions<ApiKeyRes, Error, UpdateApiKeyReq> | ||
) => { | ||
return useMutation({ | ||
mutationFn: (payload) => client.apiKeys.update(id, payload), | ||
onSuccess: (data, variables, context) => { | ||
queryClient.invalidateQueries({ queryKey: apiKeysQueryKeys.lists() }) | ||
queryClient.invalidateQueries({ queryKey: apiKeysQueryKeys.detail(id) }) | ||
|
||
options?.onSuccess?.(data, variables, context) | ||
}, | ||
...options, | ||
}) | ||
} | ||
|
||
export const useRevokeApiKey = ( | ||
id: string, | ||
options?: UseMutationOptions<ApiKeyRes, Error, void> | ||
) => { | ||
return useMutation({ | ||
mutationFn: () => client.apiKeys.revoke(id), | ||
onSuccess: (data, variables, context) => { | ||
queryClient.invalidateQueries({ queryKey: apiKeysQueryKeys.lists() }) | ||
queryClient.invalidateQueries({ queryKey: apiKeysQueryKeys.detail(id) }) | ||
|
||
options?.onSuccess?.(data, variables, context) | ||
}, | ||
}) | ||
} | ||
|
||
export const useDeleteApiKey = ( | ||
id: string, | ||
options?: MutationOptions<ApiKeyDeleteRes, Error, void> | ||
) => { | ||
return useMutation({ | ||
mutationFn: () => client.apiKeys.delete(id), | ||
onSuccess: (data, variables, context) => { | ||
queryClient.invalidateQueries({ queryKey: apiKeysQueryKeys.lists() }) | ||
queryClient.invalidateQueries({ queryKey: apiKeysQueryKeys.detail(id) }) | ||
|
||
options?.onSuccess?.(data, variables, context) | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { UseMutationOptions, useMutation } from "@tanstack/react-query" | ||
|
||
import { client } from "../../lib/client" | ||
import { EmailPassReq } from "../../types/api-payloads" | ||
import { EmailPassRes } from "../../types/api-responses" | ||
|
||
export const useEmailPassLogin = ( | ||
options?: UseMutationOptions<EmailPassRes, Error, EmailPassReq> | ||
) => { | ||
return useMutation({ | ||
mutationFn: (payload) => client.auth.authenticate.emailPass(payload), | ||
onSuccess: async (data: { token: string }, variables, context) => { | ||
const { token } = data | ||
|
||
// Create a new session with the token | ||
await client.auth.login(token) | ||
|
||
options?.onSuccess?.(data, variables, context) | ||
}, | ||
...options, | ||
}) | ||
} |
Oops, something went wrong.