fix(ui): Suggestions API fixes

This commit is contained in:
Guillaume Chau
2018-06-11 21:00:36 +02:00
parent 692d4638d8
commit e7e2fb2ea6
2 changed files with 21 additions and 6 deletions

View File

@@ -5,12 +5,18 @@
>
<ApolloSubscribeToMore
:document="require('../graphql/suggestionAdded.gql')"
:updateQuery="(previousResult, { subscriptionData }) => ({
suggestions: [
...previousResult.suggestions,
subscriptionData.data.suggestionAdded
]
})"
:updateQuery="(previousResult, { subscriptionData }) => {
const newSuggestion = subscriptionData.data.suggestionAdded
if (previousResult.suggestions.find(s => s.id === newSuggestion.id)) {
return previousResult
}
return {
suggestions: [
...previousResult.suggestions,
newSuggestion
]
}
}"
/>
<ApolloSubscribeToMore

View File

@@ -1,5 +1,7 @@
// Subs
const channels = require('../channels')
// Connectors
const { log } = require('../utils/logger')
const suggestions = []
@@ -25,11 +27,14 @@ function add (suggestion, context) {
suggestionAdded: suggestion
})
log('Suggestion added', suggestion.id)
return suggestion
}
function remove (id, context) {
const suggestion = findOne(id)
if (!suggestion) return
const index = suggestions.indexOf(suggestion)
suggestions.splice(index, 1)
@@ -37,6 +42,8 @@ function remove (id, context) {
suggestionRemoved: suggestion
})
log('Suggestion removed', suggestion.id)
return suggestion
}
@@ -55,6 +62,8 @@ function update (data, context) {
suggestionUpdated: suggestion
})
log('Suggestion updated', suggestion.id)
return suggestion
}