From 3386fb39283cfd077e038c5733a2c2441818a21a Mon Sep 17 00:00:00 2001 From: Thomas Ruoff Date: Mon, 15 Feb 2021 23:29:25 +0100 Subject: [PATCH] fix all type errors --- components/App.tsx | 143 ++++++++++----------- components/Input.tsx | 8 +- components/LetterOptions.tsx | 2 +- components/Preview.tsx | 11 +- components/Select.tsx | 2 +- components/TextAreaInput.tsx | 2 +- components/apiHelper.tsx | 3 +- interfaces/{IDocProps.tsx => IDocProps.ts} | 0 interfaces/{ILatest.tsx => ILatest.ts} | 0 interfaces/IPdfProps.ts | 5 + lib/store.ts | 1 + lib/templates.ts | 6 +- pages/api/pdf/[id].ts | 70 +++++----- pages/index.tsx | 2 +- 14 files changed, 123 insertions(+), 132 deletions(-) rename interfaces/{IDocProps.tsx => IDocProps.ts} (100%) rename interfaces/{ILatest.tsx => ILatest.ts} (100%) create mode 100644 interfaces/IPdfProps.ts diff --git a/components/App.tsx b/components/App.tsx index 83393ce..b1fb3b6 100644 --- a/components/App.tsx +++ b/components/App.tsx @@ -1,96 +1,83 @@ -import React, { Component } from 'react' +import React, { useState, useEffect } from 'react' import LetterOptions from './LetterOptions' import Button from './Button' import Preview from './Preview' import LatestList from './LatestList' import { generatePdf, getLatest, removeLatest } from './apiHelper' +import { ILatest } from '../interfaces/ILatest' +import { IDocProps } from '../interfaces/IDocProps' //import './App.css'; -class App extends Component { - componentDidMount() { - this._getLatest() - } +export default function App() { + const [options, setOptions] = useState({ + template: 'brief-fam', + subject: '', + body: '', + address: '', + }) + const [latest, setLatest] = useState([]) + const [pdfUrl, setPdfUrl] = useState(null) + const [pdfIsLoading, setPdfIsLoading] = useState(false) + const [pdfError, setPdfError] = useState(null) - render() { - const state: Record = this.state || {} - - return ( -
-
- - -
-
-
- - -
- -
-
- ) - } - - _onSelectLatest(selectedOptions) { - this.setState({ options: Object.assign({}, selectedOptions) }) - } - - _onRemoveLatest(item) { - removeLatest(item).then(() => { - const latest = this.state.latest.filter((curr) => curr.id !== item.id) - - this.setState({ latest }) - }) - } - - _onClear() { - window.location.reload() - } - - _onGenerate() { - const state = this.state || {} - this.setState({ - pdfIsLoading: true, - pdfError: null, - }) - - generatePdf(state.options) - .then((data) => { - const { id } = data - this.setState({ - pdfIsLoading: false, - pdfUrl: `/api/pdf/${id}`, - }) - }) - .catch((error) => { - this.setState({ - pdfIsLoading: false, - pdfError: error.message, - pdfUrl: null, - }) - }) - .then(() => this._getLatest()) - } - - _getLatest() { + useEffect(() => { getLatest() - .then((latest) => this.setState({ latest })) + .then((latest) => setLatest(latest)) .catch((err) => console.error('Unable to get latest:', err)) - } + }, [pdfUrl]) - _onChange(name, event) { + const _onChange = (name: string, event: React.ChangeEvent<{ value: string }>) => { if (!name) { return } const value = event && event.target && event.target.value - const options = Object.assign({}, this.state.options, { [name]: value || undefined }) - this.setState({ options }) + setOptions({ ...options, [name]: value || undefined }) } -} -export default App + const _onSelectLatest = (selectedOption: ILatest) => { + setOptions({ ...selectedOption }) + } + + const _onRemoveLatest = (item: ILatest) => { + removeLatest(item).then(() => setLatest(latest.filter((curr) => curr.id !== item.id))) + } + + const _onClear = () => { + window.location.reload() + } + + const _onGenerate = () => { + setPdfIsLoading(true) + setPdfError(null) + + generatePdf(options) + .then((data) => { + const { id } = data + + setPdfIsLoading(false) + setPdfUrl(`/api/pdf/${id}`) + }) + .catch((error) => { + setPdfIsLoading(false) + setPdfError(error.message) + setPdfUrl(null) + }) + } + + return ( +
+
+ + +
+
+
+ + +
+ +
+
+ ) +} diff --git a/components/Input.tsx b/components/Input.tsx index 8aeac7a..2244ad8 100644 --- a/components/Input.tsx +++ b/components/Input.tsx @@ -3,15 +3,15 @@ import React from 'react' export default function Input({ text, name, - value, + value = '', placeholder, onchange, }: { text: string name: string - value: string - placeholder: string - onchange: () => void + value?: string + placeholder?: string + onchange: (name: string, event: React.ChangeEvent) => void }) { return (