FreeTube/src/renderer/store/modules/profile.js

118 lines
2.6 KiB
JavaScript
Raw Normal View History

2020-08-23 21:07:29 +02:00
import Datastore from 'nedb'
let dbLocation
if (window && window.process && window.process.type === 'renderer') {
// Electron is being used
/* let dbLocation = localStorage.getItem('dbLocation')
if (dbLocation === null) {
const electron = require('electron')
dbLocation = electron.remote.app.getPath('userData')
} */
const electron = require('electron')
dbLocation = electron.remote.app.getPath('userData')
dbLocation = dbLocation + '/profiles.db'
} else {
dbLocation = 'profiles.db'
}
const profileDb = new Datastore({
filename: dbLocation,
autoload: true
})
const state = {
profileList: [],
activeProfile: 'allChannels'
}
const getters = {
getProfileList: () => {
2020-08-24 04:56:33 +02:00
return state.profileList
2020-08-23 21:07:29 +02:00
}
}
const actions = {
grabAllProfiles ({ dispatch, commit }, defaultName = null) {
profileDb.find({}, (err, results) => {
if (!err) {
2020-08-24 04:56:33 +02:00
console.log(results)
2020-08-23 21:07:29 +02:00
if (results.length === 0) {
dispatch('createDefaultProfile', defaultName)
} else {
commit('setProfileList', results)
}
}
})
},
2020-08-24 04:56:33 +02:00
grabProfileInfo (_, profileId) {
return new Promise((resolve, reject) => {
console.log(profileId)
profileDb.findOne({ _id: profileId }, (err, results) => {
if (!err) {
resolve(results)
}
})
})
},
2020-08-23 21:07:29 +02:00
async createDefaultProfile ({ dispatch }, defaultName) {
const randomColor = await dispatch('getRandomColor')
const textColor = await dispatch('calculateColorLuminance', randomColor)
const defaultProfile = {
_id: 'allChannels',
name: defaultName,
bgColor: randomColor,
textColor: textColor,
subscriptions: []
}
console.log(defaultProfile)
profileDb.update({ _id: 'allChannels' }, defaultProfile, { upsert: true }, (err, numReplaced) => {
if (!err) {
dispatch('grabAllProfiles')
}
})
},
updateProfile ({ dispatch }, profile) {
2020-08-24 04:56:33 +02:00
profileDb.update({ _id: profile._id }, profile, { upsert: true }, (err, numReplaced) => {
2020-08-23 21:07:29 +02:00
if (!err) {
dispatch('grabAllProfiles')
}
})
},
2020-08-24 04:56:33 +02:00
insertProfile ({ dispatch }, profile) {
profileDb.insert(profile, (err, newDocs) => {
2020-08-23 21:07:29 +02:00
if (!err) {
2020-08-24 04:56:33 +02:00
dispatch('grabAllProfiles')
2020-08-23 21:07:29 +02:00
}
})
},
2020-08-24 04:56:33 +02:00
removeProfile ({ dispatch }, videoId) {
profileDb.remove({ videoId: videoId }, (err, numReplaced) => {
2020-08-23 21:07:29 +02:00
if (!err) {
dispatch('grabHistory')
}
})
}
}
const mutations = {
2020-08-24 04:56:33 +02:00
setProfileList (state, profileList) {
state.profileList = profileList
2020-08-23 21:07:29 +02:00
}
}
export default {
state,
getters,
actions,
mutations
}