mirror of
https://github.com/tomru/pdfer.git
synced 2026-03-03 06:27:19 +01:00
92 lines
2.8 KiB
TypeScript
92 lines
2.8 KiB
TypeScript
import React, { useState, useEffect } from 'react'
|
|
import LetterOptions from './LetterOptions'
|
|
import Button from './Button'
|
|
import Preview from './Preview'
|
|
import LatestList from './LatestList'
|
|
import { generatePdf } from './apiHelper'
|
|
import { addDocument, removeDocument, getDocuments } from '../lib/dataStore'
|
|
import { getDocuments as getLatestDocuments } from '../lib/dataStore'
|
|
import { IStoreItem } from '../interfaces/IStoreItem'
|
|
import { IDocProps } from '../interfaces/IDocProps'
|
|
|
|
export default function App() {
|
|
const [options, setOptions] = useState<IDocProps>({
|
|
address: 'Max Mustermann\nMusterstrasse\n12345 Musterstadt',
|
|
body: 'Inhalt des Briefs',
|
|
closing: 'Mit freundlichen Grüßen',
|
|
customer: '',
|
|
date: new Date().toLocaleDateString('de-DE'),
|
|
enclosing: '',
|
|
invoice: '',
|
|
myRef: '',
|
|
opening: 'Sehr geehrte Damen und Herren',
|
|
ps: '',
|
|
signature: '',
|
|
specialMail: '',
|
|
subject: 'Betreffzeile',
|
|
letterOption: 'brief-fam',
|
|
yourMail: '',
|
|
yourRef: '',
|
|
yourRefName: '',
|
|
})
|
|
const [storeData, setStoreData] = useState<IStoreItem[]>([])
|
|
const [pdfUrl, setPdfUrl] = useState<string | null>(null)
|
|
const [pdfIsLoading, setPdfIsLoading] = useState<boolean>(false)
|
|
const [pdfError, setPdfError] = useState<string | null>(null)
|
|
|
|
useEffect(() => {
|
|
setStoreData(getDocuments())
|
|
}, [pdfUrl])
|
|
|
|
const _onChange = (name: string, event: React.ChangeEvent<{ value: string }>) => {
|
|
if (!name) {
|
|
return
|
|
}
|
|
const value = event && event.target && event.target.value
|
|
setOptions({ ...options, [name]: value })
|
|
}
|
|
|
|
const _onSelectLatest = (selectedOption: IStoreItem) => {
|
|
setOptions({ ...selectedOption })
|
|
}
|
|
|
|
const _onRemoveLatest = async (item: IStoreItem) => {
|
|
removeDocument(item.id)
|
|
setStoreData(getLatestDocuments())
|
|
}
|
|
|
|
const _onClear = () => {
|
|
window.location.reload()
|
|
}
|
|
|
|
const _onGenerate = async () => {
|
|
setPdfIsLoading(true)
|
|
setPdfError(null)
|
|
|
|
try {
|
|
const { url, data } = await generatePdf(options)
|
|
addDocument(data)
|
|
setPdfIsLoading(false)
|
|
setPdfUrl(url)
|
|
} catch (error: any) {
|
|
setPdfIsLoading(false)
|
|
setPdfError(error?.message || 'Unknown error')
|
|
setPdfUrl(null)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="flex space-x-5 py-3 sm:px-2 lg:px-3 bg-gray-100">
|
|
<div className=" flex-grow flex flex-col shadow sm:rounded-md sm:overflow-hidden">
|
|
<LetterOptions onChange={_onChange} {...options} />
|
|
<div className="flex space-x-3 p-3">
|
|
<Button onClick={_onGenerate}>PDF Erstellen</Button>
|
|
<Button onClick={_onClear}>Alles Löschen</Button>
|
|
</div>
|
|
<Preview pdfUrl={pdfUrl} pdfIsLoading={pdfIsLoading} pdfError={pdfError} />
|
|
</div>
|
|
<LatestList storeData={storeData} onSelect={_onSelectLatest} onRemove={_onRemoveLatest} />
|
|
</div>
|
|
)
|
|
}
|