pleroma-fe/src/stores/lists.js

116 lines
3.6 KiB
JavaScript
Raw Normal View History

2026-01-06 16:23:17 +02:00
import { find, remove } from 'lodash'
2023-04-06 22:13:30 -06:00
import { defineStore } from 'pinia'
2023-04-06 22:17:03 -06:00
export const useListsStore = defineStore('lists', {
state: () => ({
allLists: [],
2026-01-06 16:22:52 +02:00
allListsObject: {},
2023-04-06 22:17:03 -06:00
}),
getters: {
2026-01-06 16:22:52 +02:00
findListTitle() {
2023-04-06 22:17:03 -06:00
return (id) => {
if (!this.allListsObject[id]) return
return this.allListsObject[id].title
}
},
2026-01-06 16:22:52 +02:00
findListAccounts() {
2023-04-06 22:17:03 -06:00
return (id) => [...this.allListsObject[id].accountIds]
2026-01-06 16:22:52 +02:00
},
2023-04-06 22:13:30 -06:00
},
2023-04-06 22:17:03 -06:00
actions: {
2026-01-06 16:22:52 +02:00
setLists(value) {
2023-04-06 22:17:03 -06:00
this.allLists = value
},
2026-01-06 16:22:52 +02:00
createList({ title }) {
return window.vuex.state.api.backendInteractor
.createList({ title })
2023-04-06 22:17:03 -06:00
.then((list) => {
this.setList({ listId: list.id, title })
return list
})
},
2026-01-06 16:22:52 +02:00
fetchList({ listId }) {
return window.vuex.state.api.backendInteractor
.getList({ listId })
2023-04-06 22:17:03 -06:00
.then((list) => this.setList({ listId: list.id, title: list.title }))
},
2026-01-06 16:22:52 +02:00
fetchListAccounts({ listId }) {
return window.vuex.state.api.backendInteractor
.getListAccounts({ listId })
2023-04-06 22:17:03 -06:00
.then((accountIds) => {
if (!this.allListsObject[listId]) {
this.allListsObject[listId] = { accountIds: [] }
}
this.allListsObject[listId].accountIds = accountIds
})
},
2026-01-06 16:22:52 +02:00
setList({ listId, title }) {
2025-12-22 14:14:40 +02:00
window.vuex.state.api.backendInteractor.updateList({ listId, title })
2023-04-06 22:17:03 -06:00
if (!this.allListsObject[listId]) {
this.allListsObject[listId] = { accountIds: [] }
}
this.allListsObject[listId].title = title
2023-04-06 22:19:36 -06:00
2023-04-06 22:17:03 -06:00
const entry = find(this.allLists, { id: listId })
if (!entry) {
this.allLists.push({ id: listId, title })
} else {
entry.title = title
}
},
2026-01-06 16:22:52 +02:00
setListAccounts({ listId, accountIds }) {
2025-02-03 16:37:53 +02:00
const saved = this.allListsObject[listId]?.accountIds || []
2026-01-06 16:22:52 +02:00
const added = accountIds.filter((id) => !saved.includes(id))
const removed = saved.filter((id) => !accountIds.includes(id))
2023-04-06 22:17:03 -06:00
if (!this.allListsObject[listId]) {
this.allListsObject[listId] = { accountIds: [] }
}
this.allListsObject[listId].accountIds = accountIds
if (added.length > 0) {
2026-01-06 16:22:52 +02:00
window.vuex.state.api.backendInteractor.addAccountsToList({
listId,
accountIds: added,
})
2023-04-06 22:17:03 -06:00
}
if (removed.length > 0) {
2026-01-06 16:22:52 +02:00
window.vuex.state.api.backendInteractor.removeAccountsFromList({
listId,
accountIds: removed,
})
2023-04-06 22:17:03 -06:00
}
},
2026-01-06 16:22:52 +02:00
addListAccount({ listId, accountId }) {
return window.vuex.state.api.backendInteractor
2023-04-06 22:17:03 -06:00
.addAccountsToList({ listId, accountIds: [accountId] })
.then((result) => {
if (!this.allListsObject[listId]) {
this.allListsObject[listId] = { accountIds: [] }
}
this.allListsObject[listId].accountIds.push(accountId)
return result
})
},
2026-01-06 16:22:52 +02:00
removeListAccount({ listId, accountId }) {
return window.vuex.state.api.backendInteractor
2023-04-06 22:17:03 -06:00
.removeAccountsFromList({ listId, accountIds: [accountId] })
.then((result) => {
if (!this.allListsObject[listId]) {
this.allListsObject[listId] = { accountIds: [] }
}
const { accountIds } = this.allListsObject[listId]
const set = new Set(accountIds)
set.delete(accountId)
this.allListsObject[listId].accountIds = [...set]
2023-04-06 22:19:36 -06:00
2023-04-06 22:17:03 -06:00
return result
})
},
2026-01-06 16:22:52 +02:00
deleteList({ listId }) {
2023-04-06 22:17:03 -06:00
window.vuex.state.api.backendInteractor.deleteList({ listId })
2023-04-06 22:19:36 -06:00
2023-04-06 22:17:03 -06:00
delete this.allListsObject[listId]
2026-01-06 16:22:52 +02:00
remove(this.allLists, (list) => list.id === listId)
},
},
2023-04-06 22:13:30 -06:00
})