change api

This commit is contained in:
Thomas Ruoff
2021-03-06 23:52:55 +01:00
parent 2721e65f09
commit fa85d36924
5 changed files with 21 additions and 23 deletions

View File

@@ -58,22 +58,19 @@ export default function App() {
window.location.reload()
}
const _onGenerate = () => {
const _onGenerate = async () => {
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)
})
try {
const url = await generatePdf(options)
setPdfIsLoading(false)
setPdfUrl(url)
} catch (error) {
setPdfIsLoading(false)
setPdfError(error.message)
setPdfUrl(null)
}
}
return (

View File

@@ -7,7 +7,7 @@ function checkStatus(res: Response) {
}
}
export async function generatePdf(state: IDocProps) {
export async function generatePdf(state: IDocProps): Promise<string> {
const options = {
method: 'POST',
headers: {
@@ -16,19 +16,20 @@ export async function generatePdf(state: IDocProps) {
body: JSON.stringify(state),
}
const response = await fetch('/api/pdf/generate/brief', options)
const response = await fetch('/api/document?template=brief', options)
checkStatus(response)
return response.json()
const { id } = await response.json()
return `/api/document/${id}`
}
export async function getLatest(): Promise<ILatest[]> {
const response = await fetch('/api/pdf/latest')
const response = await fetch('/api/document/latest')
checkStatus(response)
return response.json()
}
export async function removeLatest(item: { id: string }) {
const response = await fetch(`/api/pdf/${item.id}`, { method: 'DELETE' })
const response = await fetch(`/api/document/${item.id}`, { method: 'DELETE' })
checkStatus(response)
return response
}