Skip to content

Commit

Permalink
⚡️ perf(subscribe): Form discount price
Browse files Browse the repository at this point in the history
  • Loading branch information
web-ppanel committed Nov 14, 2024
1 parent a7927d7 commit 059a892
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
29 changes: 28 additions & 1 deletion apps/admin/app/dashboard/subscribe/subscribe-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Combobox } from '@repo/ui/combobox';
import { ArrayInput } from '@repo/ui/dynamic-Inputs';
import { JSONEditor } from '@repo/ui/editor';
import { EnhancedInput } from '@repo/ui/enhanced-input';
import { unitConversion } from '@repo/ui/utils';
import { evaluateWithPrecision, unitConversion } from '@repo/ui/utils';
import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from '@shadcn/ui/accordion';
import { Button } from '@shadcn/ui/button';
import { Checkbox } from '@shadcn/ui/checkbox';
Expand Down Expand Up @@ -448,6 +448,33 @@ export default function SubscribeForm<T extends Record<string, any>>({
max: 100,
placeholder: t('form.discountPercent'),
suffix: '%',
calculateValue: (data) => {
const { unit_price } = form.getValues();
return {
...data,
price: evaluateWithPrecision(
`${unit_price} * ${data.months} * ${data.discount} / 100`,
),
};
},
},
{
name: 'price',
placeholder: t('form.discount_price'),
type: 'number',
min: 0,
formatInput: (value) => unitConversion('centsToDollars', value),
formatOutput: (value) => unitConversion('dollarsToCents', value),
internal: true,
calculateValue: (data) => {
const { unit_price } = form.getValues();
return {
...data,
discount: evaluateWithPrecision(
`${data.price} / ${data.months} / ${unit_price} * 100`,
),
};
},
},
]}
value={field.value}
Expand Down
1 change: 1 addition & 0 deletions apps/admin/locales/en-US/subscribe.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"discountDescription": "Set discount based on unit price",
"discountMonths": "Months",
"discountPercent": "Discount Percentage",
"discount_price": "Discount Price",
"duration": "Duration (months)",
"inventory": "Inventory",
"name": "Name",
Expand Down
1 change: 1 addition & 0 deletions apps/admin/locales/zh-CN/subscribe.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"discountDescription": "基于单价设置折扣",
"discountMonths": "",
"discountPercent": "折扣比",
"discount_price": "折扣价格",
"duration": "时长(月)",
"inventory": "库存",
"name": "名称",
Expand Down

0 comments on commit 059a892

Please sign in to comment.