Skip to content

Commit 8c65120

Browse files
committed
Add DELETE_REVIEW_MUTATION
1 parent 738f60a commit 8c65120

File tree

1 file changed

+74
-6
lines changed

1 file changed

+74
-6
lines changed

src/components/Review.js

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,17 @@ import distanceInWordsToNow from 'date-fns/distance_in_words_to_now'
1414
import times from 'lodash/times'
1515
import remove from 'lodash/remove'
1616
import gql from 'graphql-tag'
17-
import { graphql } from 'react-apollo'
17+
import { graphql, compose } from 'react-apollo'
1818
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'
1926

20-
import { REVIEW_ENTRY } from '../graphql/Review'
27+
import { REVIEW_ENTRY, REVIEWS_QUERY } from '../graphql/Review'
2128

2229
const StarRating = ({ rating }) => (
2330
<div>
@@ -28,7 +35,8 @@ const StarRating = ({ rating }) => (
2835

2936
class Review extends Component {
3037
state = {
31-
anchorEl: null
38+
anchorEl: null,
39+
deleteConfirmationOpen: false
3240
}
3341

3442
openMenu = event => {
@@ -43,8 +51,18 @@ class Review extends Component {
4351
this.closeMenu()
4452
}
4553

46-
delete = () => {
54+
openDeleteConfirmation = () => {
4755
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)
4866
}
4967

5068
toggleFavorite = () => {
@@ -95,14 +113,37 @@ class Review extends Component {
95113
</IconButton>
96114
</CardActions>
97115
</Card>
116+
98117
<Menu
99118
anchorEl={this.state.anchorEl}
100119
open={Boolean(this.state.anchorEl)}
101120
onClose={this.closeMenu}
102121
>
103122
<MenuItem onClick={this.edit}>Edit</MenuItem>
104-
<MenuItem onClick={this.delete}>Delete</MenuItem>
123+
<MenuItem onClick={this.openDeleteConfirmation}>Delete</MenuItem>
105124
</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>
106147
</div>
107148
)
108149
}
@@ -159,4 +200,31 @@ const withFavoriteMutation = graphql(FAVORITE_REVIEW_MUTATION, {
159200
})
160201
})
161202

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

Comments
 (0)