aboutsummaryrefslogtreecommitdiff
path: root/src/modules/lists.js
diff options
context:
space:
mode:
authorSean King <seanking2919@protonmail.com>2022-08-22 19:08:58 -0600
committerSean King <seanking2919@protonmail.com>2022-08-22 19:08:58 -0600
commitee58e3868c2d58b889d8a32c1b6dfd3732df7584 (patch)
treeda8f8783734740df18cb5c1082d4756bfcf47489 /src/modules/lists.js
parent325930eecb4943bb50344159646a7c62b4bf10b3 (diff)
parentcb6b96b9ba4e71310e823aecc4bb8c22d370397a (diff)
Merge branch 'develop' of git.pleroma.social:pleroma/pleroma-fe into add/edit-status
Diffstat (limited to 'src/modules/lists.js')
-rw-r--r--src/modules/lists.js94
1 files changed, 94 insertions, 0 deletions
diff --git a/src/modules/lists.js b/src/modules/lists.js
new file mode 100644
index 00000000..84c15759
--- /dev/null
+++ b/src/modules/lists.js
@@ -0,0 +1,94 @@
+import { remove, find } from 'lodash'
+
+export const defaultState = {
+ allLists: [],
+ allListsObject: {}
+}
+
+export const mutations = {
+ setLists (state, value) {
+ state.allLists = value
+ },
+ setList (state, { id, title }) {
+ if (!state.allListsObject[id]) {
+ state.allListsObject[id] = {}
+ }
+ state.allListsObject[id].title = title
+
+ if (!find(state.allLists, { id })) {
+ state.allLists.push({ id, title })
+ } else {
+ find(state.allLists, { id }).title = title
+ }
+ },
+ setListAccounts (state, { id, accountIds }) {
+ if (!state.allListsObject[id]) {
+ state.allListsObject[id] = {}
+ }
+ state.allListsObject[id].accountIds = accountIds
+ },
+ deleteList (state, { id }) {
+ delete state.allListsObject[id]
+ remove(state.allLists, list => list.id === id)
+ }
+}
+
+const actions = {
+ setLists ({ commit }, value) {
+ commit('setLists', value)
+ },
+ createList ({ rootState, commit }, { title }) {
+ return rootState.api.backendInteractor.createList({ title })
+ .then((list) => {
+ commit('setList', { id: list.id, title })
+ return list
+ })
+ },
+ fetchList ({ rootState, commit }, { id }) {
+ return rootState.api.backendInteractor.getList({ id })
+ .then((list) => commit('setList', { id: list.id, title: list.title }))
+ },
+ fetchListAccounts ({ rootState, commit }, { id }) {
+ return rootState.api.backendInteractor.getListAccounts({ id })
+ .then((accountIds) => commit('setListAccounts', { id, accountIds }))
+ },
+ setList ({ rootState, commit }, { id, title }) {
+ rootState.api.backendInteractor.updateList({ id, title })
+ commit('setList', { id, title })
+ },
+ setListAccounts ({ rootState, commit }, { id, accountIds }) {
+ const saved = rootState.lists.allListsObject[id].accountIds || []
+ const added = accountIds.filter(id => !saved.includes(id))
+ const removed = saved.filter(id => !accountIds.includes(id))
+ commit('setListAccounts', { id, accountIds })
+ if (added.length > 0) {
+ rootState.api.backendInteractor.addAccountsToList({ id, accountIds: added })
+ }
+ if (removed.length > 0) {
+ rootState.api.backendInteractor.removeAccountsFromList({ id, accountIds: removed })
+ }
+ },
+ deleteList ({ rootState, commit }, { id }) {
+ rootState.api.backendInteractor.deleteList({ id })
+ commit('deleteList', { id })
+ }
+}
+
+export const getters = {
+ findListTitle: state => id => {
+ if (!state.allListsObject[id]) return
+ return state.allListsObject[id].title
+ },
+ findListAccounts: state => id => {
+ return [...state.allListsObject[id].accountIds]
+ }
+}
+
+const lists = {
+ state: defaultState,
+ mutations,
+ actions,
+ getters
+}
+
+export default lists