Skip to content

Commit

Permalink
Add ability to save current state into local storage
Browse files Browse the repository at this point in the history
  • Loading branch information
tuanpham-dev committed Aug 31, 2022
1 parent aa6cab9 commit f2616be
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
import React from 'react'
import InvoicePage from './components/InvoicePage'
import { Invoice } from './data/types'

function App() {
const savedInvoice = window.localStorage.getItem('invoiceData')
let data = null

try {
if (savedInvoice) {
data = JSON.parse(savedInvoice)
}
} catch (_e) { }

const onInvoiceUpdated = (invoice: Invoice) => {
window.localStorage.setItem('invoiceData', JSON.stringify(invoice))
}

return (
<div className="app">
<h1 className="center fs-30">React Invoice Generator</h1>
<InvoicePage />
<InvoicePage data={data} onChange={onInvoiceUpdated} />
</div>
)
}
Expand Down
9 changes: 8 additions & 1 deletion src/components/InvoicePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ Font.register({
interface Props {
data?: Invoice
pdfMode?: boolean
onChange?: (invoice: Invoice) => void
}

const InvoicePage: FC<Props> = ({ data, pdfMode }) => {
const InvoicePage: FC<Props> = ({ data, pdfMode, onChange }) => {
const [invoice, setInvoice] = useState<Invoice>(data ? { ...data } : { ...initialInvoice })
const [subTotal, setSubTotal] = useState<number>()
const [saleTax, setSaleTax] = useState<number>()
Expand Down Expand Up @@ -129,6 +130,12 @@ const InvoicePage: FC<Props> = ({ data, pdfMode }) => {
setSaleTax(saleTax)
}, [subTotal, invoice.taxLabel])

useEffect(() => {
if (onChange) {
onChange(invoice)
}
}, [onChange, invoice])

return (
<Document pdfMode={pdfMode}>
<Page className="invoice-wrapper" pdfMode={pdfMode}>
Expand Down

0 comments on commit f2616be

Please sign in to comment.