Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

11.7 KB
Raw Blame
import { zodResolver } from '@hookform/resolvers/zod'
import type { CustomerGroup, CustomerGroupCreateParams } from '@spree/admin-sdk'
import {
  adminClient,
  Can,
  extensionFormValues,
  extensionSubmitValues,
  HostFormSlot,
  mapSpreeErrorsToForm,
  ResourceTable,
  resourceSearchSchema,
  Subject,
  usePermissions,
} from '@spree/dashboard-core'
import {
  Button,
  Field,
  FieldGroup,
  FieldLabel,
  Input,
  Item,
  ItemActions,
  ItemContent,
  ItemMedia,
  ItemTitle,
  RowActions,
  Sheet,
  SheetContent,
  SheetDescription,
  SheetFooter,
  SheetHeader,
  SheetTitle,
  Textarea,
  useConfirm,
  useRowClickBridge,
} from '@spree/dashboard-ui'
import { PlusIcon, UsersIcon } from '@spree/dashboard-ui/icons'
import { createFileRoute, Link, useNavigate } from '@tanstack/react-router'
import { useEffect } from 'react'
import { type UseFormReturn, useForm } from 'react-hook-form'
import { useTranslation } from 'react-i18next'
import { z } from 'zod/v4'
import {
  useCreateCustomerGroup,
  useCustomerGroup,
  useDeleteCustomerGroup,
  useUpdateCustomerGroup,
} from '../../../../hooks/use-customer-groups'
import {
  CUSTOMER_GROUP_DEFAULTS,
  type CustomerGroupFormValues,
  customerGroupFormSchema,
  customerGroupValuesToParams,
} from '../../../../schemas/customer-group'
import '../../../../tables/customer-groups'

const customerGroupsSearchSchema = resourceSearchSchema.extend({
  edit: z.string().optional(),
  new: z.coerce.boolean().optional(),
})

export const Route = createFileRoute('/_authenticated/$storeId/customers/groups')({
  validateSearch: customerGroupsSearchSchema,
  component: CustomerGroupsPage,
})

function CustomerGroupsPage() {
  const { t } = useTranslation()
  const search = Route.useSearch() as z.infer<typeof customerGroupsSearchSchema>
  const navigate = useNavigate()
  const confirm = useConfirm()
  const deleteMutation = useDeleteCustomerGroup()
  const { permissions } = usePermissions()

  const editId = search.edit
  const isCreating = !!search.new

  const closeSheet = () =>
    navigate({
      search: (prev: Record<string, unknown>) => {
        const { edit: _e, new: _n, ...rest } = prev
        return rest as never
      },
    })

  const openCreate = () =>
    navigate({ search: (prev: Record<string, unknown>) => ({ ...prev, new: true }) as never })

  const openEdit = (id: string) =>
    navigate({ search: (prev: Record<string, unknown>) => ({ ...prev, edit: id }) as never })

  useRowClickBridge('data-customer-group-id', openEdit)

  async function handleDelete(group: CustomerGroup) {
    const ok = await confirm({
      title: t('admin.customers.groups.delete_confirm.title'),
      message: t('admin.customers.groups.delete_confirm.message', {
        name: group.name ?? t('admin.customers.groups.default_name'),
      }),
      variant: 'destructive',
      confirmLabel: t('admin.actions.delete'),
    })
    if (!ok) return
    // `useDeleteCustomerGroup` toasts on success/error via `onError`; the
    // `.catch` only swallows the rethrow so the row-action callback doesn't
    // surface an unhandled rejection.
    await deleteMutation.mutateAsync(group.id).catch(() => undefined)
  }

  return (
    <>
      <ResourceTable<CustomerGroup>
        tableKey="customer-groups"
        queryKey="customer-groups"
        queryFn={(params) => adminClient.customerGroups.list(params)}
        searchParams={search}
        rowActions={(group) => (
          <RowActions
            actions={[
              { key: 'edit', onSelect: () => openEdit(group.id) },
              {
                key: 'delete',
                destructive: true,
                visible: permissions.can('destroy', Subject.CustomerGroup),
                disabled: deleteMutation.isPending,
                onSelect: () => handleDelete(group),
              },
            ]}
          />
        )}
        actions={
          <Can I="create" a={Subject.CustomerGroup}>
            <Button size="sm" className="h-[2.125rem]" onClick={openCreate}>
              <PlusIcon className="size-4" />
              {t('admin.pages.customers.groups.add_cta')}
            </Button>
          </Can>
        }
      />

      {isCreating && <CreateCustomerGroupSheet open onOpenChange={(o) => !o && closeSheet()} />}
      {editId && (
        <EditCustomerGroupSheet id={editId} open onOpenChange={(o) => !o && closeSheet()} />
      )}
    </>
  )
}

function CreateCustomerGroupSheet({
  open,
  onOpenChange,
}: {
  open: boolean
  onOpenChange: (open: boolean) => void
}) {
  const { t } = useTranslation()
  const createMutation = useCreateCustomerGroup()
  const form = useForm<CustomerGroupFormValues>({
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    resolver: zodResolver(customerGroupFormSchema) as any,
    defaultValues: { ...CUSTOMER_GROUP_DEFAULTS, ...extensionFormValues('customer_group', null) },
  })

  async function onSubmit(values: CustomerGroupFormValues) {
    try {
      await createMutation.mutateAsync({
        ...(customerGroupValuesToParams(values) as CustomerGroupCreateParams),
        ...extensionSubmitValues('customer_group', form),
      })
      form.reset(CUSTOMER_GROUP_DEFAULTS)
      onOpenChange(false)
    } catch (err) {
      if (!mapSpreeErrorsToForm(err, form.setError)) throw err
    }
  }

  return (
    <Sheet
      open={open}
      onOpenChange={(next) => {
        if (!next) form.reset(CUSTOMER_GROUP_DEFAULTS)
        onOpenChange(next)
      }}
    >
      <SheetContent>
        <SheetHeader>
          <SheetTitle>{t('admin.pages.customers.groups.add_sheet_title')}</SheetTitle>
          <SheetDescription>{t('admin.customers.groups.create_description')}</SheetDescription>
        </SheetHeader>
        <form onSubmit={form.handleSubmit(onSubmit)} className="flex min-h-0 flex-1 flex-col">
          <div className="flex flex-1 flex-col gap-4 overflow-y-auto p-4">
            <NameDescriptionFields form={form} />
            <HostFormSlot
              form={form}
              name="customer_group.form_fields"
              context={{ customerGroup: undefined, mode: 'new' }}
            />
          </div>
          <SheetFooter>
            <Button
              type="button"
              variant="outline"
              onClick={() => onOpenChange(false)}
              disabled={form.formState.isSubmitting}
            >
              {t('admin.actions.cancel')}
            </Button>
            <Button type="submit" disabled={form.formState.isSubmitting}>
              {form.formState.isSubmitting
                ? t('admin.actions.creating')
                : t('admin.customers.groups.create_label')}
            </Button>
          </SheetFooter>
        </form>
      </SheetContent>
    </Sheet>
  )
}

function EditCustomerGroupSheet({
  id,
  open,
  onOpenChange,
}: {
  id: string
  open: boolean
  onOpenChange: (open: boolean) => void
}) {
  const { t } = useTranslation()
  const { storeId } = Route.useParams()
  const { data: group, isLoading } = useCustomerGroup(id)
  const updateMutation = useUpdateCustomerGroup(id)

  const form = useForm<CustomerGroupFormValues>({
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    resolver: zodResolver(customerGroupFormSchema) as any,
    defaultValues: CUSTOMER_GROUP_DEFAULTS,
  })

  useEffect(() => {
    if (group) {
      form.reset({
        name: group.name,
        description: group.description ?? '',
        ...extensionFormValues('customer_group', group),
      })
    }
  }, [group, form])

  async function onSubmit(values: CustomerGroupFormValues) {
    try {
      await updateMutation.mutateAsync({
        ...customerGroupValuesToParams(values),
        ...extensionSubmitValues('customer_group', form),
      })
      form.reset(values)
      onOpenChange(false)
    } catch (err) {
      if (!mapSpreeErrorsToForm(err, form.setError)) throw err
    }
  }

  // Filter the /customers index by this group's id. Ransack join filter on the
  // `customer_groups` association — server uses `customer_groups_id_in`.
  const membersFilter = JSON.stringify([
    { id: 'group', field: 'customer_groups_id', operator: 'in', value: id },
  ])

  return (
    <Sheet open={open} onOpenChange={onOpenChange}>
      <SheetContent>
        <SheetHeader>
          <SheetTitle>
            {group?.name ?? t('admin.pages.customers.groups.edit_sheet_title')}
          </SheetTitle>
          <SheetDescription>{t('admin.customers.groups.edit_description')}</SheetDescription>
        </SheetHeader>
        {isLoading ? (
          <div className="p-4 text-sm text-muted-foreground">{t('admin.common.loading')}</div>
        ) : (
          <form onSubmit={form.handleSubmit(onSubmit)} className="flex min-h-0 flex-1 flex-col">
            <div className="flex flex-1 flex-col gap-4 overflow-y-auto p-4">
              <NameDescriptionFields form={form} />
              <HostFormSlot
                form={form}
                name="customer_group.form_fields"
                context={{ customerGroup: group, mode: 'edit' }}
              />
              <MembersSummary
                count={group?.customers_count ?? 0}
                href={`/${storeId}/customers?filters=${encodeURIComponent(membersFilter)}`}
              />
            </div>
            <SheetFooter>
              <Button
                type="button"
                variant="outline"
                onClick={() => onOpenChange(false)}
                disabled={form.formState.isSubmitting}
              >
                {t('admin.actions.cancel')}
              </Button>
              <Button
                type="submit"
                disabled={form.formState.isSubmitting || !form.formState.isDirty}
              >
                {form.formState.isSubmitting ? t('admin.actions.saving') : t('admin.actions.save')}
              </Button>
            </SheetFooter>
          </form>
        )}
      </SheetContent>
    </Sheet>
  )
}

function NameDescriptionFields({ form }: { form: UseFormReturn<CustomerGroupFormValues> }) {
  const { t } = useTranslation()
  const { errors } = form.formState
  return (
    <FieldGroup>
      {errors.root?.message && (
        <p className="text-sm text-destructive" role="alert">
          {errors.root.message}
        </p>
      )}
      <Field>
        <FieldLabel htmlFor="name">{t('admin.fields.name.label')}</FieldLabel>
        <Input
          id="name"
          autoFocus
          placeholder={t('admin.fields.customer_group.name.placeholder')}
          aria-invalid={!!errors.name || undefined}
          {...form.register('name')}
        />
        {errors.name && <p className="text-sm text-destructive">{errors.name.message}</p>}
      </Field>

      <Field>
        <FieldLabel htmlFor="description">{t('admin.fields.description.label')}</FieldLabel>
        <Textarea
          id="description"
          rows={3}
          placeholder={t('admin.fields.customer_group.description.placeholder')}
          aria-invalid={!!errors.description || undefined}
          {...form.register('description')}
        />
        {errors.description && (
          <p className="text-sm text-destructive">{errors.description.message}</p>
        )}
      </Field>
    </FieldGroup>
  )
}

function MembersSummary({ count, href }: { count: number; href: string }) {
  const { t } = useTranslation()
  return (
    <Item variant="muted" size="sm">
      <ItemMedia variant="icon">
        <UsersIcon className="text-muted-foreground" />
      </ItemMedia>
      <ItemContent>
        <ItemTitle className="font-normal">
          {t('admin.customers.groups.count', { count })}
        </ItemTitle>
      </ItemContent>
      <ItemActions>
        <Link to={href as never} className="text-sm font-medium text-primary hover:underline">
          {t('admin.customers.groups.view_members')}
        </Link>
      </ItemActions>
    </Item>
  )
}