juggl/src/store/modules/juggl.js

245 lines
8 KiB
JavaScript
Raw Normal View History

2020-12-20 17:27:26 +01:00
import { jugglService } from "@/services/juggl.service.js";
2021-01-03 12:29:41 +01:00
export const juggl = {
2020-12-20 17:27:26 +01:00
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,
2021-01-03 12:29:41 +01:00
usingFinishedRecords: false,
usingRunningRecords: false,
usingProjects: false,
recordsLimit: 0,
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-03 12:29:41 +01:00
usingFinishedRecords(state, using) {
state.usingFinishedRecords = using;
},
usingRunningRecords(state, using) {
state.usingRunningRecords = using;
},
usingProjects(state, using) {
state.usingProjects = using;
},
setRecordsLimit(state, limit) {
state.recordsLimit = limit;
},
2021-01-01 16:35:35 +01:00
setUser(state, user) {
state.user = user;
},
logout(state) {
state.auth = undefined;
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-03 12:29:41 +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-03 12:29:41 +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-03 12:29:41 +01:00
return jugglService.getProjects().then((r) => {
2021-01-01 16:35:35 +01:00
commit("setProjects", r.data.projects);
2021-01-03 12:29:41 +01:00
commit("usingProjects", true);
2021-01-01 16:35:35 +01:00
});
},
2021-01-03 12:29:41 +01:00
loadUser({ commit }) {
return jugglService
.getUser()
.catch(() => {
return false;
})
.then((r) => {
commit("setUser", r.data.users[0]);
return true;
});
},
loadRecords({ commit, state }, { limit, finished }) {
commit("setRecordsLimit", limit);
return jugglService.getRecords({ limit: state.recordsLimit, finished: finished }).then((r) => {
2021-01-01 16:35:35 +01:00
commit("setRecords", r.data.records);
2021-01-03 12:29:41 +01:00
commit("usingFinishedRecords", true);
commit("usingRunningRecords", true);
2021-01-01 16:35:35 +01:00
});
},
loadRunningRecords({ commit, getters }) {
2021-01-03 12:29:41 +01:00
return jugglService.getRunningRecords().then((r) => {
2021-01-01 16:35:35 +01:00
var allRecords = {
...getters.finishedRecords,
...r.data.records,
};
commit("setRecords", allRecords);
2021-01-03 12:29:41 +01:00
commit("usingRunningRecords", true);
2021-01-01 16:35:35 +01:00
});
},
login({ commit, getters }, { userId, apiKey }) {
// Is already logged in?
if (getters.isLoggedIn) {
this.dispatch("logout");
}
commit("login", { apiKey: apiKey, userId: userId });
2021-01-03 12:29:41 +01:00
return this.dispatch("loadUser")
2021-01-01 16:35:35 +01:00
.catch(() => {
this.dispatch("logout");
return false;
})
.then((r) => {
2021-01-03 12:29:41 +01:00
if (r === false) {
this.dispatch("logout");
return false;
} else {
return true;
}
2021-01-01 16:35:35 +01:00
});
},
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(() => {
2021-01-03 12:29:41 +01:00
this.dispatch("updateState");
return true;
});
},
addProject(context, { name }) {
return jugglService
.addProject(name)
.catch(() => {
return false;
})
.then(() => {
this.dispatch("updateState");
2021-01-01 16:35:35 +01:00
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(() => {
2021-01-03 12:29:41 +01:00
this.dispatch("updateState");
return true;
});
},
removeRecord(context, recordId) {
return jugglService
.removeRecord(recordId)
.catch(() => {
return false;
})
.then(() => {
this.dispatch("updateState");
2021-01-01 16:35:35 +01:00
return true;
});
2021-01-03 12:29:41 +01:00
},
updateState({ state }) {
if (state.usingProjects) {
this.dispatch("loadProjects");
}
if (state.usingRunningRecords && state.usingFinishedRecords) {
this.dispatch("loadRecords");
} else if (state.usingRunningRecords) {
this.dispatch("loadRunningRecords");
}
if (state.user === undefined) {
this.dispatch("loadUser");
}
},
loadSavedLogin({ commit }) {
var userId = localStorage.getItem("userId");
var apiKey = localStorage.getItem("apiKey");
if (userId === undefined || apiKey === undefined) {
return;
}
commit("login", { apiKey: apiKey, userId: userId });
},
2020-12-20 17:27:26 +01:00
},
2021-01-03 12:29:41 +01:00
};