|
| 1 | +import { createIdFromString, type Id } from "@evolu/common"; |
| 2 | +import { merge } from "es-toolkit"; |
| 3 | +import type { TFunction } from "i18next"; |
| 4 | +import type React from "react"; |
| 5 | +import { useMemo, useState } from "react"; |
| 6 | +import { useTranslation } from "react-i18next"; |
| 7 | +import type { PartialDeep } from "type-fest"; |
| 8 | +import { z } from "zod"; |
| 9 | +import { AutoForm, createAutoFormLayout } from "@/components/auto-form"; |
| 10 | +import { useActionForm } from "@/hooks/use-action-form"; |
| 11 | +import { useEvolu } from "@/hooks/use-evolu"; |
| 12 | +import { |
| 13 | + NonEmptyString32Schema, |
| 14 | + PositiveIntegerSchema, |
| 15 | + StringToNullableStringSchema, |
| 16 | + StringToNumberSchema, |
| 17 | +} from "@/lib/shared/types"; |
| 18 | + |
| 19 | +export const paymentReceiptNumberSeriesFormSchema = z.object({ |
| 20 | + serialNumberDigits: StringToNumberSchema.pipe(PositiveIntegerSchema), |
| 21 | + yearFormat: z.enum(["default", "short"]), |
| 22 | + monthFormat: z.enum(["default", "hidden"]), |
| 23 | + dayFormat: z.enum(["default", "hidden"]), |
| 24 | + prefix: StringToNullableStringSchema.pipe(NonEmptyString32Schema.nullable()), |
| 25 | +}); |
| 26 | + |
| 27 | +export const createPaymentReceiptNumberSeriesDefaultValues = () => |
| 28 | + ({ |
| 29 | + serialNumberDigits: "4", |
| 30 | + yearFormat: "default", |
| 31 | + monthFormat: "hidden", |
| 32 | + dayFormat: "hidden", |
| 33 | + prefix: "R", |
| 34 | + }) satisfies z.input<typeof paymentReceiptNumberSeriesFormSchema>; |
| 35 | + |
| 36 | +const now = new Date(); |
| 37 | + |
| 38 | +const createComponents = (t: TFunction) => |
| 39 | + createAutoFormLayout(paymentReceiptNumberSeriesFormSchema, ({ builder }) => ({ |
| 40 | + ...builder.magicInput("serialNumberDigits").text({ |
| 41 | + label: t( |
| 42 | + "settings:form.receipt-number-series-form.label.number-of-digits", |
| 43 | + ), |
| 44 | + type: "number", |
| 45 | + description: t( |
| 46 | + "settings:form.receipt-number-series-form.description.if-you-dont-issue-more-than-9999-receipts-per-time-period-number-4-will-be-opt", |
| 47 | + ), |
| 48 | + }), |
| 49 | + |
| 50 | + ...builder.magicInput("yearFormat").select({ |
| 51 | + values: { |
| 52 | + default: `${t("settings:form.receipt-number-series-form.option.default")} (${now.getFullYear()})`, |
| 53 | + short: `${t("settings:form.receipt-number-series-form.option.short")} (${now.getFullYear().toString().substring(2)})`, |
| 54 | + }, |
| 55 | + allowEmpty: false, |
| 56 | + label: t("settings:form.receipt-number-series-form.label.year-format"), |
| 57 | + variant: "toggle", |
| 58 | + }), |
| 59 | + |
| 60 | + ...builder.magicInput("monthFormat").select({ |
| 61 | + values: { |
| 62 | + default: `${t("settings:form.receipt-number-series-form.option.default")} (${(now.getMonth() + 1).toString().padStart(2, "0")})`, |
| 63 | + hidden: t("settings:form.receipt-number-series-form.option.hidden"), |
| 64 | + }, |
| 65 | + allowEmpty: false, |
| 66 | + label: t("settings:form.receipt-number-series-form.label.month-format"), |
| 67 | + variant: "toggle", |
| 68 | + }), |
| 69 | + |
| 70 | + ...builder.when("monthFormat", (value) => value !== "hidden", { |
| 71 | + ...builder.magicInput("dayFormat").select({ |
| 72 | + values: { |
| 73 | + default: `${t("settings:form.receipt-number-series-form.option.default")} (${now.getDate().toString().padStart(2, "0")})`, |
| 74 | + hidden: t("settings:form.receipt-number-series-form.option.hidden"), |
| 75 | + }, |
| 76 | + allowEmpty: false, |
| 77 | + label: t("settings:form.receipt-number-series-form.label.day-format"), |
| 78 | + variant: "toggle", |
| 79 | + }), |
| 80 | + }), |
| 81 | + |
| 82 | + ...builder.magicInput("prefix").text({ |
| 83 | + label: t( |
| 84 | + "settings:form.receipt-number-series-form.label.receipt-number-prefix", |
| 85 | + ), |
| 86 | + }), |
| 87 | + })); |
| 88 | + |
| 89 | +export const PaymentReceiptNumberSeriesForm: React.FC<{ |
| 90 | + defaultValues?: PartialDeep< |
| 91 | + z.input<typeof paymentReceiptNumberSeriesFormSchema> |
| 92 | + >; |
| 93 | + onSuccess?: (newEventId: Id) => unknown; |
| 94 | +}> = (params) => { |
| 95 | + const { t } = useTranslation(); |
| 96 | + const evolu = useEvolu(); |
| 97 | + const [defaultValues] = useState(() => |
| 98 | + merge( |
| 99 | + createPaymentReceiptNumberSeriesDefaultValues(), |
| 100 | + params.defaultValues ?? {}, |
| 101 | + ), |
| 102 | + ); |
| 103 | + const components = useMemo(() => createComponents(t), [t]); |
| 104 | + const form = useActionForm(paymentReceiptNumberSeriesFormSchema, { |
| 105 | + defaultValues, |
| 106 | + saveAction: async (values) => { |
| 107 | + const id = createIdFromString(""); |
| 108 | + |
| 109 | + evolu.upsert( |
| 110 | + "paymentReceiptNumberSeries", |
| 111 | + { |
| 112 | + ...values, |
| 113 | + id, |
| 114 | + }, |
| 115 | + { |
| 116 | + onComplete: () => { |
| 117 | + params.onSuccess?.(id); |
| 118 | + }, |
| 119 | + }, |
| 120 | + ); |
| 121 | + }, |
| 122 | + }); |
| 123 | + |
| 124 | + return <AutoForm form={form} components={components} />; |
| 125 | +}; |
0 commit comments