Restructure status module for easier testing.

This commit is contained in:
Roger Braun 2016-11-07 18:04:00 +01:00
parent 26d47c0f41
commit 78db0db637
1 changed files with 35 additions and 33 deletions

View File

@ -2,7 +2,7 @@ import { map, slice, last, intersectionBy, sortBy, unionBy, toInteger, groupBy,
import moment from 'moment' import moment from 'moment'
import apiService from '../services/api/api.service.js' import apiService from '../services/api/api.service.js'
const defaultState = { export const defaultState = {
allStatuses: [], allStatuses: [],
maxId: 0, maxId: 0,
timelines: { timelines: {
@ -110,6 +110,39 @@ const updateTimestampsInStatuses = (statuses) => {
}) })
} }
export const mutations = {
addNewStatuses (state, { statuses, showImmediately = false, timeline }) {
state.timelines[timeline] = addStatusesToTimeline(statuses, showImmediately, state.timelines[timeline])
state.allStatuses = unionBy(state.timelines[timeline].statuses, state.allStatuses.id)
},
showNewStatuses (state, { timeline }) {
const oldTimeline = (state.timelines[timeline])
oldTimeline.newStatusCount = 0
oldTimeline.visibleStatuses = slice(oldTimeline.statuses, 0, 50)
},
updateTimestamps (state) {
updateTimestampsInStatuses(state.allStatuses)
},
setFavorited (state, { status, value }) {
const newStatus = find(state.allStatuses, status)
newStatus.favorited = value
},
setLoading (state, { timeline, value }) {
state.timelines[timeline].loading = value
},
setNsfw (state, { id, nsfw }) {
// For now, walk through all the statuses because the stuff might be in the replied_to_status
// TODO: Save the replied_tos as references.
each(state.allStatuses, (statusoid) => {
const status = statusoid.retweeted_status || statusoid
if (status.id === id) {
status.nsfw = nsfw
}
})
}
}
const statuses = { const statuses = {
state: defaultState, state: defaultState,
actions: { actions: {
@ -124,38 +157,7 @@ const statuses = {
apiService.unfavorite({ id: status.id, credentials: rootState.users.currentUser.credentials }) apiService.unfavorite({ id: status.id, credentials: rootState.users.currentUser.credentials })
} }
}, },
mutations: { mutations
addNewStatuses (state, { statuses, showImmediately = false, timeline }) {
state.timelines[timeline] = addStatusesToTimeline(statuses, showImmediately, state.timelines[timeline])
state.allStatuses = unionBy(state.timelines[timeline].statuses, state.allStatuses.id)
},
showNewStatuses (state, { timeline }) {
const oldTimeline = (state.timelines[timeline])
oldTimeline.newStatusCount = 0
oldTimeline.visibleStatuses = slice(oldTimeline.statuses, 0, 50)
},
updateTimestamps (state) {
updateTimestampsInStatuses(state.allStatuses)
},
setFavorited (state, { status, value }) {
const newStatus = find(state.allStatuses, status)
newStatus.favorited = value
},
setLoading (state, { timeline, value }) {
state.timelines[timeline].loading = value
},
setNsfw (state, { id, nsfw }) {
// For now, walk through all the statuses because the stuff might be in the replied_to_status
// TODO: Save the replied_tos as references.
each(state.allStatuses, (statusoid) => {
const status = statusoid.retweeted_status || statusoid
if (status.id === id) {
status.nsfw = nsfw
}
})
}
}
} }
export default statuses export default statuses