juggl/juggl-vue/src/store/index.js

167 lines
5.4 KiB
JavaScript
Raw Normal View History

2020-12-20 17:27:26 +01:00
import Vue from "vue";
import Vuex from "vuex";
import { jugglService } from "@/services/juggl.service.js";
Vue.use(Vuex);
export default new Vuex.Store({
state: {
apiUrl: "https://juggl.giller.dev/api",
2021-01-01 16:35:35 +01:00
projects: {},
records: {},
2020-12-20 17:27:26 +01:00
user: undefined,
2021-01-01 16:35:35 +01:00
auth: undefined,
2020-12-20 17:27:26 +01:00
},
mutations: {
setKey(state, key) {
state.key = key;
},
setProjects(state, projects) {
state.projects = projects;
},
setRecords(state, records) {
state.records = records;
},
2021-01-01 16:35:35 +01:00
setUser(state, user) {
state.user = user;
},
logout(state) {
state.auth = undefined;
// TODO: Doesn't work apparently
localStorage.removeItem("apiKey");
localStorage.removeItem("userId");
},
login(state, { apiKey, userId }) {
state.auth = { apiKey: apiKey, userId: userId };
localStorage.setItem("apiKey", apiKey);
localStorage.setItem("userId", userId);
},
2020-12-20 17:27:26 +01:00
},
getters: {
runningRecords: (state) => {
2021-01-01 16:35:35 +01:00
return Object.values(state.records).filter((record) => record.running);
2020-12-20 17:27:26 +01:00
},
finishedRecords: (state) => {
2021-01-01 16:35:35 +01:00
return Object.values(state.records).filter(
(record) => !record.running
);
2020-12-20 17:27:26 +01:00
},
2021-01-01 16:35:35 +01:00
auth: (state) => state.auth,
2020-12-20 17:27:26 +01:00
apiUrl: (state) => state.apiUrl,
2021-01-01 16:35:35 +01:00
user: (state) => state.user,
isLoggedIn: (state) => !!state.auth,
2020-12-20 17:27:26 +01:00
records: (state) => state.records,
projects: (state) => state.projects,
2021-01-01 16:35:35 +01:00
projectIds: (state) => {
var projectIds = [];
Object.values(state.projects).forEach((project) => {
projectIds.push(project.project_id);
});
return projectIds;
},
runningProjectIds: (state, getters) => {
var runningProjectIds = [];
Object.values(getters.runningRecords).forEach((record) => {
var projectId = record.project_id;
if (runningProjectIds.includes(runningProjectIds) === false) {
runningProjectIds.push(projectId);
}
});
return runningProjectIds;
},
finishedProjectIds: (state, getters) => {
var runningProjectIds = getters.runningProjectIds;
return getters.projectIds.filter(
(id) => !runningProjectIds.includes(id)
);
},
finishedProjects: (state, getters) => {
var ids = getters.finishedProjectIds;
return Object.values(state.projects).filter((project) =>
ids.includes(project.project_id)
);
},
runningProjects: (state, getters) => {
var ids = getters.runningProjectIds;
return Object.values(state.projects).filter((project) =>
ids.includes(project.project_id)
);
},
2020-12-20 17:27:26 +01:00
getProjectById: (state, getters) => (id) => {
2021-01-01 16:35:35 +01:00
return Object.values(getters.projects).find(
2020-12-20 17:27:26 +01:00
(project) => project.project_id === id
);
},
getRecordById: (state, getters) => (id) => {
2021-01-01 16:35:35 +01:00
return Object.values(getters.records).find((record) => record.record_id === id);
2020-12-20 17:27:26 +01:00
},
},
actions: {
loadProjects({ commit }) {
2021-01-01 16:35:35 +01:00
jugglService.getProjects().then((r) => {
commit("setProjects", r.data.projects);
});
},
loadAllRecords({ commit }) {
jugglService.getRecords().then((r) => {
commit("setRecords", r.data.records);
});
},
loadRunningRecords({ commit, getters }) {
jugglService.getRunningRecords().then((r) => {
var allRecords = {
...getters.finishedRecords,
...r.data.records,
};
commit("setRecords", allRecords);
});
},
login({ commit, getters }, { userId, apiKey }) {
// Is already logged in?
if (getters.isLoggedIn) {
this.dispatch("logout");
}
commit("login", { apiKey: apiKey, userId: userId });
return jugglService
.getUser()
.catch(() => {
this.dispatch("logout");
return false;
})
.then((r) => {
commit("setUser", r.data.users[0]);
return true;
});
},
logout({ commit }) {
commit("setUser", undefined);
commit("logout");
2020-12-20 17:27:26 +01:00
},
2021-01-01 16:35:35 +01:00
endRecord(context, recordId) {
return jugglService
.endRecord(recordId)
.catch(() => {
return false;
})
.then(() => {
this.dispatch("loadRunningRecords");
this.dispatch("loadProjects");
return true;
});
2020-12-20 17:27:26 +01:00
},
2021-01-01 16:35:35 +01:00
startRecord(context, projectId) {
return jugglService
.startRecord(projectId)
.catch(() => {
return false;
})
.then(() => {
this.dispatch("loadRunningRecords");
return true;
});
}
2020-12-20 17:27:26 +01:00
},
});