Small status reducer changes.

This commit is contained in:
Roger Braun 2016-11-19 12:39:10 +01:00
parent de25c46670
commit 47ddf8c3fb

View file

@ -90,12 +90,12 @@ const mergeOrAdd = (arr, item) => {
if (oldItem) { if (oldItem) {
// We already have this, so only merge the new info. // We already have this, so only merge the new info.
merge(oldItem, item) merge(oldItem, item)
return oldItem return {item: oldItem, new: false}
} else { } else {
// This is a new item, prepare it // This is a new item, prepare it
prepareStatus(item) prepareStatus(item)
arr.push(item) arr.push(item)
return item return {item, new: true}
} }
} }
@ -110,13 +110,12 @@ export const mutations = {
} }
const addStatus = (status, showImmediately, addToTimeline = true) => { const addStatus = (status, showImmediately, addToTimeline = true) => {
// Remember the current amount of statuses const result = mergeOrAdd(allStatuses, status)
// We need that to calculate new status count. status = result.item
const prevLength = timelineObject.statuses.length
if (result.new) {
updateMaxId(status) updateMaxId(status)
}
status = mergeOrAdd(allStatuses, status)
// Some statuses should only be added to the global status repository. // Some statuses should only be added to the global status repository.
if (addToTimeline) { if (addToTimeline) {
@ -127,24 +126,24 @@ export const mutations = {
// Add it directly to the visibleStatuses, don't change // Add it directly to the visibleStatuses, don't change
// newStatusCount // newStatusCount
mergeOrAdd(timelineObject.visibleStatuses, status) mergeOrAdd(timelineObject.visibleStatuses, status)
} else { } else if (addToTimeline && result.new) {
// Just change newStatuscount // Just change newStatuscount
timelineObject.newStatusCount += (timelineObject.statuses.length - prevLength) timelineObject.newStatusCount += 1
} }
return status return status
} }
const addNotification = (type, status) => { const addNotification = ({type, status, action}) => {
state.notifications.push({type, status}) state.notifications.push({type, status, action})
} }
const favoriteStatus = (favorite, user) => { const favoriteStatus = (favorite) => {
const status = find(allStatuses, { id: toInteger(favorite.in_reply_to_status_id) }) const status = find(allStatuses, { id: toInteger(favorite.in_reply_to_status_id) })
if (status) { if (status) {
status.fave_num += 1 status.fave_num += 1
if (status.user.id === user.id) { if (status.user.id === user.id) {
addNotification('favorite', status) addNotification({type: 'favorite', status, action: favorite})
} }
} }
return status return status
@ -172,7 +171,7 @@ export const mutations = {
}, },
'favorite': (favorite) => { 'favorite': (favorite) => {
updateMaxId(favorite) updateMaxId(favorite)
favoriteStatus(favorite, user) favoriteStatus(favorite)
}, },
'deletion': ({uri}) => { 'deletion': ({uri}) => {
remove(allStatuses, { tag: uri }) remove(allStatuses, { tag: uri })