From 78db0db637ca0976e767ef43eaf5239fdb5de3c8 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Mon, 7 Nov 2016 18:04:00 +0100 Subject: [PATCH] Restructure status module for easier testing. --- src/modules/statuses.js | 68 +++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/src/modules/statuses.js b/src/modules/statuses.js index 5fb57a4f..38cba185 100644 --- a/src/modules/statuses.js +++ b/src/modules/statuses.js @@ -2,7 +2,7 @@ import { map, slice, last, intersectionBy, sortBy, unionBy, toInteger, groupBy, import moment from 'moment' import apiService from '../services/api/api.service.js' -const defaultState = { +export const defaultState = { allStatuses: [], maxId: 0, 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 = { state: defaultState, actions: { @@ -124,38 +157,7 @@ const statuses = { apiService.unfavorite({ id: status.id, credentials: rootState.users.currentUser.credentials }) } }, - 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 - } - }) - } - } + mutations } export default statuses