From 22ea55733746dcc18063261a2be3799bcdbeee6c Mon Sep 17 00:00:00 2001 From: Ahmed Bouhuolia Date: Tue, 5 Nov 2024 13:33:22 +0200 Subject: [PATCH] feat: wip invoice paper template server-side --- .../src/components/InvoicePaperTemplate.tsx | 17 -- .../src/components/PaperTemplate.tsx | 151 +++++++++++------- .../src/components/PaperTemplateLayout.tsx | 21 ++- shared/pdf-templates/src/index.ts | 1 + .../renders/render-invoice-paper-template.tsx | 36 +++++ 5 files changed, 136 insertions(+), 90 deletions(-) create mode 100644 shared/pdf-templates/src/renders/render-invoice-paper-template.tsx diff --git a/shared/pdf-templates/src/components/InvoicePaperTemplate.tsx b/shared/pdf-templates/src/components/InvoicePaperTemplate.tsx index 2d29c0db9..0c0dbfa4b 100644 --- a/shared/pdf-templates/src/components/InvoicePaperTemplate.tsx +++ b/shared/pdf-templates/src/components/InvoicePaperTemplate.tsx @@ -1,10 +1,8 @@ -import { renderToString } from 'react-dom/server'; import { PaperTemplate, PaperTemplateProps, PaperTemplateTotalBorder, } from './PaperTemplate'; -import { x } from '@xstyled/emotion'; import { Box } from '../lib/layout/Box'; import { Text } from '../lib/text/Text'; import { Stack } from '../lib/layout/Stack'; @@ -17,8 +15,6 @@ import { DefaultPdfTemplateAddressBilledTo, DefaultPdfTemplateAddressBilledFrom, } from './_constants'; -import { PaperTemplateLayout } from './PaperTemplateLayout'; -import createCache from '@emotion/cache'; interface PapaerLine { item?: string; @@ -328,16 +324,3 @@ export function InvoicePaperTemplate({ ); } - -export const renderInvoicePaperTemplateHtml = ( - props: InvoicePaperTemplateProps -) => { - const key = 'custom'; - const cache = createCache({ key }); - - return renderToString( - - - - ); -}; diff --git a/shared/pdf-templates/src/components/PaperTemplate.tsx b/shared/pdf-templates/src/components/PaperTemplate.tsx index 73bd4f73a..d51a7ca3b 100644 --- a/shared/pdf-templates/src/components/PaperTemplate.tsx +++ b/shared/pdf-templates/src/components/PaperTemplate.tsx @@ -2,30 +2,10 @@ import React from 'react'; import clsx from 'classnames'; import { get, isFunction } from 'lodash'; import { x } from '@xstyled/emotion'; +import { css } from '@emotion/css'; import { Box, BoxProps } from '../lib/layout/Box'; import { Group, GroupProps } from '../lib/layout/Group'; -const styles = { - root: 'root', - bigTitle: 'bigTitle', - logoWrap: 'logoWrap', - logoImg: 'logoImg', - table: 'table', - tableBody: 'tableBody', - totals: 'totals', - totalsItem: 'totalsItem', - totalBottomBordered: 'totalBottomBordered', - totalBottomGrayBordered: 'totalBottomGrayBordered', - totalsItemLabel: 'totalsItemLabel', - totalsItemAmount: 'totalsItemAmount', - addressRoot: 'addressRoot', - paragraph: 'paragraph', - paragraphLabel: 'paragraphLabel', - details: 'details', - detail: 'detail', - detailLabel: 'detailLabel', -}; - export interface PaperTemplateProps extends BoxProps { primaryColor?: string; secondaryColor?: string; @@ -51,7 +31,7 @@ export function PaperTemplate({ h="1123px" w="794px" {...restProps} - className={clsx(styles.root, restProps?.className)} + className={restProps?.className} > {children} @@ -76,14 +56,11 @@ interface PaperTemplateBigTitleProps { PaperTemplate.BigTitle = ({ title }: PaperTemplateBigTitleProps) => { return ( {title} @@ -96,15 +73,63 @@ interface PaperTemplateLogoProps { PaperTemplate.Logo = ({ logoUri }: PaperTemplateLogoProps) => { return ( -
- -
+ + + ); }; PaperTemplate.Table = ({ columns, data }: PaperTemplateTableProps) => { return ( - +
{columns.map((col, index) => ( @@ -115,7 +140,7 @@ PaperTemplate.Table = ({ columns, data }: PaperTemplateTableProps) => { - + {data.map((_data: any) => ( {columns.map((column, index) => ( @@ -148,8 +173,16 @@ PaperTemplate.Totals = ({ children }: { children: React.ReactNode }) => { }} > {children} - ); + + ); }; + +const totalBottomBordered = css` + border-bottom: 1px solid #000; +`; +const totalBottomGrayBordered = css` + border-bottom: 1px solid #dadada; +`; PaperTemplate.TotalLine = ({ label, amount, @@ -165,34 +198,34 @@ PaperTemplate.TotalLine = ({ {label} - {amount} + + {amount} + ); }; -PaperTemplate.MutedText = () => { }; - -PaperTemplate.Text = () => { }; - PaperTemplate.AddressesGroup = (props: GroupProps) => { return ( div { + flex: 1; + } + `} /> ); }; + PaperTemplate.Address = ({ children }: { children: React.ReactNode }) => { return {children}; }; @@ -205,22 +238,16 @@ PaperTemplate.Statement = ({ children: React.ReactNode; }) => { return ( -
- {label &&
{label}
} -
{children}
-
+ + {label && {label}} + {children} + ); }; PaperTemplate.TermsList = ({ children }: { children: React.ReactNode }) => { return ( - + {children} ); @@ -234,9 +261,9 @@ PaperTemplate.TermsItem = ({ children: React.ReactNode; }) => { return ( - - {label} + + {label} {children} - +
); }; diff --git a/shared/pdf-templates/src/components/PaperTemplateLayout.tsx b/shared/pdf-templates/src/components/PaperTemplateLayout.tsx index d2e767837..5d8c233af 100644 --- a/shared/pdf-templates/src/components/PaperTemplateLayout.tsx +++ b/shared/pdf-templates/src/components/PaperTemplateLayout.tsx @@ -1,25 +1,24 @@ import { CacheProvider, ThemeProvider } from '@emotion/react'; import { EmotionCache } from '@emotion/cache'; import { defaultTheme } from '@xstyled/system'; +import { Preflight } from '@xstyled/emotion'; const theme = { ...defaultTheme, }; -export function PaperTemplateLayout({ cache, children }: { +export function PaperTemplateLayout({ + cache, + children, +}: { children: React.ReactNode; cache: EmotionCache; }) { - const html = ( + return ( - {children} + + + {children} + ); - - return ( - - -
{html}
- - - ); } diff --git a/shared/pdf-templates/src/index.ts b/shared/pdf-templates/src/index.ts index 8a15d72ed..4b0a66437 100644 --- a/shared/pdf-templates/src/index.ts +++ b/shared/pdf-templates/src/index.ts @@ -1,2 +1,3 @@ export * from './components/PaperTemplate'; export * from './components/InvoicePaperTemplate'; +export * from './renders/render-invoice-paper-template'; diff --git a/shared/pdf-templates/src/renders/render-invoice-paper-template.tsx b/shared/pdf-templates/src/renders/render-invoice-paper-template.tsx new file mode 100644 index 000000000..dea926919 --- /dev/null +++ b/shared/pdf-templates/src/renders/render-invoice-paper-template.tsx @@ -0,0 +1,36 @@ +import { renderToString } from 'react-dom/server'; +import createCache from '@emotion/cache'; +import { + InvoicePaperTemplate, + InvoicePaperTemplateProps, +} from '../components/InvoicePaperTemplate'; +import { PaperTemplateLayout } from '../components/PaperTemplateLayout'; +import { extractCritical } from '@emotion/server'; + +export const renderInvoicePaperTemplateHtml = ( + props: InvoicePaperTemplateProps +) => { + const key = 'invoice-paper-template'; + const cache = createCache({ key }); + + const renderedHtml = renderToString( + + + + ); + const { html, css, ids } = extractCritical(renderedHtml); + + return ` + + + + + + Invoice + + + +
${html}
+ +`; +};