@@ -6,18 +6,27 @@ import TextField from 'material-ui/TextField'
6
6
import StarIcon from 'material-ui-icons/Star'
7
7
import StarBorderIcon from 'material-ui-icons/StarBorder'
8
8
import gql from 'graphql-tag'
9
- import { graphql } from 'react-apollo'
9
+ import { graphql , compose } from 'react-apollo'
10
+ import classNames from 'classnames'
10
11
11
12
import { validateReview } from '../lib/validators'
12
13
import { REVIEWS_QUERY , REVIEW_ENTRY } from '../graphql/Review'
13
14
14
15
const GREY = '#0000008a'
15
16
16
- class AddReview extends Component {
17
- state = {
18
- text : '' ,
19
- stars : null ,
20
- errorText : null
17
+ class ReviewForm extends Component {
18
+ constructor ( props ) {
19
+ super ( props )
20
+
21
+ const { review } = props
22
+
23
+ this . isEditing = ! ! review
24
+
25
+ this . state = {
26
+ text : review ? review . text : '' ,
27
+ stars : review ? review . stars : null ,
28
+ errorText : null
29
+ }
21
30
}
22
31
23
32
updateText = event => {
@@ -38,15 +47,21 @@ class AddReview extends Component {
38
47
return
39
48
}
40
49
41
- this . props . addReview ( text , stars )
50
+ const { review } = this . props
51
+
52
+ if ( this . isEditing ) {
53
+ this . props . editReview ( review . id , text , stars )
54
+ } else {
55
+ this . props . addReview ( text , stars )
56
+ }
42
57
43
58
this . props . done ( )
44
59
}
45
60
46
61
render ( ) {
47
62
return (
48
63
< form
49
- className = "AddReview"
64
+ className = { classNames ( 'ReviewForm' , { editing : this . isEditing } ) }
50
65
autoComplete = "off"
51
66
onSubmit = { this . handleSubmit }
52
67
>
@@ -83,22 +98,28 @@ class AddReview extends Component {
83
98
</ Button >
84
99
85
100
< Button type = "submit" color = "primary" className = "AddReview-submit" >
86
- Add review
101
+ { this . isEditing ? 'Save' : ' Add review' }
87
102
</ Button >
88
103
</ div >
89
104
</ form >
90
105
)
91
106
}
92
107
}
93
108
94
- AddReview . propTypes = {
109
+ ReviewForm . propTypes = {
95
110
done : PropTypes . func . isRequired ,
96
111
user : PropTypes . shape ( {
97
112
name : PropTypes . string . isRequired ,
98
113
photo : PropTypes . string . isRequired ,
99
114
username : PropTypes . string . isRequired
100
- } ) . isRequired ,
101
- addReview : PropTypes . func . isRequired
115
+ } ) ,
116
+ addReview : PropTypes . func . isRequired ,
117
+ editReview : PropTypes . func . isRequired ,
118
+ review : PropTypes . shape ( {
119
+ id : PropTypes . string . isRequired ,
120
+ text : PropTypes . string ,
121
+ stars : PropTypes . number
122
+ } )
102
123
}
103
124
104
125
const ADD_REVIEW_MUTATION = gql `
@@ -110,7 +131,7 @@ const ADD_REVIEW_MUTATION = gql`
110
131
${ REVIEW_ENTRY }
111
132
`
112
133
113
- const withMutation = graphql ( ADD_REVIEW_MUTATION , {
134
+ const withAddReview = graphql ( ADD_REVIEW_MUTATION , {
114
135
props : ( { ownProps : { user } , mutate } ) => ( {
115
136
addReview : ( text , stars ) => {
116
137
mutate ( {
@@ -145,4 +166,36 @@ const withMutation = graphql(ADD_REVIEW_MUTATION, {
145
166
} )
146
167
} )
147
168
148
- export default withMutation ( AddReview )
169
+ const EDIT_REVIEW_MUTATION = gql `
170
+ mutation EditReview($id: ObjID!, $input: UpdateReviewInput!) {
171
+ updateReview(id: $id, input: $input) {
172
+ id
173
+ text
174
+ stars
175
+ }
176
+ }
177
+ `
178
+
179
+ const withEditReview = graphql ( EDIT_REVIEW_MUTATION , {
180
+ props : ( { mutate } ) => ( {
181
+ editReview : ( id , text , stars ) => {
182
+ mutate ( {
183
+ variables : {
184
+ id,
185
+ input : { text, stars }
186
+ } ,
187
+ optimisticResponse : {
188
+ updateReview : {
189
+ __typename : 'Review' ,
190
+ id,
191
+ text,
192
+ stars,
193
+ favorite : true
194
+ }
195
+ }
196
+ } )
197
+ }
198
+ } )
199
+ } )
200
+
201
+ export default compose ( withAddReview , withEditReview ) ( ReviewForm )
0 commit comments