Skip to content

Commit

Permalink
Pass orderBy down from ReviewList to delete()
Browse files Browse the repository at this point in the history
  • Loading branch information
lorensr committed Aug 5, 2018
1 parent 3191e74 commit 2102ed5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
22 changes: 9 additions & 13 deletions src/components/Review.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class Review extends Component {

delete = () => {
this.closeDeleteConfirmation()
this.props.delete(this.props.review.id).catch(e => {
this.props.delete(this.props.review.id, this.props.orderBy).catch(e => {
if (find(e.graphQLErrors, { message: 'unauthorized' })) {
alert('👮‍♀️✋ You can only delete your own reviews!')
}
Expand Down Expand Up @@ -181,7 +181,8 @@ class Review extends Component {
Review.propTypes = {
review: propType(REVIEW_ENTRY).isRequired,
favorite: PropTypes.func.isRequired,
user: PropTypes.object
user: PropTypes.object,
orderBy: PropTypes.string.isRequired
}

const FAVORITE_REVIEW_MUTATION = gql`
Expand Down Expand Up @@ -239,7 +240,7 @@ const DELETE_REVIEW_MUTATION = gql`

const withDeleteMutation = graphql(DELETE_REVIEW_MUTATION, {
props: ({ mutate }) => ({
delete: id =>
delete: (id, orderBy) =>
mutate({
variables: { id },
optimisticResponse: {
Expand All @@ -248,21 +249,13 @@ const withDeleteMutation = graphql(DELETE_REVIEW_MUTATION, {
update: store => {
const query = {
query: REVIEWS_QUERY,
variables: { limit: 10, orderBy: 'createdAt_DESC' }
variables: { limit: 10, orderBy }
}

let data = store.readQuery(query)
remove(data.reviews, { id })
store.writeQuery({ ...query, data })

query.variables.orderBy = 'createdAt_ASC'

try {
data = store.readQuery(query)
remove(data.reviews, { id })
store.writeQuery({ ...query, data })
} catch (e) {}

data = store.readQuery({ query: READ_USER_FAVORITES })
remove(data.currentUser.favoriteReviews, { id })
store.writeQuery({ query: READ_USER_FAVORITES, data })
Expand All @@ -271,4 +264,7 @@ const withDeleteMutation = graphql(DELETE_REVIEW_MUTATION, {
})
})

export default compose(withFavoriteMutation, withDeleteMutation)(Review)
export default compose(
withFavoriteMutation,
withDeleteMutation
)(Review)
9 changes: 7 additions & 2 deletions src/components/ReviewList.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,19 @@ class ReviewList extends Component {
}

render() {
const { reviews, user } = this.props
const { reviews, user, orderBy } = this.props

return (
<div className="Reviews-list">
<div className="Reviews-content">
{reviews &&
reviews.map(review => (
<Review key={review.id} review={review} user={user} />
<Review
key={review.id}
review={review}
user={user}
orderBy={orderBy}
/>
))}
</div>
<div className="Spinner" />
Expand Down

0 comments on commit 2102ed5

Please sign in to comment.