1- import React , { useState , SyntheticEvent } from 'react'
1+ import React , { useState } from 'react'
22import { RouteComponentProps } from 'react-router-dom'
33import {
44 IonContent ,
55 IonCard ,
6- IonButton ,
7- IonItem ,
86 IonLabel ,
9- IonInput ,
107 IonCardHeader ,
118 IonCardContent ,
129 IonNote ,
@@ -22,13 +19,11 @@ import { mutationOptions } from '../helpers';
2219import { IUpdateMatchParams } from '../declarations' ;
2320import { updateTask } from '../graphql/mutations/updateTask' ;
2421import { findTasks } from '../graphql/queries/findTasks' ;
22+ import { TaskForm } from '../forms/TaskForm' ;
2523
2624export const UpdateTaskPage : React . FC < RouteComponentProps < IUpdateMatchParams > > = ( { history, match } ) => {
2725
2826 const { id } = match . params ;
29-
30- const [ title , setTitle ] = useState < string > ( null ! ) ;
31- const [ description , setDescription ] = useState < string > ( null ! ) ;
3227 const [ showToast , setShowToast ] = useState < boolean > ( false ) ;
3328 const [ errorMessage , setErrorMessage ] = useState < string > ( '' ) ;
3429 const { loading, error, data } = useQuery ( findTasks , {
@@ -46,27 +41,20 @@ export const UpdateTaskPage: React.FC<RouteComponentProps<IUpdateMatchParams>> =
4641 history . push ( '/' ) ;
4742 return ;
4843 }
44+ console . log ( error ) ;
4945 setErrorMessage ( error . message ) ;
5046 setShowToast ( true ) ;
5147 }
5248
53- const submit = ( event : SyntheticEvent ) => {
54- event . preventDefault ( ) ;
55- const task = data . findTasks ;
56- delete task . __typename ;
57- const variables = {
58- input : {
59- ...task ,
60- title : title || task . title ,
61- description : description || task . description ,
62- }
63- } ;
64-
49+ const submit = ( model : any ) => {
50+ // remove `__typename` property without
51+ // deleting from the model object (as this may be a state reference)
52+ const { __typename, ...no__typename } = model ;
6553 updateTaskMutation ( {
66- variables
54+ variables : { input : no__typename }
6755 } )
68- . then ( ( ) => history . push ( '/' ) )
69- . catch ( handleError ) ;
56+ . then ( ( ) => history . push ( '/' ) )
57+ . catch ( handleError ) ;
7058 }
7159
7260 if ( error ) return < pre > { JSON . stringify ( error ) } </ pre > ;
@@ -99,17 +87,7 @@ export const UpdateTaskPage: React.FC<RouteComponentProps<IUpdateMatchParams>> =
9987 </ IonLabel >
10088 </ IonCardContent >
10189 </ IonCard >
102- < form onSubmit = { submit } style = { { padding : '0 16px' } } >
103- < IonItem >
104- < IonLabel color = "primary" position = "floating" > Title</ IonLabel >
105- < IonInput type = "text" name = "title" onInput = { ( e : any ) => setTitle ( e . target . value ) } value = { task . title } />
106- </ IonItem >
107- < IonItem >
108- < IonLabel color = "primary" position = "floating" > Description</ IonLabel >
109- < IonInput type = "text" name = "description" onInput = { ( e : any ) => setDescription ( e . target . value ) } value = { task . description } />
110- </ IonItem >
111- < IonButton className = "submit-btn" expand = "block" type = "submit" > Update</ IonButton >
112- </ form >
90+ < TaskForm handleSubmit = { submit } model = { task } />
11391 < IonToast
11492 isOpen = { showToast }
11593 onDidDismiss = { ( ) => setShowToast ( false ) }
0 commit comments