Skip to content

Commit

Permalink
feat: listing return reasons
Browse files Browse the repository at this point in the history
  • Loading branch information
fPolic committed Jun 28, 2024
1 parent 5f0f864 commit 94732b4
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 14 deletions.
29 changes: 29 additions & 0 deletions packages/admin-next/dashboard/src/hooks/api/return-reasons.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { HttpTypes } from "@medusajs/types"
import { QueryKey, UseQueryOptions, useQuery } from "@tanstack/react-query"

import { sdk } from "../../lib/client"
import { queryKeysFactory } from "../../lib/query-key-factory"

const RETURN_REASONS_QUERY_KEY = "return_reasons" as const
export const returnReasonQueryKeys = queryKeysFactory(RETURN_REASONS_QUERY_KEY)

export const useReturnReasons = (
query?: HttpTypes.AdminReturnReasonListParams,
options?: Omit<
UseQueryOptions<
HttpTypes.AdminReturnReasonsResponse,
Error,
HttpTypes.AdminReturnReasonsResponse,
QueryKey
>,
"queryFn" | "queryKey"
>
) => {
const { data, ...rest } = useQuery({
queryFn: () => sdk.admin.returnReason.list(query),
queryKey: returnReasonQueryKeys.list(query),
...options,
})

return { ...data, ...rest }
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { useStockLocations } from "../../../../../hooks/api/stock-locations"
import { useShippingOptions } from "../../../../../hooks/api/shipping-options"
import { getStylizedAmount } from "../../../../../lib/money-amount-helpers"
import { useCreateOrderReturn } from "../../../../../hooks/api/orders"
import { useReturnReasons } from "../../../../../hooks/api/return-reasons"
import { currencies } from "../../../../../lib/currencies"

type ReturnCreateFormProps = {
Expand All @@ -47,6 +48,7 @@ export const ReturnCreateForm = ({ order }: ReturnCreateFormProps) => {

const [showAddItemView, setShowAddItemView] = useState(false)

const { return_reasons = [] } = useReturnReasons({ fields: "+label" })
const { stock_locations = [] } = useStockLocations({ limit: 999 })
const { shipping_options = [] } = useShippingOptions({
limit: 999,
Expand All @@ -60,7 +62,7 @@ export const ReturnCreateForm = ({ order }: ReturnCreateFormProps) => {
/**
* TODO: reason selection once Return reason settings are added
*/
defaultValues: { items: [], reason_id: "todo" },
defaultValues: { items: [] },
resolver: zodResolver(ReturnCreateSchema),
})

Expand Down Expand Up @@ -218,20 +220,21 @@ export const ReturnCreateForm = ({ order }: ReturnCreateFormProps) => {
<Form.Field
control={form.control}
name="reason_id"
render={({ field: { ref, onChange, ...field } }) => {
render={({ field: { ref, value, onChange, ...field } }) => {
return (
<Form.Item>
<Form.Control>
<Select {...field} onValueChange={onChange}>
<Select.Trigger ref={ref}>
<Select.Value />
</Select.Trigger>
<Select.Content>
{/*<Select.Item value="active">*/}
{/* TODO*/}
{/*</Select.Item>*/}
</Select.Content>
</Select>
<Combobox
value={value}
onChange={(v) => {
onChange(v)
}}
{...field}
options={return_reasons.map((reason) => ({
label: reason.label,
value: reason.id,
}))}
/>
</Form.Control>
<Form.ErrorMessage />
</Form.Item>
Expand Down
3 changes: 3 additions & 0 deletions packages/core/js-sdk/src/admin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { StockLocation } from "./stock-location"
import { TaxRate } from "./tax-rate"
import { TaxRegion } from "./tax-region"
import { Upload } from "./upload"
import { ReturnReason } from "./return-reason"

export class Admin {
public invite: Invite
Expand All @@ -30,6 +31,7 @@ export class Admin {
public productType: ProductType
public upload: Upload
public region: Region
public returnReason: ReturnReason
public stockLocation: StockLocation
public salesChannel: SalesChannel
public fulfillmentSet: FulfillmentSet
Expand All @@ -52,6 +54,7 @@ export class Admin {
this.productType = new ProductType(client)
this.upload = new Upload(client)
this.region = new Region(client)
this.returnReason = new ReturnReason(client)
this.stockLocation = new StockLocation(client)
this.salesChannel = new SalesChannel(client)
this.fulfillmentSet = new FulfillmentSet(client)
Expand Down
24 changes: 24 additions & 0 deletions packages/core/js-sdk/src/admin/return-reason.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { HttpTypes } from "@medusajs/types"

import { Client } from "../client"
import { ClientHeaders } from "../types"

export class ReturnReason {
private client: Client
constructor(client: Client) {
this.client = client
}

async list(
queryParams?: HttpTypes.AdminReturnReasonListParams,
headers?: ClientHeaders
) {
return await this.client.fetch<HttpTypes.AdminReturnReasonsResponse>(
"/admin/return-reasons",
{
headers,
query: queryParams,
}
)
}
}
1 change: 1 addition & 0 deletions packages/core/types/src/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export * from "./product-category"
export * from "./product-type"
export * from "./promotion"
export * from "./region"
export * from "./return-reason"
export * from "./reservation"
export * from "./sales-channel"
export * from "./shipping-option"
Expand Down
27 changes: 27 additions & 0 deletions packages/core/types/src/http/return-reason/admin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { BaseReturnReason, BaseReturnReasonFilters } from "./common"
import { FindParams } from "../common"
import { OperatorMap } from "../../dal"

export interface AdminReturnReason extends BaseReturnReason {}
export interface AdminReturnReasonFilters extends BaseReturnReasonFilters {}

export interface AdminCreateReturnReason {
// TODO:
value: string
label: string
description?: string
}

export interface AdminReturnReasonsResponse {
return_reasons: AdminReturnReason[]
}

export interface AdminReturnReasonListParams extends FindParams {
id?: string[] | string | OperatorMap<string | string[]>
value?: string | OperatorMap<string>
label?: string | OperatorMap<string>
description?: string | OperatorMap<string>
parent_return_reason_id?: string | OperatorMap<string | string[]>
created_at?: OperatorMap<string>
updated_at?: OperatorMap<string>
}
23 changes: 23 additions & 0 deletions packages/core/types/src/http/return-reason/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { BaseFilterable, OperatorMap } from "../../dal"

export interface BaseReturnReason {
id: string
value: string
label: string
description?: string
metadata?: Record<string, any> | null
created_at?: string
updated_at?: string
}

export interface BaseReturnReasonFilters
extends BaseFilterable<BaseReturnReason> {
q?: string
id?: string[] | string | OperatorMap<string | string[]>
value?: string | OperatorMap<string>
label?: string | OperatorMap<string>
description?: string | OperatorMap<string>
parent_return_reason_id?: string | OperatorMap<string | string[]>
created_at?: OperatorMap<string>
updated_at?: OperatorMap<string>
}
1 change: 1 addition & 0 deletions packages/core/types/src/http/return-reason/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./admin"
4 changes: 2 additions & 2 deletions packages/medusa/src/api/admin/return-reasons/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export type AdminGetReturnReasonsParamsType = z.infer<
export const AdminCreateReturnReason = z.object({
value: z.string(),
label: z.string(),
descripton: z.string().nullish(),
description: z.string().nullish(),
parent_return_reason_id: z.string().nullish(),
metadata: z.record(z.unknown()).nullish(),
})
Expand All @@ -63,7 +63,7 @@ export type AdminCreateReturnReasonType = z.infer<
export const AdminUpdateReturnReason = z.object({
value: z.string(),
label: z.string(),
descripton: z.string().nullish(),
description: z.string().nullish(),
metadata: z.record(z.unknown()).nullish(),
})
export type AdminUpdateReturnReasonType = z.infer<
Expand Down

0 comments on commit 94732b4

Please sign in to comment.