drop some types in

This commit is contained in:
Thomas Ruoff
2021-02-14 22:47:44 +01:00
parent 81db4b5d0e
commit 85a7b54a13
13 changed files with 116 additions and 45 deletions

View File

@@ -1,9 +1,9 @@
import React from 'react' import React from 'react'
export default function (props) { export default function Button({ children, onClick }: { children: React.ReactNode; onClick: () => void }) {
return ( return (
<button className="p-button" onClick={props.onClick}> <button className="p-button" onClick={onClick}>
{props.children} {children}
</button> </button>
) )
} }

View File

@@ -1,6 +1,6 @@
import React from 'react' import React from 'react'
export default function () { export default function Header() {
return ( return (
<header className="header"> <header className="header">
<h1>PDFer</h1> <h1>PDFer</h1>

View File

@@ -1,15 +1,27 @@
import React from 'react' import React from 'react'
export default function (props) { export default function Input({
text,
name,
value,
placeholder,
onchange,
}: {
text: string
name: string
value: string
placeholder: string
onchange: () => void
}) {
return ( return (
<label> <label>
{props.text} {text}
<input <input
className={props.name} className={name}
type="text" type="text"
placeholder={props.placeholder} placeholder={placeholder}
onChange={props.onchange.bind(null, props.name)} onChange={onchange.bind(null, name)}
value={props.value} value={value}
/> />
</label> </label>
) )

View File

@@ -1,18 +1,30 @@
import React from 'react' import React from 'react'
import { ILatest } from '../interfaces/ILatest'
import Button from './Button' import Button from './Button'
export default function (props) { export default function LatestList({
const latest = props.latest || [] latest,
onSelect,
onRemove,
}: {
latest: ILatest[]
onSelect: (l: ILatest) => void
onRemove: (l: ILatest) => void
}) {
if (!latest || !latest.length) {
return null
}
const latestElements = latest.map((item) => { const latestElements = latest.map((item) => {
const created = new Date(item.created) const created = new Date(item.created)
const subject = item.subject
const hrefId = `#item-${item.id}` const hrefId = `#item-${item.id}`
return ( return (
<li key={item.id}> <li key={item.id}>
<a href={hrefId} onClick={() => props.onSelect(item)}> <a href={hrefId} onClick={() => onSelect(item)}>
{created.toLocaleString()} <div>{subject}</div>
<div>{created.toLocaleDateString()}</div>
</a> </a>
<span> </span> <Button onClick={() => onRemove(item)}>Remove</Button>
<Button onClick={() => props.onRemove(item)}>Remove</Button>
</li> </li>
) )
}) })

View File

@@ -2,11 +2,11 @@ import React from 'react'
import Header from './Header' import Header from './Header'
export default function (props) { export default function Layout({ children }: { children: React.ReactNode }) {
return ( return (
<div id="app"> <div id="app">
<Header /> <Header />
<div id="content">{props.children}</div> <div id="content">{children}</div>
</div> </div>
) )
} }

View File

@@ -4,12 +4,17 @@ import Collapsible from 'react-collapsible'
import Input from './Input' import Input from './Input'
import TextAreaInput from './TextAreaInput' import TextAreaInput from './TextAreaInput'
import Select from './Select' import Select from './Select'
import { IDocProps } from '../interfaces/IDocProps'
//import './LetterOptions.css'; //import './LetterOptions.css';
const EXAMPLE_ADDRESS = 'Max Mustermann\nMusterstr. 73\n12345 Musterstadt' const EXAMPLE_ADDRESS = 'Max Mustermann\nMusterstr. 73\n12345 Musterstadt'
export default function (props) { interface IProps extends IDocProps {
onChange: () => void
}
export default function LetterOptions(props: IProps) {
const templateTypeOptions = [ const templateTypeOptions = [
{ {
value: 'brief-fam', value: 'brief-fam',

View File

@@ -1,7 +1,15 @@
import React from 'react' import React from 'react'
export default (props) => { export default function Preview({
if (props.pdfIsLoading) { pdfIsLoading,
pdfError,
pdfUrl,
}: {
pdfIsLoading: boolean
pdfError: string
pdfUrl: string
}) {
if (pdfIsLoading) {
return <div>Lade&hellip;</div> return <div>Lade&hellip;</div>
} }
@@ -12,18 +20,18 @@ export default (props) => {
borderRadius: '3px', borderRadius: '3px',
} }
if (props.pdfError) { if (pdfError) {
return ( return (
<div style={errorStyles}> <div style={errorStyles}>
<span role="img" aria-label="Crying Man"> <span role="img" aria-label="Crying Man">
😢 😢
</span>{' '} </span>{' '}
{props.pdfError} {pdfError}
</div> </div>
) )
} }
if (!props.pdfUrl) { if (!pdfUrl) {
return <div>Knopf drücken dann gibts hier was zu sehen!</div> return <div>Knopf drücken dann gibts hier was zu sehen!</div>
} }
@@ -32,5 +40,5 @@ export default (props) => {
height: '1050px', height: '1050px',
} }
return <embed src={props.pdfUrl} style={styles} type="application/pdf" /> return <embed src={pdfUrl} style={styles} type="application/pdf" />
} }

View File

@@ -1,12 +1,22 @@
import React from 'react' import React from 'react'
export default (props) => { export default function Select({
const { options = [] } = props name,
text,
value,
onchange,
options = [],
}: {
name: string
text: string
value: string
onchange: () => void
options: { name: string; value: string }[]
}) {
return ( return (
<label> <label>
{props.text} {text}
<select className={props.name} onChange={props.onchange.bind(null, props.name)} value={props.value}> <select className={name} onChange={onchange.bind(null, name)} value={value}>
{options.map((option) => ( {options.map((option) => (
<option value={option.value} key={option.value}> <option value={option.value} key={option.value}>
{option.name} {option.name}

View File

@@ -1,15 +1,22 @@
import React from 'react' import React from 'react'
export default function (props) { export default function TextAreaInput({
text,
name,
placeholder,
onchange,
value,
}: {
text: string
name: string
placeholder: string
onchange: () => void
value: string
}) {
return ( return (
<label> <label>
{props.text} {text}
<textarea <textarea className={name} placeholder={placeholder} onChange={onchange.bind(null, name)} value={value} />
className={props.name}
placeholder={props.placeholder}
onChange={props.onchange.bind(null, props.name)}
value={props.value}
/>
</label> </label>
) )
} }

View File

@@ -1,3 +1,5 @@
import { ILatest } from '../interfaces/ILatest'
function checkStatus(res: Response) { function checkStatus(res: Response) {
if (res.status < 200 || res.status >= 400) { if (res.status < 200 || res.status >= 400) {
throw new Error(`Something went wrong (Status ${res.status}) - I do feel very sorry!`) throw new Error(`Something went wrong (Status ${res.status}) - I do feel very sorry!`)
@@ -18,7 +20,7 @@ export async function generatePdf(state: Record<string, string>) {
return response.json() return response.json()
} }
export async function getLatest() { export async function getLatest(): Promise<ILatest[]> {
const response = await fetch('/api/pdf/latest') const response = await fetch('/api/pdf/latest')
checkStatus(response) checkStatus(response)
return response.json() return response.json()

View File

@@ -1,6 +0,0 @@
import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'
import './index.css'
ReactDOM.render(<App />, document.getElementById('root'))

15
interfaces/IDocProps.tsx Normal file
View File

@@ -0,0 +1,15 @@
export interface IDocProps {
template: string
subject: string
body: string
address: string
date?: string
opening?: string
closing?: string
yourRef?: string
yourMail?: string
myRef?: string
customer?: string
invoice?: string
specialMail?: string
}

6
interfaces/ILatest.tsx Normal file
View File

@@ -0,0 +1,6 @@
import { IDocProps } from './IDocProps'
export interface ILatest extends IDocProps {
id: string
created: Date
}