-
-
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: Add product routes and components to v2 in admin-next
- Loading branch information
Showing
81 changed files
with
597 additions
and
144 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
1 change: 1 addition & 0 deletions
1
packages/admin-next/dashboard/src/components/common/list/index.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 @@ | ||
export * from "./list" |
54 changes: 54 additions & 0 deletions
54
packages/admin-next/dashboard/src/components/common/list/list.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,54 @@ | ||
import { Checkbox, Text } from "@medusajs/ui" | ||
|
||
export interface ListProps<T> { | ||
options: { title: string; value: T }[] | ||
value?: T[] | ||
onChange?: (value: T[]) => void | ||
compare?: (a: T, b: T) => boolean | ||
disabled?: boolean | ||
} | ||
|
||
export const List = <T extends any>({ | ||
options, | ||
onChange, | ||
value, | ||
compare, | ||
disabled, | ||
}: ListProps<T>) => { | ||
if (options.length === 0) { | ||
return <div>No options</div> | ||
} | ||
|
||
return ( | ||
<div className="flex-row justify-center border divide-y rounded-lg"> | ||
{options.map((option) => { | ||
return ( | ||
<div className="flex p-4 gap-x-4"> | ||
{onChange && value !== undefined && ( | ||
<Checkbox | ||
disabled={disabled} | ||
checked={value.some( | ||
(v) => compare?.(v, option.value) ?? v === option.value | ||
)} | ||
onCheckedChange={(checked) => { | ||
if (checked) { | ||
onChange([...value, option.value]) | ||
} else { | ||
onChange( | ||
value.filter( | ||
(v) => | ||
!(compare?.(v, option.value) ?? v === option.value) | ||
) | ||
) | ||
} | ||
}} | ||
/> | ||
)} | ||
|
||
<Text key={option.title}>{option.title}</Text> | ||
</div> | ||
) | ||
})} | ||
</div> | ||
) | ||
} |
39 changes: 39 additions & 0 deletions
39
packages/admin-next/dashboard/src/hooks/api/categories.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,39 @@ | ||
import { QueryKey, UseQueryOptions, useQuery } from "@tanstack/react-query" | ||
import { client } from "../../lib/client" | ||
import { queryKeysFactory } from "../../lib/query-key-factory" | ||
import { CategoriesListRes, CategoryRes } from "../../types/api-responses" | ||
|
||
const CATEGORIES_QUERY_KEY = "categories" as const | ||
export const categoriesQueryKeys = queryKeysFactory(CATEGORIES_QUERY_KEY) | ||
|
||
export const useCategory = ( | ||
id: string, | ||
options?: Omit< | ||
UseQueryOptions<CategoryRes, Error, CategoryRes, QueryKey>, | ||
"queryFn" | "queryKey" | ||
> | ||
) => { | ||
const { data, ...rest } = useQuery({ | ||
queryKey: categoriesQueryKeys.detail(id), | ||
queryFn: async () => client.categories.retrieve(id), | ||
...options, | ||
}) | ||
|
||
return { ...data, ...rest } | ||
} | ||
|
||
export const useCategories = ( | ||
query?: Record<string, any>, | ||
options?: Omit< | ||
UseQueryOptions<CategoriesListRes, Error, CategoriesListRes, QueryKey>, | ||
"queryFn" | "queryKey" | ||
> | ||
) => { | ||
const { data, ...rest } = useQuery({ | ||
queryKey: categoriesQueryKeys.list(query), | ||
queryFn: async () => client.categories.list(query), | ||
...options, | ||
}) | ||
|
||
return { ...data, ...rest } | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { QueryKey, UseQueryOptions, useQuery } from "@tanstack/react-query" | ||
import { client } from "../../lib/client" | ||
import { queryKeysFactory } from "../../lib/query-key-factory" | ||
import { TagsListRes, TagRes } from "../../types/api-responses" | ||
|
||
const TAGS_QUERY_KEY = "tags" as const | ||
export const tagsQueryKeys = queryKeysFactory(TAGS_QUERY_KEY) | ||
|
||
export const useTag = ( | ||
id: string, | ||
options?: Omit< | ||
UseQueryOptions<TagRes, Error, TagRes, QueryKey>, | ||
"queryFn" | "queryKey" | ||
> | ||
) => { | ||
const { data, ...rest } = useQuery({ | ||
queryKey: tagsQueryKeys.detail(id), | ||
queryFn: async () => client.tags.retrieve(id), | ||
...options, | ||
}) | ||
|
||
return { ...data, ...rest } | ||
} | ||
|
||
export const useTags = ( | ||
query?: Record<string, any>, | ||
options?: Omit< | ||
UseQueryOptions<TagsListRes, Error, TagsListRes, QueryKey>, | ||
"queryFn" | "queryKey" | ||
> | ||
) => { | ||
const { data, ...rest } = useQuery({ | ||
queryKey: tagsQueryKeys.list(query), | ||
queryFn: async () => client.tags.list(query), | ||
...options, | ||
}) | ||
|
||
return { ...data, ...rest } | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/admin-next/dashboard/src/lib/client/categories.ts
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,21 @@ | ||
import { | ||
ProductCollectionListRes, | ||
ProductCollectionRes, | ||
} from "../../types/api-responses" | ||
import { getRequest } from "./common" | ||
|
||
async function listProductCategories(query?: Record<string, any>) { | ||
return getRequest<ProductCollectionListRes>(`/admin/categories`, query) | ||
} | ||
|
||
async function retrieveProductCategory( | ||
id: string, | ||
query?: Record<string, any> | ||
) { | ||
return getRequest<ProductCollectionRes>(`/admin/categories/${id}`, query) | ||
} | ||
|
||
export const categories = { | ||
list: listProductCategories, | ||
retrieve: retrieveProductCategory, | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,34 @@ | ||
import { ProductListRes, ProductRes } from "../../types/api-responses" | ||
import { getRequest } from "./common" | ||
import { | ||
ProductDeleteRes, | ||
ProductListRes, | ||
ProductRes, | ||
} from "../../types/api-responses" | ||
import { deleteRequest, getRequest, postRequest } from "./common" | ||
|
||
async function retrieveProduct(id: string, query?: Record<string, any>) { | ||
return getRequest<ProductRes>(`/admin/products/${id}`, query) | ||
} | ||
|
||
async function createProduct(payload: any) { | ||
return postRequest<ProductRes>(`/admin/products`, payload) | ||
} | ||
|
||
async function listProducts(query?: Record<string, any>) { | ||
return getRequest<ProductListRes>(`/admin/products`, query) | ||
} | ||
|
||
async function updateProduct(id: string, payload: any) { | ||
return postRequest<ProductRes>(`/admin/products/${id}`, payload) | ||
} | ||
|
||
async function deleteProduct(id: string) { | ||
return deleteRequest<ProductDeleteRes>(`/admin/products/${id}`) | ||
} | ||
|
||
export const products = { | ||
retrieve: retrieveProduct, | ||
list: listProducts, | ||
create: createProduct, | ||
update: updateProduct, | ||
delete: deleteProduct, | ||
} |
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,18 @@ | ||
import { | ||
ProductCollectionListRes, | ||
ProductCollectionRes, | ||
} from "../../types/api-responses" | ||
import { getRequest } from "./common" | ||
|
||
async function listProductTags(query?: Record<string, any>) { | ||
return getRequest<ProductCollectionListRes>(`/admin/tags`, query) | ||
} | ||
|
||
async function retrieveProductTag(id: string, query?: Record<string, any>) { | ||
return getRequest<ProductCollectionRes>(`/admin/tags/${id}`, query) | ||
} | ||
|
||
export const tags = { | ||
list: listProductTags, | ||
retrieve: retrieveProductTag, | ||
} |
Oops, something went wrong.