-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
generalize PdfForm to work with edit + add
- Loading branch information
Showing
7 changed files
with
107 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,76 @@ | ||
import React from 'react'; | ||
import { string } from 'prop-types'; | ||
|
||
import { useParams } from 'react-router-dom'; | ||
|
||
import { gql, useMutation, useQuery } from '@apollo/client'; | ||
|
||
import PdfForm from '../components/PdfForm'; | ||
|
||
const GET_PDF = gql` | ||
query getPdf($uuid: String) { | ||
pdf(uuid: $uuid) { | ||
organization { | ||
name | ||
uuid | ||
} | ||
user { | ||
uuid | ||
name | ||
} | ||
url | ||
uuid | ||
year | ||
done | ||
} | ||
} | ||
`; | ||
|
||
const UPDATE_PDF = gql` | ||
mutation updatePdf($input: PdfInput!) { | ||
upsertPdf(input: $input) { | ||
uuid | ||
} | ||
} | ||
`; | ||
|
||
const EditPdf = () => { | ||
const { pdfId } = useParams(); | ||
const { uuid } = useParams(); | ||
|
||
const { loading, error, data } = useQuery(GET_PDF, { | ||
variables: { uuid }, | ||
}); | ||
|
||
const [ | ||
updatePdf, | ||
{ loading: updatePdfLoading, error: updatePdfError, data: updatePdfData }, | ||
] = useMutation(UPDATE_PDF); | ||
|
||
return <>{pdfId}</>; | ||
if (loading) return <>Loading...</>; | ||
|
||
if (error) throw new Error('todo catch these?'); | ||
|
||
const updateError = updatePdfError && <p>{updatePdfError.message}</p>; | ||
const updated = updatePdfData && <p>Updated PDF</p>; | ||
|
||
const onSubmit = (input) => { | ||
updatePdf({ | ||
variables: { | ||
input: { ...input, uuid }, | ||
}, | ||
}); | ||
}; | ||
|
||
return ( | ||
<> | ||
<PdfForm defaultValues={data.pdf} onSubmit={onSubmit} /> | ||
{updated} | ||
{updatePdfLoading && 'Loading...'} | ||
{updateError} | ||
</> | ||
); | ||
}; | ||
|
||
EditPdf.propTypes = { uuid: string.isRequired }; | ||
|
||
export default EditPdf; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters