Skip to content

Commit

Permalink
update according to new API
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperkristensen committed Dec 9, 2024
1 parent ff8eaf5 commit 848e2c8
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ const OuterComponent = ({

useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
if (isAnchor && (e.metaKey || e.ctrlKey) && e.key === "b") {
if (isAnchor && (e.metaKey || e.ctrlKey) && e.key.toLowerCase() === "b") {
e.preventDefault()
buttonRef.current?.click()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ export enum ShippingOptionPriceType {
export const GEO_ZONE_STACKED_MODAL_ID = "geo-zone"

export const CONDITIONAL_PRICES_STACKED_MODAL_ID = "conditional-prices"

export const ITEM_TOTAL_ATTRIBUTE = "item_total"
export const REGION_ID_ATTRIBUTE = "region_id"
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { castNumber } from "../../../../lib/cast-number"
import { ITEM_TOTAL_ATTRIBUTE } from "../constants"

const createPriceRule = (
attribute: string,
operator: string,
value: string | number
) => {
const rule = {
attribute,
operator,
value: castNumber(value),
}

return rule
}

export const buildShippingOptionPriceRules = (rule: {
gte?: string | number | null
lte?: string | number | null
gt?: string | number | null
lt?: string | number | null
eq?: string | number | null
}) => {
const conditions = [
{ value: rule.gte, operator: "gte" },
{ value: rule.lte, operator: "lte" },
{ value: rule.gt, operator: "gt" },
{ value: rule.lt, operator: "lt" },
{ value: rule.eq, operator: "eq" },
]

const conditionsWithValues = conditions.filter(({ value }) => value) as {
value: string | number
operator: string
}[]

return conditionsWithValues.map(({ operator, value }) =>
createPriceRule(ITEM_TOTAL_ATTRIBUTE, operator, value)
)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { HandTruck, PencilSquare } from "@medusajs/icons"
import { HttpTypes } from "@medusajs/types"
import { Container, Heading } from "@medusajs/ui"
import { Fragment } from "react"
import { useTranslation } from "react-i18next"

import { ActionMenu } from "../../../../../components/common/action-menu"
Expand Down Expand Up @@ -50,15 +51,15 @@ function LocationsFulfillmentProvidersSection({
<div className="grid grid-cols-[28px_1fr] items-center gap-x-3 gap-y-3">
{fulfillment_providers?.map((fulfillmentProvider) => {
return (
<>
<Fragment key={fulfillmentProvider.id}>
<IconAvatar>
<HandTruck className="text-ui-fg-subtle" />
</IconAvatar>

<div className="txt-compact-small">
{formatProvider(fulfillmentProvider.id)}
</div>
</>
</Fragment>
)
})}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { KeyboundForm } from "../../../../../components/utilities/keybound-form"
import { useCreateShippingOptions } from "../../../../../hooks/api/shipping-options"
import { castNumber } from "../../../../../lib/cast-number"
import { ShippingOptionPriceType } from "../../../common/constants"
import { buildShippingOptionPriceRules } from "../../../common/utils/price-rule-helpers"
import { CreateShippingOptionDetailsForm } from "./create-shipping-option-details-form"
import { CreateShippingOptionsPricesForm } from "./create-shipping-options-prices-form"
import {
Expand Down Expand Up @@ -62,30 +63,6 @@ export function CreateShippingOptionsForm({

const { mutateAsync, isPending: isLoading } = useCreateShippingOptions()

const createPriceRule = (
attribute: string,
operator: string,
value: string | number
) => ({
attribute,
operator,
value: castNumber(value),
})

const buildRules = (rule: {
gte?: string | number | null
lte?: string | number | null
}) => {
const conditions = [
{ value: rule.gte, operator: "gte" },
{ value: rule.lte, operator: "lte" },
]

return conditions
.filter(({ value }) => value)
.map(({ operator, value }) => createPriceRule("total", operator, value!))
}

const handleSubmit = form.handleSubmit(async (data) => {
const currencyPrices = Object.entries(data.currency_prices)
.map(([code, value]) => {
Expand Down Expand Up @@ -120,7 +97,7 @@ export function CreateShippingOptionsForm({
value?.map((rule) => ({
region_id: region_id,
amount: castNumber(rule.amount),
rules: buildRules(rule),
rules: buildShippingOptionPriceRules(rule),
})) || []

return prices?.filter(Boolean)
Expand All @@ -133,7 +110,7 @@ export function CreateShippingOptionsForm({
value?.map((rule) => ({
currency_code,
amount: castNumber(rule.amount),
rules: buildRules(rule),
rules: buildShippingOptionPriceRules(rule),
})) || []

return prices?.filter(Boolean)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ import { useStore } from "../../../../../hooks/api/store"
import { castNumber } from "../../../../../lib/cast-number"
import { ConditionalPriceForm } from "../../../common/components/conditional-price-form"
import { ShippingOptionPriceProvider } from "../../../common/components/shipping-option-price-provider"
import { CONDITIONAL_PRICES_STACKED_MODAL_ID } from "../../../common/constants"
import {
CONDITIONAL_PRICES_STACKED_MODAL_ID,
ITEM_TOTAL_ATTRIBUTE,
REGION_ID_ATTRIBUTE,
} from "../../../common/constants"
import { useShippingOptionPriceColumns } from "../../../common/hooks/use-shipping-option-price-columns"
import {
UpdateConditionalPrice,
UpdateConditionalPriceSchema,
} from "../../../common/schema"
import { ConditionalPriceInfo } from "../../../common/types"
import { buildShippingOptionPriceRules } from "../../../common/utils/price-rule-helpers"

type PriceRecord = {
id?: string
Expand Down Expand Up @@ -124,30 +129,6 @@ export function EditShippingOptionsPricingForm({
[currencies, regions]
)

const createPriceRule = (
attribute: string,
operator: string,
value: string | number
) => ({
attribute,
operator,
value: castNumber(value),
})

const buildRules = (rule: UpdateConditionalPrice) => {
const conditions = [
{ value: rule.gte, operator: "gte" },
{ value: rule.lte, operator: "lte" },
{ value: rule.gt, operator: "gt" },
{ value: rule.lt, operator: "lt" },
{ value: rule.eq, operator: "eq" },
]

return conditions
.filter(({ value }) => value)
.map(({ operator, value }) => createPriceRule("total", operator, value!))
}

const handleSubmit = form.handleSubmit(async (data) => {
const currencyPrices = Object.entries(data.currency_prices)
.map(([code, value]) => {
Expand Down Expand Up @@ -182,7 +163,7 @@ export function EditShippingOptionsPricingForm({
id: rule.id,
currency_code,
amount: castNumber(rule.amount),
rules: buildRules(rule),
rules: buildShippingOptionPriceRules(rule),
}))
)

Expand Down Expand Up @@ -212,7 +193,7 @@ export function EditShippingOptionsPricingForm({
id: rule.id,
region_id,
amount: castNumber(rule.amount),
rules: buildRules(rule),
rules: buildShippingOptionPriceRules(rule),
}))
)

Expand Down Expand Up @@ -317,8 +298,9 @@ const findRuleValue = (
const fallbackValue = ["eq", "gt", "lt"].includes(operator) ? undefined : null

return (
rules?.find((r) => r.attribute === "total" && r.operator === operator)
?.value || fallbackValue
rules?.find(
(r) => r.attribute === ITEM_TOTAL_ATTRIBUTE && r.operator === operator
)?.value || fallbackValue
)
}

Expand Down Expand Up @@ -363,7 +345,7 @@ const getDefaultValues = (prices: HttpTypes.AdminShippingOptionPrice[]) => {
return
}

if (hasAttributes(price, ["total"], ["region_id"])) {
if (hasAttributes(price, [ITEM_TOTAL_ATTRIBUTE], [REGION_ID_ATTRIBUTE])) {
const code = price.currency_code!
if (!conditional_currency_prices[code]) {
conditional_currency_prices[code] = []
Expand All @@ -372,13 +354,13 @@ const getDefaultValues = (prices: HttpTypes.AdminShippingOptionPrice[]) => {
return
}

if (hasAttributes(price, ["region_id"], ["total"])) {
if (hasAttributes(price, [REGION_ID_ATTRIBUTE], [ITEM_TOTAL_ATTRIBUTE])) {
const regionId = price.price_rules[0].value
region_prices[regionId] = price.amount
return
}

if (hasAttributes(price, ["region_id", "total"])) {
if (hasAttributes(price, [REGION_ID_ATTRIBUTE, ITEM_TOTAL_ATTRIBUTE])) {
const regionId = price.price_rules[0].value
if (!conditional_region_prices[regionId]) {
conditional_region_prices[regionId] = []
Expand Down

0 comments on commit 848e2c8

Please sign in to comment.