2023-10-20 17:24:45 +02:00
|
|
|
import { defineStore } from 'pinia'
|
|
|
|
import { useUserStore } from '@/stores/user'
|
2023-10-27 15:27:15 +02:00
|
|
|
import { useNotifyStore } from '@/stores/notify'
|
2023-10-20 17:24:45 +02:00
|
|
|
import groupService from '@/services/groupService'
|
|
|
|
|
|
|
|
export const useGroups = defineStore({
|
|
|
|
id: 'groups',
|
|
|
|
|
|
|
|
state: () => {
|
|
|
|
return {
|
|
|
|
items: [],
|
2023-11-02 14:54:12 +01:00
|
|
|
fetchedOn: null,
|
2023-10-20 17:24:45 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
getters: {
|
|
|
|
current(state) {
|
|
|
|
const group = state.items.find(item => item.id === parseInt(useUserStore().preferences.activeGroup))
|
|
|
|
|
|
|
|
return group ? group.name : trans('commons.all')
|
2023-10-27 15:27:15 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
withoutTheAllGroup(state) {
|
|
|
|
return state.items.filter(item => item.id > 0)
|
|
|
|
},
|
|
|
|
|
|
|
|
theAllGroup(state) {
|
|
|
|
return state.items.find(item => item.id == 0)
|
|
|
|
},
|
|
|
|
|
|
|
|
isEmpty() {
|
|
|
|
return this.withoutTheAllGroup.length == 0
|
|
|
|
},
|
|
|
|
|
|
|
|
count() {
|
|
|
|
return this.withoutTheAllGroup.length
|
|
|
|
},
|
2023-10-20 17:24:45 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
2023-10-27 15:27:15 +02:00
|
|
|
/**
|
|
|
|
* Adds or edits a group
|
|
|
|
* @param {object} group
|
|
|
|
*/
|
|
|
|
addOrEdit(group) {
|
|
|
|
const index = this.items.findIndex(g => g.id === parseInt(group.id))
|
|
|
|
|
|
|
|
if (index > -1) {
|
|
|
|
this.items[index] = group
|
|
|
|
useNotifyStore().success({ text: trans('groups.group_name_saved') })
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.items.push(group)
|
|
|
|
useNotifyStore().success({ text: trans('groups.group_successfully_created') })
|
|
|
|
}
|
|
|
|
},
|
2023-10-20 17:24:45 +02:00
|
|
|
|
|
|
|
/**
|
2023-10-27 15:19:33 +02:00
|
|
|
* Fetches the groups collection from the backend
|
2023-10-20 17:24:45 +02:00
|
|
|
*/
|
2023-10-27 15:19:33 +02:00
|
|
|
async fetch() {
|
2023-11-02 14:54:12 +01:00
|
|
|
// We do not want to fetch fresh data multiple times in the same 2s timespan
|
|
|
|
const age = Math.floor(Date.now() - this.fetchedOn)
|
|
|
|
const isNotFresh = age > 2000
|
|
|
|
|
|
|
|
if (isNotFresh) {
|
|
|
|
this.fetchedOn = Date.now()
|
|
|
|
|
|
|
|
await groupService.getAll().then(response => {
|
|
|
|
this.items = response.data
|
|
|
|
})
|
|
|
|
}
|
2023-10-20 17:24:45 +02:00
|
|
|
},
|
|
|
|
|
2023-10-27 15:27:15 +02:00
|
|
|
/**
|
|
|
|
* Deletes a group
|
|
|
|
*/
|
|
|
|
async delete(id) {
|
|
|
|
const user = useUserStore()
|
|
|
|
|
|
|
|
if (confirm(trans('groups.confirm.delete'))) {
|
|
|
|
await groupService.delete(id).then(response => {
|
|
|
|
this.items = this.items.filter(a => a.id !== id)
|
|
|
|
useNotifyStore().success({ text: trans('groups.group_successfully_deleted') })
|
|
|
|
|
|
|
|
// Reset group filter to 'All' (groupId=0) since the backend has already made
|
|
|
|
// the change automatically. This prevents a new request.
|
|
|
|
if( parseInt(user.preferences.activeGroup) === id ) {
|
|
|
|
user.preferences.activeGroup = 0
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2023-10-20 17:24:45 +02:00
|
|
|
},
|
|
|
|
})
|