feat(subscribe): Add unit time

This commit is contained in:
web@ppanel
2024-12-20 18:52:30 +07:00
parent d8b0bd9c2d
commit 39d07ecee5
80 changed files with 553 additions and 165 deletions
@@ -15,7 +15,6 @@ import {
SheetTrigger,
} from '@shadcn/ui/sheet';
import { useTranslations } from 'next-intl';
import { useTheme } from 'next-themes';
import { useEffect, useState } from 'react';
const formSchema = z.object({
@@ -39,7 +38,6 @@ export default function AnnouncementForm<T extends Record<string, any>>({
title,
}: AnnouncementFormProps<T>) {
const t = useTranslations('announcement');
const { resolvedTheme } = useTheme();
const [open, setOpen] = useState(false);
const form = useForm({
resolver: zodResolver(formSchema),
@@ -16,7 +16,6 @@ import {
SheetTrigger,
} from '@shadcn/ui/sheet';
import { useTranslations } from 'next-intl';
import { useTheme } from 'next-themes';
import { useEffect, useState } from 'react';
const formSchema = z.object({
@@ -41,7 +40,6 @@ export default function DocumentForm<T extends Record<string, any>>({
title,
}: DocumentFormProps<T>) {
const t = useTranslations('document');
const { resolvedTheme } = useTheme();
const [open, setOpen] = useState(false);
const form = useForm({
resolver: zodResolver(formSchema),
@@ -32,7 +32,6 @@ import {
} from '@shadcn/ui/sheet';
import { useQuery } from '@tanstack/react-query';
import { useTranslations } from 'next-intl';
import { useTheme } from 'next-themes';
import { assign, shake } from 'radash';
import { useEffect, useState } from 'react';
@@ -53,6 +52,7 @@ const defaultValues = {
discount: [],
server_group: [],
server: [],
unit_time: 'Month',
};
export default function SubscribeForm<T extends Record<string, any>>({
@@ -63,7 +63,6 @@ export default function SubscribeForm<T extends Record<string, any>>({
title,
}: SubscribeFormProps<T>) {
const t = useTranslations('subscribe');
const { resolvedTheme } = useTheme();
const [open, setOpen] = useState(false);
@@ -71,11 +70,12 @@ export default function SubscribeForm<T extends Record<string, any>>({
name: z.string(),
description: z.string().optional(),
unit_price: z.number(),
unit_time: z.string().default('Month'),
replacement: z.number().optional(),
discount: z
.array(
z.object({
months: z.number(),
quantity: z.number(),
discount: z.number(),
}),
)
@@ -135,6 +135,7 @@ export default function SubscribeForm<T extends Record<string, any>>({
},
});
const unit_time = form.watch('unit_time');
return (
<Sheet open={open} onOpenChange={setOpen}>
<SheetTrigger asChild>
@@ -255,7 +256,7 @@ export default function SubscribeForm<T extends Record<string, any>>({
name='unit_price'
render={({ field }) => (
<FormItem>
<FormLabel>{t('form.unit_price')}</FormLabel>
<FormLabel>{t('form.unitPrice')}</FormLabel>
<FormControl>
<EnhancedInput
type='number'
@@ -272,6 +273,34 @@ export default function SubscribeForm<T extends Record<string, any>>({
</FormItem>
)}
/>
<FormField
control={form.control}
name='unit_time'
render={({ field }) => (
<FormItem>
<FormLabel>{t('form.unitTime')}</FormLabel>
<FormControl>
<Combobox
placeholder={t('form.selectUnitTime')}
{...field}
onChange={(value) => {
if (value) {
form.setValue(field.name, value);
}
}}
options={[
{ label: t('form.Year'), value: 'Year' },
{ label: t('form.Month'), value: 'Month' },
{ label: t('form.Day'), value: 'Day' },
{ label: t('form.Hour'), value: 'Hour' },
{ label: t('form.Minute'), value: 'Minute' },
]}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name='replacement'
@@ -436,10 +465,10 @@ export default function SubscribeForm<T extends Record<string, any>>({
<ArrayInput<API.SubscribeDiscount>
fields={[
{
name: 'months',
name: 'quantity',
type: 'number',
min: 1,
suffix: t('form.discountMonths'),
suffix: unit_time && t(`form.${unit_time}`),
},
{
name: 'discount',
@@ -453,7 +482,7 @@ export default function SubscribeForm<T extends Record<string, any>>({
return {
...data,
price: evaluateWithPrecision(
`${unit_price} * ${data.months} * ${data.discount} / 100`,
`${unit_price} * ${data.quantity} * ${data.discount} / 100`,
),
};
},
@@ -471,7 +500,7 @@ export default function SubscribeForm<T extends Record<string, any>>({
return {
...data,
discount: evaluateWithPrecision(
`${data.price} / ${data.months} / ${unit_price} * 100`,
`${data.price} / ${data.quantity} / ${unit_price} * 100`,
),
};
},