1+ import React , { Component } from 'react' ;
2+ import { Link , withRouter } from 'react-router-dom' ;
3+ import { Button , Container , Form , FormGroup , Input , Label } from 'reactstrap' ;
4+ import AppNavbar from './AppNavbar' ;
5+ import { instanceOf } from 'prop-types' ;
6+ import { Cookies , withCookies } from 'react-cookie' ;
7+
8+ class GroupEdit extends Component {
9+ static propTypes = {
10+ cookies : instanceOf ( Cookies ) . isRequired
11+ } ;
12+
13+ emptyItem = {
14+ name : '' ,
15+ address : '' ,
16+ city : '' ,
17+ stateOrProvince : '' ,
18+ country : '' ,
19+ postalCode : ''
20+ } ;
21+
22+
23+ constructor ( props ) {
24+ super ( props ) ;
25+ const { cookies} = props ;
26+ this . state = {
27+ item : this . emptyItem ,
28+ csrfToken : cookies . get ( 'XSRF-TOKEN' )
29+ } ;
30+ this . handleChange = this . handleChange . bind ( this ) ;
31+ this . handleSubmit = this . handleSubmit . bind ( this ) ;
32+ }
33+
34+ async componentDidMount ( ) {
35+ if ( this . props . match . params . id !== 'new' ) {
36+ try {
37+ const group = await ( await fetch ( `/api/group/${ this . props . match . params . id } ` , { credentials : 'include' } ) ) . json ( ) ;
38+ this . setState ( { item : group } ) ;
39+ } catch ( error ) {
40+ this . props . history . push ( '/' ) ;
41+ }
42+ }
43+ }
44+
45+ handleChange ( event ) {
46+ const target = event . target ;
47+ const value = target . value ;
48+ const name = target . name ;
49+ let item = { ...this . state . item } ;
50+ item [ name ] = value ;
51+ this . setState ( { item} ) ;
52+ }
53+
54+ async handleSubmit ( event ) {
55+ event . preventDefault ( ) ;
56+ const { item, csrfToken} = this . state ;
57+
58+ await fetch ( '/api/group' , {
59+ method : ( item . id ) ? 'PUT' : 'POST' ,
60+ headers : {
61+ 'X-XSRF-TOKEN' : csrfToken ,
62+ 'Accept' : 'application/json' ,
63+ 'Content-Type' : 'application/json'
64+ } ,
65+ body : JSON . stringify ( item ) ,
66+ credentials : 'include'
67+ } ) ;
68+ this . props . history . push ( '/groups' ) ;
69+ }
70+
71+ render ( ) {
72+ const { item} = this . state ;
73+ const title = < h2 > { item . id ? 'Edit Group' : 'Add Group' } </ h2 > ;
74+
75+ return < div >
76+ < AppNavbar />
77+ < Container >
78+ { title }
79+ < Form onSubmit = { this . handleSubmit } >
80+ < FormGroup >
81+ < Label for = "name" > Name</ Label >
82+ < Input type = "text" name = "name" id = "name" value = { item . name || '' }
83+ onChange = { this . handleChange } autoComplete = "name" />
84+ </ FormGroup >
85+ < FormGroup >
86+ < Label for = "address" > Address</ Label >
87+ < Input type = "text" name = "address" id = "address" value = { item . address || '' }
88+ onChange = { this . handleChange } autoComplete = "address-level1" />
89+ </ FormGroup >
90+ < FormGroup >
91+ < Label for = "city" > City</ Label >
92+ < Input type = "text" name = "city" id = "city" value = { item . city || '' }
93+ onChange = { this . handleChange } autoComplete = "address-level1" />
94+ </ FormGroup >
95+ < div className = "row" >
96+ < FormGroup className = "col-md-4 mb-3" >
97+ < Label for = "stateOrProvince" > State/Province</ Label >
98+ < Input type = "text" name = "stateOrProvince" id = "stateOrProvince" value = { item . stateOrProvince || '' }
99+ onChange = { this . handleChange } autoComplete = "address-level1" />
100+ </ FormGroup >
101+ < FormGroup className = "col-md-5 mb-3" >
102+ < Label for = "country" > Country</ Label >
103+ < Input type = "text" name = "country" id = "country" value = { item . country || '' }
104+ onChange = { this . handleChange } autoComplete = "address-level1" />
105+ </ FormGroup >
106+ < FormGroup className = "col-md-3 mb-3" >
107+ < Label for = "country" > Postal Code</ Label >
108+ < Input type = "text" name = "postalCode" id = "postalCode" value = { item . postalCode || '' }
109+ onChange = { this . handleChange } autoComplete = "address-level1" />
110+ </ FormGroup >
111+ </ div >
112+ < FormGroup >
113+ < Button color = "primary" type = "submit" > Save</ Button > { ' ' }
114+ < Button color = "secondary" tag = { Link } to = "/groups" > Cancel</ Button >
115+ </ FormGroup >
116+ </ Form >
117+ </ Container >
118+ </ div >
119+ }
120+ }
121+
122+ export default withCookies ( withRouter ( GroupEdit ) ) ;
0 commit comments