The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.
TypeScript b2b-marketplacecomposable-commerceecommerceenterpriseheadless-commercemarketplacemedusamedusajsmulti-vendortypescript
feat(admin): add rules section to commission rate create form and improve field UX
vholikc546a5f2dc48
diff --git a/packages/admin/src/pages/commission-rates/commission-rate-create/components/create-commission-rate-form/create-commission-rate-form.tsx b/packages/admin/src/pages/commission-rates/commission-rate-create/components/create-commission-rate-form/create-commission-rate-form.tsxindex b0f838e95..9e84a6145 100644--- a/packages/admin/src/pages/commission-rates/commission-rate-create/components/create-commission-rate-form/create-commission-rate-form.tsx+++ b/packages/admin/src/pages/commission-rates/commission-rate-create/components/create-commission-rate-form/create-commission-rate-form.tsx@@ -2,16 +2,20 @@ import { zodResolver } from "@hookform/resolvers/zod";import {Button,CurrencyInput,+ Divider,Heading,+ Hint,Input,+ Label,Select,Text,clx,toast,Switch,} from "@medusajs/ui";+import { MagnifyingGlass } from "@medusajs/icons";import { useEffect } from "react";-import { useForm } from "react-hook-form";+import { useFieldArray, useForm, useWatch } from "react-hook-form";import { useTranslation } from "react-i18next";import * as zod from "zod";@@ -19,13 +23,26 @@ import { Form } from "../../../../../components/common/form";import { PercentageInput } from "../../../../../components/inputs/percentage-input";import {RouteFocusModal,+ StackedFocusModal,useRouteModal,+ useStackedModal,} from "../../../../../components/modals";import { KeyboundForm } from "../../../../../components/utilities/keybound-form";import { useCreateCommissionRate } from "../../../../../hooks/api/commission-rates";-import { useStore } from "../../../../../hooks/api/store";import { currencies as currencyData } from "../../../../../lib/data/currencies";import { HttpTypes } 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 CreateCommissionRateSchema = zod.object({name: zod.string().min(1),@@ -38,15 +55,25 @@ const CreateCommissionRateSchema = zod.object({include_tax: zod.boolean(),is_enabled: 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(),});export const CreateCommissionRateForm = ({store,}: {- store: HttpTypes.Store;+ store: HttpTypes.AdminStore;}) => {const { t } = useTranslation();const { handleSuccess } = useRouteModal();+ const { setIsOpen } = useStackedModal();+ const direction = useDocumentDirection();const form = useForm<zod.infer<typeof CreateCommissionRateSchema>>({defaultValues: {@@ -60,6 +87,14 @@ export const CreateCommissionRateForm = ({include_tax: false,is_enabled: true,priority: 0,+ enabled_rules: {+ product: false,+ product_type: false,+ shipping_option: false,+ },+ product: [],+ product_type: [],+ shipping_option: [],},resolver: zodResolver(CreateCommissionRateSchema),});@@ -81,6 +116,21 @@ export const CreateCommissionRateForm = ({useCreateCommissionRate();const handleSubmit = form.handleSubmit(async (values) => {+ const rules = [+ ...(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,+ })),+ ];+await createCommissionRate({name: values.name,@@ -93,6 +143,7 @@ export const CreateCommissionRateForm = ({include_tax: values.include_tax,is_enabled: values.is_enabled,priority: values.priority,+ ...(rules.length > 0 ? { rules } : {}),},{onSuccess: ({ commission_rate }) => {@@ -109,6 +160,137 @@ export const CreateCommissionRateForm = ({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, append } = 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));+ }+ }++ append(fieldsToAdd);+ setIsOpen(modalId, false);+ };+ };++ const displayOrder = new Set<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(b.value) - orderArray.indexOf(a.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,+ );+return (<RouteFocusModal.Form form={form}><KeyboundForm@@ -207,6 +389,33 @@ export const CreateCommissionRateForm = ({/></div><div className="grid grid-cols-1 gap-4 md:grid-cols-2">+ <Form.Field+ control={form.control}+ name="currency_code"+ render={({ field: { onChange, ref, ...field } }) => (+ <Form.Item>+ <Form.Label>Currency Code</Form.Label>+ <Form.Control>+ <Select {...field} onValueChange={onChange}>+ <Select.Trigger ref={ref}>+ <Select.Value placeholder="Select currency" />+ </Select.Trigger>+ <Select.Content>+ {storeCurrencies.map((currency) => (+ <Select.Item+ value={currency.code.toLowerCase()}+ key={currency.code}+ >+ {currency.name}+ </Select.Item>+ ))}+ </Select.Content>+ </Select>+ </Form.Control>+ <Form.ErrorMessage />+ </Form.Item>+ )}+ /><Form.Fieldcontrol={form.control}name="value"@@ -237,43 +446,24 @@ export const CreateCommissionRateForm = ({</Form.Item>)}/>- <Form.Field- control={form.control}- name="priority"- render={({ field }) => (- <Form.Item>- <Form.Label>Priority</Form.Label>- <Form.Control>- <Input type="number" {...field} />- </Form.Control>- <Form.ErrorMessage />- </Form.Item>- )}- /></div><div className="grid grid-cols-1 gap-4 md:grid-cols-2"><Form.Fieldcontrol={form.control}- name="currency_code"- render={({ field: { onChange, ref, ...field } }) => (+ name="min_amount"+ render={({ field: { value, onChange, ...field } }) => (<Form.Item>- <Form.Label>Currency Code</Form.Label>+ <Form.Label>Minimum Amount</Form.Label><Form.Control>- <Select {...field} onValueChange={onChange}>- <Select.Trigger ref={ref}>- <Select.Value placeholder="Select currency" />- </Select.Trigger>- <Select.Content>- {storeCurrencies.map((currency) => (- <Select.Item- value={currency.code.toLowerCase()}- key={currency.code}- >- {currency.name}- </Select.Item>- ))}- </Select.Content>- </Select>+ <CurrencyInput+ {...field}+ min={0}+ code={watchCurrency || defaultCurrencyCode}+ value={value}+ onValueChange={(_value, _name, values) =>+ onChange(values?.float ?? 0)+ }+ /></Form.Control><Form.ErrorMessage /></Form.Item>@@ -281,10 +471,10 @@ export const CreateCommissionRateForm = ({/><Form.Fieldcontrol={form.control}- name="min_amount"+ name="priority"render={({ field }) => (<Form.Item>- <Form.Label>Minimum Amount</Form.Label>+ <Form.Label>Priority</Form.Label><Form.Control><Input type="number" {...field} /></Form.Control>@@ -338,6 +528,199 @@ export const CreateCommissionRateForm = ({</Form.Item>)}/>+ <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 { fields, remove } = getControls(type);+ const handler = getFieldHandler(type);++ const modalId = getStackedModalId(type);++ const handleChangeType = (+ value: TaxRateRuleReferenceType,+ ) => {+ disableRule(type);+ enableRule(value);+ };++ return (+ <div key={type}>+ <Form.Field+ control={form.control}+ name={ruleType.value}+ render={({ 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">+ <StackedFocusModal id={modalId}>+ <StackedFocusModal.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>+ </StackedFocusModal.Trigger>+ <StackedFocusModal.Trigger asChild>+ <Button variant="secondary">+ {t("actions.browse")}+ </Button>+ </StackedFocusModal.Trigger>+ <StackedFocusModal.Content>+ <StackedFocusModal.Header>+ <StackedFocusModal.Title asChild>+ <Heading className="sr-only">+ {t(+ "taxRegions.fields.targets.modal.header",+ )}+ </Heading>+ </StackedFocusModal.Title>+ <StackedFocusModal.Description className="sr-only">+ {t(+ "taxRegions.fields.targets.hint",+ )}+ </StackedFocusModal.Description>+ </StackedFocusModal.Header>+ <TargetForm+ type="focus"+ referenceType={type}+ state={fields}+ setState={handler}+ />+ </StackedFocusModal.Content>+ </StackedFocusModal>+ <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}+ onRemove={remove}+ value={field.value}+ />+ );+ })}+ </div>+ </div>+ ) : null}+ </div>+ <Form.ErrorMessage className="mt-2" />+ </Form.Item>+ );+ }}+ />+ </div>+ );+ })}+ </div>+ </div></div></div></RouteFocusModal.Body>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.tsxindex 18d8e5a56..df6896c3c 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@@ -166,6 +166,33 @@ export const EditCommissionRateForm = ({</Form.Item>)}/>+ <Form.Field+ control={form.control}+ name="currency_code"+ render={({ field: { onChange, ref, ...field } }) => (+ <Form.Item>+ <Form.Label>Currency Code</Form.Label>+ <Form.Control>+ <Select {...field} onValueChange={onChange}>+ <Select.Trigger ref={ref}>+ <Select.Value placeholder="Select currency" />+ </Select.Trigger>+ <Select.Content>+ {storeCurrencies.map((currency) => (+ <Select.Item+ value={currency.code.toLowerCase()}+ key={currency.code}+ >+ {currency.name}+ </Select.Item>+ ))}+ </Select.Content>+ </Select>+ </Form.Control>+ <Form.ErrorMessage />+ </Form.Item>+ )}+ /><Form.Fieldcontrol={form.control}name="value"@@ -198,39 +225,20 @@ export const EditCommissionRateForm = ({/><Form.Fieldcontrol={form.control}- name="priority"- render={({ field }) => (- <Form.Item>- <Form.Label>Priority</Form.Label>- <Form.Control>- <Input type="number" {...field} />- </Form.Control>- <Form.ErrorMessage />- </Form.Item>- )}- />- <Form.Field- control={form.control}- name="currency_code"- render={({ field: { onChange, ref, ...field } }) => (+ name="min_amount"+ render={({ field: { value, onChange, ...field } }) => (<Form.Item>- <Form.Label>Currency Code</Form.Label>+ <Form.Label>Minimum Amount</Form.Label><Form.Control>- <Select {...field} onValueChange={onChange}>- <Select.Trigger ref={ref}>- <Select.Value placeholder="Select currency" />- </Select.Trigger>- <Select.Content>- {storeCurrencies.map((currency) => (- <Select.Item- value={currency.code.toLowerCase()}- key={currency.code}- >- {currency.name}- </Select.Item>- ))}- </Select.Content>- </Select>+ <CurrencyInput+ {...field}+ min={0}+ code={watchCurrency || defaultCurrencyCode}+ value={value}+ onValueChange={(_value, _name, values) =>+ onChange(values?.float ?? 0)+ }+ /></Form.Control><Form.ErrorMessage /></Form.Item>@@ -238,10 +246,10 @@ export const EditCommissionRateForm = ({/><Form.Fieldcontrol={form.control}- name="min_amount"+ name="priority"render={({ field }) => (<Form.Item>- <Form.Label>Minimum Amount</Form.Label>+ <Form.Label>Priority</Form.Label><Form.Control><Input type="number" {...field} /></Form.Control>