Skip to content

The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.

feat(admin): add commission rules to edit form and detail page

vholik70586d6442ac
diff --git a/packages/admin/src/pages/commission-rates/commission-rate-detail/commission-rate-detail.tsx b/packages/admin/src/pages/commission-rates/commission-rate-detail/commission-rate-detail.tsx
index 004cbe76e..2517c83ac 100644
--- a/packages/admin/src/pages/commission-rates/commission-rate-detail/commission-rate-detail.tsx
+++ b/packages/admin/src/pages/commission-rates/commission-rate-detail/commission-rate-detail.tsx
@@ -4,6 +4,7 @@ import { SingleColumnPageSkeleton } from "../../../components/common/skeleton";
import { SingleColumnPage } from "../../../components/layout/pages";
import { useCommissionRate } from "../../../hooks/api/commission-rates";
import { CommissionRateGeneralSection } from "./components/commission-rate-general-section";
+import { CommissionRateRulesSection } from "./components/commission-rate-rules-section";
import { commissionRateLoader } from "./loader";
export const CommissionRateDetail = () => {
@@ -30,6 +31,7 @@ export const CommissionRateDetail = () => {
return (
<SingleColumnPage data={commission_rate} showJSON showMetadata>
<CommissionRateGeneralSection commissionRate={commission_rate} />
+ <CommissionRateRulesSection commissionRate={commission_rate} />
</SingleColumnPage>
);
};
diff --git a/packages/admin/src/pages/commission-rates/commission-rate-detail/components/commission-rate-rules-section/commission-rate-rules-section.tsx b/packages/admin/src/pages/commission-rates/commission-rate-detail/components/commission-rate-rules-section/commission-rate-rules-section.tsx
new file mode 100644
index 000000000..06f07b45e
--- /dev/null
+++ b/packages/admin/src/pages/commission-rates/commission-rate-detail/components/commission-rate-rules-section/commission-rate-rules-section.tsx
@@ -0,0 +1,253 @@
+import { ArrowDownRightMini } from "@medusajs/icons"
+import {
+ Badge,
+ Container,
+ Heading,
+ Text,
+ Tooltip,
+} from "@medusajs/ui"
+import { useTranslation } from "react-i18next"
+
+import { ClientError } from "@mercurjs/client"
+import { CommissionRateDTO } from "@mercurjs/types"
+import { useProductTypes } from "../../../../../hooks/api/product-types"
+import { useProducts } from "../../../../../hooks/api/products"
+import { useShippingOptions } from "../../../../../hooks/api"
+import { TaxRateRuleReferenceType } from "../../../../tax-regions/common/constants"
+
+type CommissionRateRulesSectionProps = {
+ commissionRate: CommissionRateDTO
+}
+
+export const CommissionRateRulesSection = ({
+ commissionRate,
+}: CommissionRateRulesSectionProps) => {
+ const { t } = useTranslation()
+ const rules = commissionRate.rules || []
+
+ const groupedRules = rules.reduce(
+ (acc, rule) => {
+ if (!acc[rule.reference]) {
+ acc[rule.reference] = []
+ }
+ acc[rule.reference].push(rule.reference_id)
+ return acc
+ },
+ {} as Record<string, string[]>
+ )
+
+ const entries = Object.entries(groupedRules)
+
+ return (
+ <Container className="divide-y p-0">
+ <div className="flex items-center justify-between px-6 py-4">
+ <Heading level="h2">
+ {t("taxRegions.fields.targets.label")}
+ </Heading>
+ </div>
+ {entries.length === 0 ? (
+ <div className="flex items-center justify-center px-6 py-8">
+ <Text size="small" className="text-ui-fg-subtle">
+ No rules configured
+ </Text>
+ </div>
+ ) : (
+ <div className="px-6 py-4">
+ <div className="flex items-center gap-x-3">
+ <div className="text-ui-fg-muted flex size-5 items-center justify-center">
+ <ArrowDownRightMini />
+ </div>
+ <div className="flex flex-wrap items-center gap-x-1.5 gap-y-2">
+ <Text
+ size="small"
+ leading="compact"
+ className="text-ui-fg-subtle"
+ >
+ {t("taxRegions.fields.targets.operators.on")}
+ </Text>
+ {entries.map(([reference, ids], index) => {
+ return (
+ <div
+ key={reference}
+ className="flex items-center gap-x-1.5"
+ >
+ <Reference
+ reference={reference as TaxRateRuleReferenceType}
+ ids={ids}
+ />
+ {index < entries.length - 1 && (
+ <Text
+ size="small"
+ leading="compact"
+ className="text-ui-fg-subtle"
+ >
+ {t("taxRegions.fields.targets.operators.and")}
+ </Text>
+ )}
+ </div>
+ )
+ })}
+ </div>
+ </div>
+ </div>
+ )}
+ </Container>
+ )
+}
+
+const Reference = ({
+ reference,
+ ids,
+}: {
+ reference: TaxRateRuleReferenceType
+ ids: string[]
+}) => {
+ return (
+ <div className="flex items-center gap-x-1.5">
+ <ReferenceBadge reference={reference} />
+ <ReferenceValues type={reference} ids={ids} />
+ </div>
+ )
+}
+
+const ReferenceBadge = ({
+ reference,
+}: {
+ reference: TaxRateRuleReferenceType
+}) => {
+ const { t } = useTranslation()
+ let label: string | null = null
+
+ switch (reference) {
+ case TaxRateRuleReferenceType.PRODUCT:
+ label = t("taxRegions.fields.targets.tags.product")
+ break
+ case TaxRateRuleReferenceType.PRODUCT_TYPE:
+ label = t("taxRegions.fields.targets.tags.productType")
+ break
+ case TaxRateRuleReferenceType.SHIPPING_OPTION:
+ label = t("taxRegions.fields.targets.tags.shippingOption")
+ break
+ }
+
+ if (!label) {
+ return null
+ }
+
+ return <Badge size="2xsmall">{label}</Badge>
+}
+
+const ReferenceValues = ({
+ type,
+ ids,
+}: {
+ type: TaxRateRuleReferenceType
+ ids: string[]
+}) => {
+ const { t } = useTranslation()
+
+ const { isPending, additional, labels, isError, error } = useReferenceValues(
+ type,
+ ids
+ )
+
+ if (isError) {
+ throw error
+ }
+
+ if (isPending) {
+ return (
+ <div className="bg-ui-tag-neutral-bg border-ui-tag-neutral-border h-5 w-14 animate-pulse rounded-md" />
+ )
+ }
+
+ return (
+ <Tooltip
+ content={
+ <ul>
+ {labels?.map((label: string, index) => (
+ <li key={index}>{label}</li>
+ ))}
+ {additional > 0 && (
+ <li>
+ {t("taxRegions.fields.targets.additionalValues", {
+ count: additional,
+ })}
+ </li>
+ )}
+ </ul>
+ }
+ >
+ <Badge size="2xsmall">
+ {t("taxRegions.fields.targets.values", {
+ count: ids.length,
+ })}
+ </Badge>
+ </Tooltip>
+ )
+}
+
+const useReferenceValues = (
+ type: TaxRateRuleReferenceType,
+ ids: string[]
+): {
+ labels: string[] | undefined
+ isPending: boolean
+ additional: number
+ isError: boolean
+ error: ClientError | null
+} => {
+ const products = useProducts(
+ { id: ids, limit: 10 },
+ { enabled: !!ids.length && type === TaxRateRuleReferenceType.PRODUCT }
+ )
+
+ const productTypes = useProductTypes(
+ { id: ids, limit: 10 },
+ { enabled: !!ids.length && type === TaxRateRuleReferenceType.PRODUCT_TYPE }
+ )
+
+ const shippingOptions = useShippingOptions(
+ { id: ids, limit: 10 },
+ {
+ enabled:
+ !!ids.length && type === TaxRateRuleReferenceType.SHIPPING_OPTION,
+ }
+ )
+
+ switch (type) {
+ case TaxRateRuleReferenceType.PRODUCT:
+ return {
+ labels: products.products?.map((product) => product.title),
+ isPending: products.isPending,
+ additional:
+ products.products && products.count
+ ? products.count - products.products.length
+ : 0,
+ isError: products.isError,
+ error: products.error,
+ }
+ case TaxRateRuleReferenceType.PRODUCT_TYPE:
+ return {
+ labels: productTypes.product_types?.map((type) => type.value),
+ isPending: productTypes.isPending,
+ additional:
+ productTypes.product_types && productTypes.count
+ ? productTypes.count - productTypes.product_types.length
+ : 0,
+ isError: productTypes.isError,
+ error: productTypes.error,
+ }
+ case TaxRateRuleReferenceType.SHIPPING_OPTION:
+ return {
+ labels: shippingOptions.shipping_options?.map((option) => option.name),
+ isPending: shippingOptions.isPending,
+ additional: shippingOptions.count
+ ? shippingOptions.count -
+ (shippingOptions.shipping_options?.length || 0)
+ : 0,
+ isError: shippingOptions.isError,
+ error: shippingOptions.error,
+ }
+ }
+}
diff --git a/packages/admin/src/pages/commission-rates/commission-rate-detail/components/commission-rate-rules-section/index.ts b/packages/admin/src/pages/commission-rates/commission-rate-detail/components/commission-rate-rules-section/index.ts
new file mode 100644
index 000000000..d0bbea1f8
--- /dev/null
+++ b/packages/admin/src/pages/commission-rates/commission-rate-detail/components/commission-rate-rules-section/index.ts
@@ -0,0 +1 @@
+export { CommissionRateRulesSection } from "./commission-rate-rules-section"
diff --git a/packages/admin/src/pages/commission-rates/commission-rate-edit/components/edit-commission-rate-form/edit-commission-rate-form.tsx b/packages/admin/src/pages/commission-rates/commission-rate-edit/components/edit-commission-rate-form/edit-commission-rate-form.tsx
index df6896c3c..e9e542c0d 100644
--- a/packages/admin/src/pages/commission-rates/commission-rate-edit/components/edit-commission-rate-form/edit-commission-rate-form.tsx
+++ b/packages/admin/src/pages/commission-rates/commission-rate-edit/components/edit-commission-rate-form/edit-commission-rate-form.tsx
@@ -1,17 +1,51 @@
import { zodResolver } from "@hookform/resolvers/zod"
-import { Button, CurrencyInput, Input, Select, Switch, toast } from "@medusajs/ui"
-import { useForm } from "react-hook-form"
+import { MagnifyingGlass } from "@medusajs/icons"
+import {
+ Button,
+ clx,
+ CurrencyInput,
+ Divider,
+ Heading,
+ Hint,
+ Input,
+ Label,
+ Select,
+ Switch,
+ Text,
+ toast,
+} from "@medusajs/ui"
+import { useFieldArray, useForm, useWatch } from "react-hook-form"
import { useTranslation } from "react-i18next"
import * as zod from "zod"
import { Form } from "../../../../../components/common/form"
import { PercentageInput } from "../../../../../components/inputs/percentage-input"
-import { RouteDrawer, useRouteModal } from "../../../../../components/modals"
+import {
+ RouteDrawer,
+ StackedDrawer,
+ useRouteModal,
+ useStackedModal,
+} from "../../../../../components/modals"
import { KeyboundForm } from "../../../../../components/utilities/keybound-form"
-import { useUpdateCommissionRate } from "../../../../../hooks/api/commission-rates"
+import {
+ useBatchCommissionRules,
+ useUpdateCommissionRate,
+} from "../../../../../hooks/api/commission-rates"
import { useStore } from "../../../../../hooks/api/store"
import { currencies as currencyData } from "../../../../../lib/data/currencies"
import { CommissionRateDTO } from "@mercurjs/types"
+import { TargetForm } from "../../../../tax-regions/common/components/target-form/target-form"
+import { TargetItem } from "../../../../tax-regions/common/components/target-item/target-item"
+import { TaxRateRuleReferenceType } from "../../../../tax-regions/common/constants"
+import {
+ TaxRateRuleReference,
+ TaxRateRuleReferenceSchema,
+} from "../../../../tax-regions/common/schemas"
+import { useDocumentDirection } from "../../../../../hooks/use-document-direction"
+
+const STACKED_MODAL_ID = "cr"
+const getStackedModalId = (type: TaxRateRuleReferenceType) =>
+ `${STACKED_MODAL_ID}-${type}`
const EditCommissionRateSchema = zod.object({
name: zod.string().min(1),
@@ -23,17 +57,49 @@ const EditCommissionRateSchema = zod.object({
min_amount: zod.coerce.number().optional(),
include_tax: zod.boolean(),
priority: zod.coerce.number().int().min(0),
+ enabled_rules: zod.object({
+ product: zod.boolean(),
+ product_type: zod.boolean(),
+ shipping_option: zod.boolean(),
+ }),
+ product: zod.array(TaxRateRuleReferenceSchema).optional(),
+ product_type: zod.array(TaxRateRuleReferenceSchema).optional(),
+ shipping_option: zod.array(TaxRateRuleReferenceSchema).optional(),
})
type EditCommissionRateFormProps = {
commissionRate: CommissionRateDTO
}
+const getRulesByType = (
+ rules: CommissionRateDTO["rules"],
+ type: TaxRateRuleReferenceType
+): TaxRateRuleReference[] => {
+ return (rules || [])
+ .filter((r) => r.reference === type)
+ .map((r) => ({ value: r.reference_id, label: "" }))
+}
+
export const EditCommissionRateForm = ({
commissionRate,
}: EditCommissionRateFormProps) => {
const { t } = useTranslation()
const { handleSuccess } = useRouteModal()
+ const { setIsOpen } = useStackedModal()
+ const direction = useDocumentDirection()
+
+ const initialProducts = getRulesByType(
+ commissionRate.rules,
+ TaxRateRuleReferenceType.PRODUCT
+ )
+ const initialProductTypes = getRulesByType(
+ commissionRate.rules,
+ TaxRateRuleReferenceType.PRODUCT_TYPE
+ )
+ const initialShippingOptions = getRulesByType(
+ commissionRate.rules,
+ TaxRateRuleReferenceType.SHIPPING_OPTION
+ )
const form = useForm<zod.infer<typeof EditCommissionRateSchema>>({
defaultValues: {
@@ -46,6 +112,14 @@ export const EditCommissionRateForm = ({
min_amount: commissionRate.min_amount ?? undefined,
include_tax: commissionRate.include_tax,
priority: commissionRate.priority,
+ enabled_rules: {
+ product: initialProducts.length > 0,
+ product_type: initialProductTypes.length > 0,
+ shipping_option: initialShippingOptions.length > 0,
+ },
+ product: initialProducts,
+ product_type: initialProductTypes,
+ shipping_option: initialShippingOptions,
},
resolver: zodResolver(EditCommissionRateSchema),
})
@@ -59,12 +133,52 @@ export const EditCommissionRateForm = ({
const defaultCurrencyCode =
store?.supported_currencies?.[0]?.currency_code ?? ""
- const { mutateAsync: updateCommissionRate, isPending } =
+ const { mutateAsync: updateCommissionRate, isPending: isUpdating } =
useUpdateCommissionRate(commissionRate.id)
+ const { mutateAsync: batchRules, isPending: isBatching } =
+ useBatchCommissionRules(commissionRate.id)
+
const handleSubmit = form.handleSubmit(async (values) => {
- await updateCommissionRate(
- {
+ const newRules = [
+ ...(values.product || []).map((ref) => ({
+ reference: TaxRateRuleReferenceType.PRODUCT,
+ reference_id: ref.value,
+ })),
+ ...(values.product_type || []).map((ref) => ({
+ reference: TaxRateRuleReferenceType.PRODUCT_TYPE,
+ reference_id: ref.value,
+ })),
+ ...(values.shipping_option || []).map((ref) => ({
+ reference: TaxRateRuleReferenceType.SHIPPING_OPTION,
+ reference_id: ref.value,
+ })),
+ ]
+
+ const existingRules = commissionRate.rules || []
+
+ const toCreate = newRules.filter(
+ (nr) =>
+ !existingRules.some(
+ (er) =>
+ er.reference === nr.reference &&
+ er.reference_id === nr.reference_id
+ )
+ )
+
+ const toDelete = existingRules
+ .filter(
+ (er) =>
+ !newRules.some(
+ (nr) =>
+ nr.reference === er.reference &&
+ nr.reference_id === er.reference_id
+ )
+ )
+ .map((er) => er.id)
+
+ try {
+ await updateCommissionRate({
name: values.name,
code: values.code,
type: values.type,
@@ -74,29 +188,175 @@ export const EditCommissionRateForm = ({
min_amount: values.min_amount ?? null,
include_tax: values.include_tax,
priority: values.priority,
- },
- {
- onSuccess: () => {
- toast.success("Commission rate updated successfully")
- handleSuccess()
- },
- onError: (e) => {
- toast.error(e.message)
- },
+ })
+
+ if (toCreate.length > 0 || toDelete.length > 0) {
+ await batchRules({
+ ...(toCreate.length > 0 ? { create: toCreate } : {}),
+ ...(toDelete.length > 0 ? { delete: toDelete } : {}),
+ })
}
- )
+
+ toast.success("Commission rate updated successfully")
+ handleSuccess()
+ } catch (e: any) {
+ toast.error(e.message)
+ }
})
const watchType = form.watch("type")
const watchCurrency = form.watch("currency_code")
+ const products = useFieldArray({
+ control: form.control,
+ name: TaxRateRuleReferenceType.PRODUCT,
+ })
+
+ const productTypes = useFieldArray({
+ control: form.control,
+ name: TaxRateRuleReferenceType.PRODUCT_TYPE,
+ })
+
+ const shippingOptions = useFieldArray({
+ control: form.control,
+ name: TaxRateRuleReferenceType.SHIPPING_OPTION,
+ })
+
+ const getControls = (type: TaxRateRuleReferenceType) => {
+ switch (type) {
+ case TaxRateRuleReferenceType.PRODUCT:
+ return products
+ case TaxRateRuleReferenceType.PRODUCT_TYPE:
+ return productTypes
+ case TaxRateRuleReferenceType.SHIPPING_OPTION:
+ return shippingOptions
+ }
+ }
+
+ const referenceTypeOptions = [
+ {
+ value: TaxRateRuleReferenceType.PRODUCT,
+ label: t("taxRegions.fields.targets.options.product"),
+ },
+ {
+ value: TaxRateRuleReferenceType.PRODUCT_TYPE,
+ label: t("taxRegions.fields.targets.options.productType"),
+ },
+ {
+ value: TaxRateRuleReferenceType.SHIPPING_OPTION,
+ label: t("taxRegions.fields.targets.options.shippingOption"),
+ },
+ ]
+
+ const searchPlaceholders = {
+ [TaxRateRuleReferenceType.PRODUCT]: t(
+ "taxRegions.fields.targets.placeholders.product"
+ ),
+ [TaxRateRuleReferenceType.PRODUCT_TYPE]: t(
+ "taxRegions.fields.targets.placeholders.productType"
+ ),
+ [TaxRateRuleReferenceType.SHIPPING_OPTION]: t(
+ "taxRegions.fields.targets.placeholders.shippingOption"
+ ),
+ }
+
+ const getFieldHandler = (type: TaxRateRuleReferenceType) => {
+ const { fields, remove, prepend } = getControls(type)
+ const modalId = getStackedModalId(type)
+
+ return (references: TaxRateRuleReference[]) => {
+ if (!references.length) {
+ form.setValue(type, [], { shouldDirty: true })
+ setIsOpen(modalId, false)
+ return
+ }
+
+ const newIds = references.map((reference) => reference.value)
+
+ const fieldsToAdd = references.filter(
+ (reference) => !fields.some((field) => field.value === reference.value)
+ )
+
+ for (const field of fields) {
+ if (!newIds.includes(field.value)) {
+ remove(fields.indexOf(field))
+ }
+ }
+
+ prepend(fieldsToAdd)
+ setIsOpen(modalId, false)
+ }
+ }
+
+ const displayOrder = new Set<TaxRateRuleReferenceType>(
+ [
+ initialProducts.length > 0 ? TaxRateRuleReferenceType.PRODUCT : null,
+ initialProductTypes.length > 0
+ ? TaxRateRuleReferenceType.PRODUCT_TYPE
+ : null,
+ initialShippingOptions.length > 0
+ ? TaxRateRuleReferenceType.SHIPPING_OPTION
+ : null,
+ ].filter(Boolean) as TaxRateRuleReferenceType[]
+ )
+
+ const disableRule = (type: TaxRateRuleReferenceType) => {
+ form.setValue(type, [], { shouldDirty: true })
+ form.setValue(`enabled_rules.${type}`, false, { shouldDirty: true })
+ displayOrder.delete(type)
+ }
+
+ const enableRule = (type: TaxRateRuleReferenceType) => {
+ form.setValue(`enabled_rules.${type}`, true, { shouldDirty: true })
+ form.setValue(type, [], { shouldDirty: true })
+ displayOrder.add(type)
+ }
+
+ const watchedEnabledRules = useWatch({
+ control: form.control,
+ name: "enabled_rules",
+ })
+
+ const addRule = () => {
+ const firstDisabledRule = Object.keys(watchedEnabledRules).find(
+ (key) => !watchedEnabledRules[key as TaxRateRuleReferenceType]
+ )
+
+ if (firstDisabledRule) {
+ enableRule(firstDisabledRule as TaxRateRuleReferenceType)
+ }
+ }
+
+ const visibleRuleTypes = referenceTypeOptions
+ .filter((option) => watchedEnabledRules[option.value])
+ .sort((a, b) => {
+ const orderArray = Array.from(displayOrder)
+ return orderArray.indexOf(a.value) - orderArray.indexOf(b.value)
+ })
+
+ const getAvailableRuleTypes = (type: TaxRateRuleReferenceType) => {
+ return referenceTypeOptions.filter((option) => {
+ return (
+ !visibleRuleTypes.some(
+ (visibleOption) => visibleOption.value === option.value
+ ) || option.value === type
+ )
+ })
+ }
+
+ const showAddButton = Object.values(watchedEnabledRules).some(
+ (value) => !value
+ )
+
+ const isPending = isUpdating || isBatching
+
return (
<RouteDrawer.Form form={form}>
<KeyboundForm
className="flex flex-1 flex-col overflow-hidden"
onSubmit={handleSubmit}
>
- <RouteDrawer.Body className="flex flex-col gap-y-4 overflow-y-auto">
+ <RouteDrawer.Body className="flex flex-1 flex-col gap-y-6 overflow-auto">
<div className="grid grid-cols-1 gap-4">
<Form.Field
control={form.control}
@@ -277,6 +537,198 @@ export const EditCommissionRateForm = ({
)}
/>
</div>
+ <div className="flex flex-col gap-y-3">
+ <div className="flex items-center justify-between gap-x-4">
+ <div className="flex flex-col">
+ <div className="flex items-center gap-x-1">
+ <Label
+ id="commission_rules_label"
+ htmlFor="commission_rules"
+ >
+ {t("taxRegions.fields.targets.label")}
+ </Label>
+ <Text
+ size="small"
+ leading="compact"
+ className="text-ui-fg-muted"
+ >
+ ({t("fields.optional")})
+ </Text>
+ </div>
+ <Hint
+ id="commission_rules_description"
+ className="text-pretty"
+ >
+ {t("taxRegions.fields.targets.hint")}
+ </Hint>
+ </div>
+ {showAddButton && (
+ <Button
+ onClick={addRule}
+ type="button"
+ size="small"
+ variant="transparent"
+ className="text-ui-fg-interactive hover:text-ui-fg-interactive-hover flex-shrink-0"
+ >
+ {t("taxRegions.fields.targets.action")}
+ </Button>
+ )}
+ </div>
+ <div
+ id="commission_rules"
+ aria-labelledby="commission_rules_label"
+ aria-describedby="commission_rules_description"
+ role="application"
+ className="flex flex-col gap-y-3"
+ >
+ {visibleRuleTypes.map((ruleType, index) => {
+ const type = ruleType.value
+ const label = ruleType.label
+ const isLast = index === visibleRuleTypes.length - 1
+ const searchPlaceholder = searchPlaceholders[type]
+
+ const options = getAvailableRuleTypes(type)
+ const modalId = getStackedModalId(type)
+
+ const { fields, remove } = getControls(type)
+ const handler = getFieldHandler(type)
+
+ const handleChangeType = (
+ value: TaxRateRuleReferenceType
+ ) => {
+ disableRule(type)
+ enableRule(value)
+ }
+
+ return (
+ <div key={type}>
+ <Form.Field
+ control={form.control}
+ name={ruleType.value}
+ render={({
+ field: { value: _value, onChange: _onChange, ...field },
+ }) => {
+ return (
+ <Form.Item className="space-y-0">
+ <Form.Label className="sr-only">
+ {label}
+ </Form.Label>
+ <div
+ className={clx(
+ "bg-ui-bg-component shadow-elevation-card-rest transition-fg grid gap-1.5 rounded-xl py-1.5",
+ "aria-[invalid='true']:shadow-borders-error"
+ )}
+ role="application"
+ {...field}
+ >
+ <div className="text-ui-fg-subtle grid gap-1.5 px-1.5 md:grid-cols-2">
+ {isLast ? (
+ <Select
+ dir={direction}
+ value={type}
+ onValueChange={handleChangeType}
+ >
+ <Select.Trigger className="bg-ui-bg-field-component hover:bg-ui-bg-field-component-hover">
+ <Select.Value />
+ </Select.Trigger>
+ <Select.Content>
+ {options.map((option) => {
+ return (
+ <Select.Item
+ key={option.value}
+ value={option.value}
+ >
+ {option.label}
+ </Select.Item>
+ )
+ })}
+ </Select.Content>
+ </Select>
+ ) : (
+ <div className="bg-ui-bg-field shadow-borders-base txt-compact-small rounded-md px-2 py-1.5">
+ {label}
+ </div>
+ )}
+ <div className="bg-ui-bg-field shadow-borders-base txt-compact-small rounded-md px-2 py-1.5">
+ {t(
+ "taxRegions.fields.targets.operators.in"
+ )}
+ </div>
+ </div>
+ <div className="flex items-center gap-1.5 px-1.5">
+ <StackedDrawer id={modalId}>
+ <StackedDrawer.Trigger asChild>
+ <button
+ type="button"
+ className="bg-ui-bg-field-component hover:bg-ui-bg-field-component-hover shadow-borders-base txt-compact-small text-ui-fg-muted transition-fg focus-visible:shadow-borders-interactive-with-active flex flex-1 items-center gap-x-2 rounded-md px-2 py-1.5 outline-none"
+ >
+ <MagnifyingGlass />
+ {searchPlaceholder}
+ </button>
+ </StackedDrawer.Trigger>
+ <StackedDrawer.Trigger asChild>
+ <Button variant="secondary">
+ {t("actions.browse")}
+ </Button>
+ </StackedDrawer.Trigger>
+ <StackedDrawer.Content>
+ <StackedDrawer.Header>
+ <StackedDrawer.Title asChild>
+ <Heading>
+ {t(
+ "taxRegions.fields.targets.modal.header"
+ )}
+ </Heading>
+ </StackedDrawer.Title>
+ <StackedDrawer.Description className="sr-only">
+ {t("taxRegions.fields.targets.hint")}
+ </StackedDrawer.Description>
+ </StackedDrawer.Header>
+ <TargetForm
+ type="drawer"
+ referenceType={type}
+ state={fields}
+ setState={handler}
+ />
+ </StackedDrawer.Content>
+ </StackedDrawer>
+ <Button
+ variant="secondary"
+ onClick={() => disableRule(type)}
+ type="button"
+ >
+ {t("actions.delete")}
+ </Button>
+ </div>
+ {fields.length > 0 ? (
+ <div className="flex flex-col gap-y-1.5">
+ <Divider variant="dashed" />
+ <div className="flex flex-col gap-y-1.5 px-1.5">
+ {fields.map((field, index) => {
+ return (
+ <TargetItem
+ key={field.id}
+ index={index}
+ label={field.label}
+ value={field.value}
+ onRemove={remove}
+ />
+ )
+ })}
+ </div>
+ </div>
+ ) : null}
+ </div>
+ <Form.ErrorMessage className="mt-2" />
+ </Form.Item>
+ )
+ }}
+ />
+ </div>
+ )
+ })}
+ </div>
+ </div>
</RouteDrawer.Body>
<RouteDrawer.Footer>
<RouteDrawer.Close asChild>