feat: add template string

This commit is contained in:
Methapon2001 2024-11-27 13:55:19 +07:00
parent aef712b409
commit 820b81aede

View file

@ -1,26 +1,43 @@
import { formatNumberDecimal } from 'src/stores/utils';
const templates = {
'my-template': {
converter: (context?: number[]) => {
return context?.join(', ') || '';
'quotation-labor': {
converter: (context?: { name: string[] }) => {
return context?.name.join('<br />') || '';
},
},
'quotation-payment': {
converter: (context?: {
paymentType: 'full' | 'installments';
installments: {
date: string;
paymentType:
| 'Full'
| 'Split'
| 'SplitCustom'
| 'BillFull'
| 'BillSplit'
| 'BillSplitCustom';
amount?: number;
installments?: {
no: number;
amount: number;
}[];
}) => {
return (
context?.installments
.map(
if (context?.paymentType === 'Full') {
return [
`**** เงื่อนไขเพิ่มเติม`,
`- เงื่อนไขการชำระเงิน แบบเต็มจำนวน`,
`&nbsp; จำนวน ${formatNumberDecimal(context?.amount || 0, 2)}`,
].join('<br/>');
} else {
return [
`**** เงื่อนไขเพิ่มเติม`,
`- เงื่อนไขการชำระเงิน แบบแบ่งจ่าย${context?.paymentType === 'SplitCustom' ? ' กำหนดเอง ' : ' '}${context?.installments?.length} งวด`,
...(context?.installments?.map(
(v) =>
`asdasd
`,
)
.join(', ') || ''
);
`&nbsp; งาดที่ ${v.no} จำนวน ${formatNumberDecimal(v.amount, 2)}`,
) || []),
].join('<br />');
}
},
},
} as const;
@ -28,7 +45,7 @@ const templates = {
type Template = typeof templates;
type TemplateName = keyof Template;
type TemplateContext = {
[key in TemplateName]: Parameters<Template[key]['converter']>[0];
[key in TemplateName]?: Parameters<Template[key]['converter']>[0];
};
export function convertTemplate(
@ -40,13 +57,13 @@ export function convertTemplate(
for (const [name, template] of Object.entries(templates)) {
if (templateUse && !templateUse.includes(name as TemplateName)) continue;
ret = text.replace(
ret = ret.replace(
new RegExp('\\#\\[' + name.replaceAll('-', '\\-') + '\\]', 'g'),
typeof template.converter === 'function'
? template.converter(context?.[name as TemplateName] as any)
: template.converter,
);
console.log(ret);
}
return ret;