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'
|
|
|
|
|
|
2026-06-15 20:02:22 +03:00
|
|
|
import { useCredentialsStore } from 'src/stores/credentials.js'
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
addAccountsToList,
|
|
|
|
|
createList,
|
|
|
|
|
deleteList,
|
|
|
|
|
fetchLists,
|
|
|
|
|
getList,
|
|
|
|
|
getListAccounts,
|
|
|
|
|
removeAccountsFromList,
|
|
|
|
|
updateList,
|
|
|
|
|
} from 'src/services/api/api.service.js'
|
|
|
|
|
import { promiseInterval } from 'src/services/promise_interval/promise_interval.js'
|
|
|
|
|
|
2023-04-06 22:17:03 -06:00
|
|
|
export const useListsStore = defineStore('lists', {
|
|
|
|
|
state: () => ({
|
2026-06-15 20:02:22 +03:00
|
|
|
fetcher: null,
|
2023-04-06 22:17:03 -06:00
|
|
|
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-06-15 20:02:22 +03:00
|
|
|
startFetching() {
|
|
|
|
|
promiseInterval(() => {
|
|
|
|
|
this.fetcher = fetchLists({
|
|
|
|
|
credentials: useCredentialsStore().current,
|
|
|
|
|
})
|
|
|
|
|
.then(
|
|
|
|
|
(lists) => this.setLists(lists),
|
|
|
|
|
(rej) => console.error(rej),
|
|
|
|
|
)
|
|
|
|
|
.catch((e) => {
|
|
|
|
|
console.error(e)
|
|
|
|
|
})
|
|
|
|
|
}, 240000)
|
|
|
|
|
},
|
|
|
|
|
stopFetching() {
|
|
|
|
|
this.fetcher?.stop()
|
|
|
|
|
},
|
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 }) {
|
2026-06-15 20:02:22 +03:00
|
|
|
return createList({
|
|
|
|
|
title,
|
|
|
|
|
credentials: useCredentialsStore().current,
|
|
|
|
|
}).then((list) => {
|
|
|
|
|
this.setList({ listId: list.id, title })
|
|
|
|
|
return list
|
|
|
|
|
})
|
2023-04-06 22:17:03 -06:00
|
|
|
},
|
2026-01-06 16:22:52 +02:00
|
|
|
fetchList({ listId }) {
|
2026-06-15 20:02:22 +03:00
|
|
|
return getList({
|
|
|
|
|
listId,
|
|
|
|
|
credentials: useCredentialsStore().current,
|
|
|
|
|
}).then((list) => this.setList({ listId: list.id, title: list.title }))
|
2023-04-06 22:17:03 -06:00
|
|
|
},
|
2026-01-06 16:22:52 +02:00
|
|
|
fetchListAccounts({ listId }) {
|
2026-06-15 20:02:22 +03:00
|
|
|
return getListAccounts({
|
|
|
|
|
listId,
|
|
|
|
|
credentials: useCredentialsStore().current,
|
|
|
|
|
}).then((accountIds) => {
|
|
|
|
|
if (!this.allListsObject[listId]) {
|
|
|
|
|
this.allListsObject[listId] = { accountIds: [] }
|
|
|
|
|
}
|
|
|
|
|
this.allListsObject[listId].accountIds = accountIds
|
|
|
|
|
})
|
2023-04-06 22:17:03 -06:00
|
|
|
},
|
2026-01-06 16:22:52 +02:00
|
|
|
setList({ listId, title }) {
|
2026-06-15 20:02:22 +03:00
|
|
|
updateList({
|
|
|
|
|
listId,
|
|
|
|
|
title,
|
|
|
|
|
credentials: useCredentialsStore().current,
|
|
|
|
|
})
|
2025-12-22 14:14:40 +02:00
|
|
|
|
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-06-15 20:02:22 +03:00
|
|
|
addAccountsToList({
|
2026-01-06 16:22:52 +02:00
|
|
|
listId,
|
|
|
|
|
accountIds: added,
|
2026-06-15 20:02:22 +03:00
|
|
|
credentials: useCredentialsStore().current,
|
2026-01-06 16:22:52 +02:00
|
|
|
})
|
2023-04-06 22:17:03 -06:00
|
|
|
}
|
|
|
|
|
if (removed.length > 0) {
|
2026-06-15 20:02:22 +03:00
|
|
|
removeAccountsFromList({
|
2026-01-06 16:22:52 +02:00
|
|
|
listId,
|
|
|
|
|
accountIds: removed,
|
2026-06-15 20:02:22 +03:00
|
|
|
credentials: useCredentialsStore().current,
|
2026-01-06 16:22:52 +02:00
|
|
|
})
|
2023-04-06 22:17:03 -06:00
|
|
|
}
|
|
|
|
|
},
|
2026-01-06 16:22:52 +02:00
|
|
|
addListAccount({ listId, accountId }) {
|
2026-06-15 20:02:22 +03:00
|
|
|
return addAccountsToList({
|
|
|
|
|
listId,
|
|
|
|
|
accountIds: [accountId],
|
|
|
|
|
credentials: useCredentialsStore().current,
|
|
|
|
|
}).then((result) => {
|
|
|
|
|
if (!this.allListsObject[listId]) {
|
|
|
|
|
this.allListsObject[listId] = { accountIds: [] }
|
|
|
|
|
}
|
|
|
|
|
this.allListsObject[listId].accountIds.push(accountId)
|
|
|
|
|
return result
|
|
|
|
|
})
|
2023-04-06 22:17:03 -06:00
|
|
|
},
|
2026-01-06 16:22:52 +02:00
|
|
|
removeListAccount({ listId, accountId }) {
|
2026-06-15 20:02:22 +03:00
|
|
|
return removeAccountsFromList({
|
|
|
|
|
listId,
|
|
|
|
|
accountIds: [accountId],
|
|
|
|
|
credentials: useCredentialsStore().current,
|
|
|
|
|
}).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
|
|
|
|
2026-06-15 20:02:22 +03:00
|
|
|
return result
|
|
|
|
|
})
|
2023-04-06 22:17:03 -06:00
|
|
|
},
|
2026-01-06 16:22:52 +02:00
|
|
|
deleteList({ listId }) {
|
2026-06-15 20:02:22 +03:00
|
|
|
deleteList({
|
|
|
|
|
listId,
|
|
|
|
|
credentials: useCredentialsStore().current,
|
|
|
|
|
})
|
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
|
|
|
})
|