@@ -14,10 +14,17 @@ import distanceInWordsToNow from 'date-fns/distance_in_words_to_now'
14
14
import times from 'lodash/times'
15
15
import remove from 'lodash/remove'
16
16
import gql from 'graphql-tag'
17
- import { graphql } from 'react-apollo'
17
+ import { graphql , compose } from 'react-apollo'
18
18
import { propType } from 'graphql-anywhere'
19
+ import Dialog , {
20
+ DialogActions ,
21
+ DialogContent ,
22
+ DialogContentText ,
23
+ DialogTitle
24
+ } from 'material-ui/Dialog'
25
+ import Button from 'material-ui/Button'
19
26
20
- import { REVIEW_ENTRY } from '../graphql/Review'
27
+ import { REVIEW_ENTRY , REVIEWS_QUERY } from '../graphql/Review'
21
28
22
29
const StarRating = ( { rating } ) => (
23
30
< div >
@@ -28,7 +35,8 @@ const StarRating = ({ rating }) => (
28
35
29
36
class Review extends Component {
30
37
state = {
31
- anchorEl : null
38
+ anchorEl : null ,
39
+ deleteConfirmationOpen : false
32
40
}
33
41
34
42
openMenu = event => {
@@ -43,8 +51,18 @@ class Review extends Component {
43
51
this . closeMenu ( )
44
52
}
45
53
46
- delete = ( ) => {
54
+ openDeleteConfirmation = ( ) => {
47
55
this . closeMenu ( )
56
+ this . setState ( { deleteConfirmationOpen : true } )
57
+ }
58
+
59
+ closeDeleteConfirmation = ( ) => {
60
+ this . setState ( { deleteConfirmationOpen : false } )
61
+ }
62
+
63
+ delete = ( ) => {
64
+ this . closeDeleteConfirmation ( )
65
+ this . props . delete ( this . props . review . id )
48
66
}
49
67
50
68
toggleFavorite = ( ) => {
@@ -95,14 +113,37 @@ class Review extends Component {
95
113
</ IconButton >
96
114
</ CardActions >
97
115
</ Card >
116
+
98
117
< Menu
99
118
anchorEl = { this . state . anchorEl }
100
119
open = { Boolean ( this . state . anchorEl ) }
101
120
onClose = { this . closeMenu }
102
121
>
103
122
< MenuItem onClick = { this . edit } > Edit</ MenuItem >
104
- < MenuItem onClick = { this . delete } > Delete</ MenuItem >
123
+ < MenuItem onClick = { this . openDeleteConfirmation } > Delete</ MenuItem >
105
124
</ Menu >
125
+
126
+ < Dialog
127
+ open = { this . state . deleteConfirmationOpen }
128
+ onClose = { this . closeDeleteConfirmation }
129
+ >
130
+ < DialogTitle > { 'Delete review?' } </ DialogTitle >
131
+ < DialogContent >
132
+ < DialogContentText >
133
+ A better UX is probably just letting you single-click delete with
134
+ an undo toast, but that's harder to code right{ ' ' }
135
+ < span role = "img" aria-label = "grinning face" >
136
+ 😄
137
+ </ span >
138
+ </ DialogContentText >
139
+ </ DialogContent >
140
+ < DialogActions >
141
+ < Button onClick = { this . closeDeleteConfirmation } > Cancel</ Button >
142
+ < Button onClick = { this . delete } color = "primary" autoFocus >
143
+ Sudo delete
144
+ </ Button >
145
+ </ DialogActions >
146
+ </ Dialog >
106
147
</ div >
107
148
)
108
149
}
@@ -159,4 +200,31 @@ const withFavoriteMutation = graphql(FAVORITE_REVIEW_MUTATION, {
159
200
} )
160
201
} )
161
202
162
- export default withFavoriteMutation ( Review )
203
+ const DELETE_REVIEW_MUTATION = gql `
204
+ mutation DeleteReview($id: ObjID!) {
205
+ removeReview(id: $id)
206
+ }
207
+ `
208
+
209
+ const withDeleteMutation = graphql ( DELETE_REVIEW_MUTATION , {
210
+ props : ( { mutate } ) => ( {
211
+ delete : id =>
212
+ mutate ( {
213
+ variables : { id } ,
214
+ optimisticResponse : {
215
+ removeReview : true
216
+ } ,
217
+ update : store => {
218
+ let data = store . readQuery ( { query : REVIEWS_QUERY } )
219
+ remove ( data . reviews , { id } )
220
+ store . writeQuery ( { query : REVIEWS_QUERY , data } )
221
+
222
+ data = store . readQuery ( { query : READ_USER_FAVORITES } )
223
+ remove ( data . currentUser . favoriteReviews , { id } )
224
+ store . writeQuery ( { query : READ_USER_FAVORITES , data } )
225
+ }
226
+ } )
227
+ } )
228
+ } )
229
+
230
+ export default compose ( withFavoriteMutation , withDeleteMutation ) ( Review )
0 commit comments