@@ -2,24 +2,28 @@ import React, { useState } from 'react';
2
2
import Modal from 'react-bootstrap/Modal'
3
3
import Button from 'react-bootstrap/Button'
4
4
import InputGroup from 'react-bootstrap/InputGroup'
5
+ import Form from 'react-bootstrap/Form'
5
6
import FormControl from 'react-bootstrap/FormControl'
7
+ import Col from 'react-bootstrap/Col'
6
8
import { gql , useMutation } from '@apollo/client' ;
9
+ import { client } from '../index' ;
7
10
8
11
9
-
10
- const UpdateModal = ( { show, handleClose, item} ) => {
12
+ const UpdateModal = ( { show, handleClose, item} ) => {
11
13
const [ state , setState ] = useState ( {
12
14
id : item [ 0 ] ,
13
15
name : item [ 1 ] ,
14
16
code : item [ 2 ] ,
15
17
district : item [ 3 ] ,
16
18
population : item [ 4 ]
17
19
} ) ;
20
+ const [ validated , setValidated ] = useState ( false ) ;
21
+
18
22
19
- const SET_MUATATION = gql `
23
+ const SET_MUTATION = gql `
20
24
mutation{
21
- addCity(cityId:${ state . id } ,cityName:${ state . name } ,cityCountrycode:${ state . code } ,cityDistrict:${ state . district } ,cityPopulation:${ state . population } ) {
22
- addCity {
25
+ addCity(cityId:${ state . id } ,cityName:" ${ state . name } " ,cityCountrycode:" ${ state . code } " ,cityDistrict:" ${ state . district } " ,cityPopulation:${ state . population } ) {
26
+ city {
23
27
cityId,
24
28
cityName,
25
29
cityCountrycode,
@@ -28,63 +32,89 @@ const UpdateModal=({show,handleClose,item}) =>{
28
32
}
29
33
}
30
34
}` ;
31
- const [ addCity ] = useMutation ( SET_MUATATION ) ;
35
+ const DELETE_MUTATION = gql `
36
+ mutation{
37
+ deleteCity(cityId:${ item [ 0 ] } ) {
38
+ city{
39
+ cityId
40
+ }
41
+ }
42
+ }` ;
43
+ const [ addCity ] = useMutation ( SET_MUTATION ) ;
44
+ const [ deleteCity , { loading, error } ] = useMutation ( DELETE_MUTATION ) ;
32
45
33
- const handleSave = ( id ) => {
34
- console . log ( "save" , id )
35
- addCity ( ) ;
36
- }
46
+ if ( loading ) return < p > Loading</ p > ; ;
47
+ if ( error ) return < p > An error occurred</ p > ;
48
+
49
+ const handleSubmit = ( event ) => {
50
+ const form = event . currentTarget ;
51
+
52
+ if ( form . checkValidity ( ) === false ) {
53
+ event . preventDefault ( ) ;
54
+ event . stopPropagation ( ) ;
55
+ } else {
56
+ setValidated ( true ) ;
57
+ addCity ( ) ;
58
+ handleClose ( ) ;
59
+ }
60
+ } ;
61
+
37
62
38
- const handleDelete = ( ) => {
39
- const DELETE_MUATATION = gql `
40
- mutation{
41
- addCity(cityId:"${ state . id } ",cityName:"${ state . name } ",cityCountrycode:"${ state . code } ",cityDistrict:"${ state . district } ",cityPopulation:${ state . population } ) {
42
- addCity{
43
- cityId,
44
- cityName,
45
- cityCountrycode,
46
- cityDistrict,
47
- cityPopulation
48
- }
49
- }
50
- }` ;
63
+ const handleDelete = ( event ) => {
64
+ deleteCity ( )
65
+ handleClose ( )
51
66
}
52
67
53
68
return (
54
69
< >
55
-
56
70
< Modal show = { show } onHide = { handleClose } >
57
71
< Modal . Header closeButton >
58
72
< Modal . Title > Admin Panel</ Modal . Title >
59
73
</ Modal . Header >
60
- < Modal . Body > Woohoo, you can now edit the records!</ Modal . Body >
61
- < InputGroup className = "mb-3" >
62
- < FormControl
63
- placeholder = { item [ 0 ] }
64
- aria-label = "Id"
65
- //value={item[0]}
66
- onChange = { e => {
67
- let inputValue = e . target . value ;
68
- ( setState ( {
69
- ...state ,
70
- id : inputValue
71
- }
72
- ) ) } }
73
- />
74
- < FormControl
75
- placeholder = { item [ 1 ] }
76
- aria-label = "name"
77
- //value={item[1]}
78
- onChange = { e =>
79
- { let inputValue = e . target . value ;
80
- setState ( {
81
- ...state ,
82
- name : inputValue
83
- } ) } }
84
- />
74
+ < Modal . Body > Woohoo, you can now edit the city records!</ Modal . Body >
75
+ < Form noValidate validated = { validated } onSubmit = { handleSubmit } >
76
+ < Form . Group as = { Col } md = "4" >
77
+ < Form . Label > Id</ Form . Label >
78
+ < FormControl
79
+ placeholder = { item [ 0 ] }
80
+ required
81
+ aria-label = "Id"
82
+ aria-describedby = "Id"
83
+ //defaultValue={item[0]}
84
+ onChange = { e => {
85
+ let inputValue = e . target . value ;
86
+ ( setState ( {
87
+ ...state ,
88
+ id : inputValue
89
+ }
90
+ ) ) } }
91
+ />
92
+ </ Form . Group >
93
+
94
+ < Form . Group as = { Col } md = "4" >
95
+ < Form . Label > Name</ Form . Label >
96
+ < FormControl
97
+ required
98
+ placeholder = { item [ 1 ] }
99
+ aria-label = "name"
100
+ aria-describedby = "name"
101
+ // defaultValue={item[1]}
102
+ onChange = { e =>
103
+ { let inputValue = e . target . value ;
104
+ setState ( {
105
+ ...state ,
106
+ name : inputValue
107
+ } ) } }
108
+ />
109
+ </ Form . Group >
110
+
111
+ < Form . Group as = { Col } md = "4" >
112
+ < Form . Label > Code</ Form . Label >
85
113
< FormControl
114
+ required
86
115
placeholder = { item [ 2 ] }
87
116
aria-label = "code"
117
+ aria-describedby = "code"
88
118
//value={item[2]}
89
119
onChange = { e =>
90
120
{ let inputValue = e . target . value ;
@@ -93,9 +123,15 @@ const UpdateModal=({show,handleClose,item}) =>{
93
123
code :inputValue
94
124
} ) } }
95
125
/>
126
+ </ Form . Group >
127
+
128
+ < Form . Group as = { Col } md = "4" >
129
+ < Form . Label > District</ Form . Label >
96
130
< FormControl
131
+ required
97
132
placeholder = { item [ 3 ] }
98
133
aria-label = "district"
134
+ aria-describedby = "district"
99
135
//value={item[3]}
100
136
onChange = { e => {
101
137
let inputValue = e . target . value ;
@@ -104,30 +140,30 @@ const UpdateModal=({show,handleClose,item}) =>{
104
140
district : inputValue
105
141
} ) } }
106
142
/>
143
+ </ Form . Group >
144
+
145
+ < Form . Group as = { Col } md = "4" >
146
+ < Form . Label > Population</ Form . Label >
107
147
< FormControl
148
+ required
108
149
placeholder = { item [ 4 ] }
109
150
aria-label = "population"
151
+ aria-describedby = "population"
110
152
//value={item[4]}
111
153
onChange = { e =>
112
154
{ let inputValue = e . target . value ;
113
155
setState ( {
114
156
...state ,
115
157
population : inputValue
116
- } ) } }
117
- />
118
- </ InputGroup >
158
+ } ) } }
159
+ />
160
+ </ Form . Group >
161
+
119
162
< Modal . Footer >
120
- < Button variant = "secondary" onClick = { handleDelete } >
121
- Delete
122
- </ Button >
123
- < Button variant = "primary" onClick = { ( ) => {
124
- handleSave ( item [ 0 ] )
125
- handleClose ( )
126
- }
127
- } >
128
- Save Changes
129
- </ Button >
163
+ < Button type = "submit" > Update</ Button >
164
+ < Button variant = "secondary" onClick = { handleDelete } > Delete </ Button >
130
165
</ Modal . Footer >
166
+ </ Form >
131
167
</ Modal >
132
168
</ >
133
169
) ;
0 commit comments