2021-06-09 02:33:42 +02:00
|
|
|
import { profilesDb } from '../datastores'
|
2020-08-23 21:07:29 +02:00
|
|
|
|
|
|
|
const state = {
|
2020-08-31 23:35:22 +02:00
|
|
|
profileList: [{
|
|
|
|
_id: 'allChannels',
|
2021-03-26 19:30:44 +01:00
|
|
|
name: 'All Channels',
|
2020-08-31 23:35:22 +02:00
|
|
|
bgColor: '#000000',
|
|
|
|
textColor: '#FFFFFF',
|
|
|
|
subscriptions: []
|
|
|
|
}],
|
|
|
|
activeProfile: 0
|
2020-08-23 21:07:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const getters = {
|
|
|
|
getProfileList: () => {
|
2020-08-24 04:56:33 +02:00
|
|
|
return state.profileList
|
2020-08-31 23:35:22 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
getActiveProfile: () => {
|
|
|
|
return state.activeProfile
|
2020-08-23 21:07:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const actions = {
|
2021-06-17 05:16:52 +02:00
|
|
|
async grabAllProfiles({ rootState, dispatch, commit }, defaultName = null) {
|
|
|
|
let profiles = await profilesDb.find({})
|
|
|
|
if (profiles.length === 0) {
|
|
|
|
dispatch('createDefaultProfile', defaultName)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// We want the primary profile to always be first
|
|
|
|
// So sort with that then sort alphabetically by profile name
|
|
|
|
profiles = profiles.sort((a, b) => {
|
|
|
|
if (a._id === 'allChannels') {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
if (b._id === 'allChannels') {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.name - a.name
|
2020-08-23 21:07:29 +02:00
|
|
|
})
|
|
|
|
|
2021-06-17 05:16:52 +02:00
|
|
|
if (state.profileList.length < profiles.length) {
|
|
|
|
const profileIndex = profiles.findIndex((profile) => {
|
|
|
|
return profile._id === rootState.settings.defaultProfile
|
2020-08-24 04:56:33 +02:00
|
|
|
})
|
2021-06-17 05:16:52 +02:00
|
|
|
|
|
|
|
if (profileIndex !== -1) {
|
|
|
|
commit('setActiveProfile', profileIndex)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
commit('setProfileList', profiles)
|
|
|
|
},
|
|
|
|
|
|
|
|
async grabProfileInfo(_, profileId) {
|
|
|
|
console.log(profileId)
|
|
|
|
return await profilesDb.findOne({ _id: profileId })
|
2020-08-24 04:56:33 +02:00
|
|
|
},
|
|
|
|
|
2021-06-17 05:16:52 +02:00
|
|
|
async createDefaultProfile({ dispatch }, defaultName) {
|
2020-08-23 21:07:29 +02:00
|
|
|
const randomColor = await dispatch('getRandomColor')
|
|
|
|
const textColor = await dispatch('calculateColorLuminance', randomColor)
|
|
|
|
const defaultProfile = {
|
|
|
|
_id: 'allChannels',
|
|
|
|
name: defaultName,
|
|
|
|
bgColor: randomColor,
|
|
|
|
textColor: textColor,
|
|
|
|
subscriptions: []
|
|
|
|
}
|
2021-03-27 19:02:53 +01:00
|
|
|
|
2021-06-17 05:16:52 +02:00
|
|
|
await profilesDb.update(
|
|
|
|
{ _id: 'allChannels' },
|
|
|
|
defaultProfile,
|
|
|
|
{ upsert: true }
|
|
|
|
)
|
|
|
|
dispatch('grabAllProfiles')
|
2020-08-23 21:07:29 +02:00
|
|
|
},
|
|
|
|
|
2021-06-17 05:16:52 +02:00
|
|
|
async updateProfile({ dispatch }, profile) {
|
|
|
|
await profilesDb.update(
|
|
|
|
{ _id: profile._id },
|
|
|
|
profile,
|
|
|
|
{ upsert: true }
|
|
|
|
)
|
|
|
|
dispatch('grabAllProfiles')
|
2020-08-23 21:07:29 +02:00
|
|
|
},
|
|
|
|
|
2021-06-17 05:16:52 +02:00
|
|
|
async insertProfile({ dispatch }, profile) {
|
|
|
|
await profilesDb.insert(profile)
|
|
|
|
dispatch('grabAllProfiles')
|
2020-08-23 21:07:29 +02:00
|
|
|
},
|
|
|
|
|
2021-06-17 05:16:52 +02:00
|
|
|
async removeProfile({ dispatch }, profileId) {
|
|
|
|
await profilesDb.remove({ _id: profileId })
|
|
|
|
dispatch('grabAllProfiles')
|
2020-08-31 23:35:22 +02:00
|
|
|
},
|
|
|
|
|
2021-06-17 05:16:52 +02:00
|
|
|
compactProfiles(_) {
|
2021-06-09 02:33:42 +02:00
|
|
|
profilesDb.persistence.compactDatafile()
|
2021-04-21 06:09:06 +02:00
|
|
|
},
|
|
|
|
|
2021-06-17 05:16:52 +02:00
|
|
|
updateActiveProfile({ commit }, index) {
|
2020-08-31 23:35:22 +02:00
|
|
|
commit('setActiveProfile', index)
|
2020-08-23 21:07:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const mutations = {
|
2021-06-17 05:16:52 +02:00
|
|
|
setProfileList(state, profileList) {
|
2020-08-24 04:56:33 +02:00
|
|
|
state.profileList = profileList
|
2020-08-31 23:35:22 +02:00
|
|
|
},
|
2021-06-17 05:16:52 +02:00
|
|
|
setActiveProfile(state, activeProfile) {
|
2020-08-31 23:35:22 +02:00
|
|
|
state.activeProfile = activeProfile
|
2020-08-23 21:07:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
state,
|
|
|
|
getters,
|
|
|
|
actions,
|
|
|
|
mutations
|
|
|
|
}
|