Skip to content

Commit

Permalink
Update both orderBy queries on add or delete
Browse files Browse the repository at this point in the history
  • Loading branch information
lorensr committed Aug 7, 2018
1 parent 2102ed5 commit 504646f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
17 changes: 12 additions & 5 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, this.props.orderBy).catch(e => {
this.props.delete(this.props.review.id).catch(e => {
if (find(e.graphQLErrors, { message: 'unauthorized' })) {
alert('👮‍♀️✋ You can only delete your own reviews!')
}
Expand Down Expand Up @@ -181,8 +181,7 @@ class Review extends Component {
Review.propTypes = {
review: propType(REVIEW_ENTRY).isRequired,
favorite: PropTypes.func.isRequired,
user: PropTypes.object,
orderBy: PropTypes.string.isRequired
user: PropTypes.object
}

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

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

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 Down
7 changes: 6 additions & 1 deletion src/components/ReviewForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,14 @@ const withAddReview = graphql(ADD_REVIEW_MUTATION, {
variables: { limit: 10, orderBy: 'createdAt_DESC' }
}

const data = store.readQuery(query)
let data = store.readQuery(query)
data.reviews.unshift(newReview)
store.writeQuery({ ...query, data })

query.variables.orderBy = 'createdAt_ASC'
data = store.readQuery(query)
data.reviews.push(newReview)
store.writeQuery({ ...query, data })
}
})
}
Expand Down
9 changes: 2 additions & 7 deletions src/components/ReviewList.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,14 @@ class ReviewList extends Component {
}

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

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

0 comments on commit 504646f

Please sign in to comment.