From ab9f6a2c3ade4d74d78221a02d2e64d9a1a88227 Mon Sep 17 00:00:00 2001 From: Anthony Hinsinger Date: Wed, 4 Mar 2020 17:31:14 +0100 Subject: [PATCH] Cleaned book form --- src/web/bookform.ts | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/src/web/bookform.ts b/src/web/bookform.ts index 72974a6..df924e0 100644 --- a/src/web/bookform.ts +++ b/src/web/bookform.ts @@ -1,4 +1,4 @@ -import { component, html, useState } from 'haunted'; +import { component, html } from 'haunted'; import { spread } from '@open-wc/lit-helpers'; import { useMutation } from '@apollo/react-hooks'; @@ -7,45 +7,42 @@ import { AddBook, GetBooks } from './queries' import Css from './bookform.scss'; const BookForm = () => { - const [titleProps, titleMeta] = useFormField("title"); - const [authorProps, authorMeta] = useFormField("author"); - const [addBook, { loading }] = useMutation(AddBook); + const [titleProps, { reset: resetAuthor, isDirty: dirtyAuthor}] = useFormField("title"); + const [authorProps, { reset: resetTitle, isDirty: dirtyTitle}] = useFormField("author"); + const [addBook, { loading }] = useMutation(AddBook, { + update: (cache, { data }) => { + const { books }: any = cache.readQuery({ query: GetBooks }); + cache.writeQuery({ + query: GetBooks, + data: { books: [...books, data.addBook] } + }); + } + }); return html`
{ e.preventDefault(); try { - const newBook = { + await addBook({ variables: { author: authorProps[".value"], title: titleProps[".value"] - }; + }}); - await addBook({ - variables: newBook, - update: (cache, { data }) => { - const { books }: any = cache.readQuery({ query: GetBooks }); - cache.writeQuery({ - query: GetBooks, - data: { books: [...books, data.addBook] } - }); - } - }); - - titleMeta.reset(); - authorMeta.reset(); + resetAuthor(); + resetTitle(); } catch(e) { console.log(e); } }}>