Skip to content

Commit 4451efb

Browse files
committed
Add FAVORITE_REVIEW_MUTATION
1 parent 6291c02 commit 4451efb

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

src/components/Review.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import StarIcon from 'material-ui-icons/Star'
1212
import StarBorderIcon from 'material-ui-icons/StarBorder'
1313
import distanceInWordsToNow from 'date-fns/distance_in_words_to_now'
1414
import times from 'lodash/times'
15+
import gql from 'graphql-tag'
16+
import { graphql } from 'react-apollo'
1517

1618
const StarRating = ({ rating }) => (
1719
<div>
@@ -41,7 +43,10 @@ class Review extends Component {
4143
this.closeMenu()
4244
}
4345

44-
toggleFavorite = () => {}
46+
toggleFavorite = () => {
47+
const { review: { id, favorited } } = this.props
48+
this.props.favorite(id, !favorited)
49+
}
4550

4651
render() {
4752
const {
@@ -111,7 +116,33 @@ Review.propTypes = {
111116
photo: PropTypes.string.isRequired,
112117
username: PropTypes.string.isRequired
113118
})
114-
}).isRequired
119+
}).isRequired,
120+
favorite: PropTypes.func.isRequired
115121
}
116122

117-
export default Review
123+
const FAVORITE_REVIEW_MUTATION = gql`
124+
mutation FavoriteReview($id: ObjID!, $favorite: Boolean!) {
125+
favoriteReview(id: $id, favorite: $favorite) {
126+
id
127+
favorited
128+
}
129+
}
130+
`
131+
132+
const withFavoriteMutation = graphql(FAVORITE_REVIEW_MUTATION, {
133+
props: ({ mutate }) => ({
134+
favorite: (id, favorite) =>
135+
mutate({
136+
variables: { id, favorite },
137+
optimisticResponse: {
138+
favoriteReview: {
139+
__typename: 'Review',
140+
id,
141+
favorited: favorite
142+
}
143+
}
144+
})
145+
})
146+
})
147+
148+
export default withFavoriteMutation(Review)

0 commit comments

Comments
 (0)