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