Files
raven-client/ayanova/src/store.js
2021-07-09 23:41:53 +00:00

195 lines
6.2 KiB
JavaScript

import Vue from "vue";
import Vuex from "vuex";
import createPersistedState from "vuex-persistedstate";
/* Xeslint-disable */
const MaxLogLength = 100;
Vue.use(Vuex);
//reset all local settings via url in case of fucked up situation
if (window.location.search) {
var searchParams = new URLSearchParams(window.location.search);
if (searchParams.has("reset")) {
localStorage.removeItem("AyaNova");
console.log("RESET LOCAL SETTINGS");
}
}
export default new Vuex.Store({
plugins: [createPersistedState({ key: "AyaNova" })],
state: {
lastClientVersion: "",
authenticated: false,
apiUrl: "",
helpUrl: "",
apiToken: "-",
downloadToken: "-",
tfaEnabled: undefined,
notifyAvailable: true,
isCustomerUser: false,
isSubContractorUser: false,
isScheduleableUser: false,
customerRights: {},
userId: 0,
userName: "NOT AUTHENTICATED",
roles: 0,
userType: 0,
homePage: undefined,
translationText: {},
enums: {}, //all enum values with translated text to match stored as key e.g. enums:={AuthorizationRoles:{0:"no role",1:"Limited role"},UserTypes:{0:"Technician",1:"Client user"}}
userOptions: {
languageOverride: "en-US",
timeZoneOverride: null, //use browser tz by default
currencyName: "USD",
hour12: true,
uiColor: "#000000",
emailAddress: null,
mapUrlTemplate: null
},
globalSettings: {},
navItems: [],
logArray: [],
formSettings: {}, //this is the settings on forms that survive a refresh like grid number of items to show etc
formCustomTemplate: {}, //this is the custom fields settings for forms,
darkMode: false,
knownPassword: false,
newNotificationCount: 0
},
// getters: {
// isOutsideUser: state => {
// return state.userType == 3 || state.userType == 4;
// }
// },
mutations: {
setLastClientVersion(state, data) {
state.lastClientVersion = data;
},
login(state, data) {
// mutate state
state.authenticated = data.authenticated;
state.userId = data.userId;
state.roles = data.roles;
state.apiToken = data.apiToken;
state.userName = data.userName;
state.userType = data.userType;
state.isSubContractorUser = data.userType == 5;
state.isCustomerUser = data.userType == 3 || data.userType == 4;
state.isScheduleableUser = data.userType == 1 || data.userType == 5;
state.downloadToken = data.dlt;
state.tfaEnabled = data.tfaEnabled;
if (data.customerRights) {
state.customerRights = data.customerRights;
//only a customer user could have zero access to notifications
state.notifyAvailable =
data.customerRights.notifyServiceImminent == true ||
data.customerRights.notifyCSRAccepted == true ||
data.customerRights.notifyCSRRejected == true ||
data.customerRights.notifyWOCompleted == true ||
data.customerRights.notifyWOCreated == true;
}
},
logout(state) {
//Things that are reset on logout
state.apiToken = "-";
state.downloadToken = "-";
state.tfaEnabled = undefined;
state.customerRights = {};
state.authenticated = false;
state.userId = 0;
state.userName = "NOT AUTHENTICATED";
state.roles = 0;
state.userType = 0;
state.notifyAvailable = true;
state.isCustomerUser = false;
state.isSubContractorUser = false;
state.isScheduleableUser = false;
state.homePage = undefined;
state.navItems = [];
state.translationText = {};
state.enums = {};
state.formCustomTemplate = {};
state.apiUrl = "";
state.userOptions.languageOverride = "en-US";
state.userOptions.timeZoneOverride = null;
state.userOptions.currencyName = "USD";
state.userOptions.hour12 = true;
state.userOptions.uiColor = "#000000";
state.userOptions.emailAddress = null;
state.userOptions.mapUrlTemplate = null;
state.globalSettings = {};
state.knownPassword = false;
state.newNotificationCount = 0;
},
addNavItem(state, data) {
state.navItems.push(data);
},
setTranslationText(state, data) {
state.translationText[data.key] = data.value;
},
setFormCustomTemplateItem(state, data) {
state.formCustomTemplate[data.formKey + "_concurrencyToken"] =
data.concurrency;
state.formCustomTemplate[data.formKey] = data.value;
},
setUserOptions(state, data) {
// mutate state
state.userOptions.languageOverride = data.languageOverride;
state.userOptions.currencyName = data.currencyName;
state.userOptions.hour12 = data.hour12;
state.userOptions.timeZoneOverride = data.timeZoneOverride;
state.userOptions.emailAddress = data.emailAddress;
state.userOptions.uiColor = data.uiColor;
state.userOptions.mapUrlTemplate = data.mapUrlTemplate;
},
setGlobalSettings(state, data) {
// mutate state
state.globalSettings = data;
},
setEnum(state, data) {
state.enums[data.enumKey] = data.items; //{enumKey:"AuthorizationRoles",items:[{0:"no role"},{1:"Limited role"}]}
},
setAPIURL(state, data) {
state.apiUrl = data;
},
setHelpURL(state, data) {
state.helpUrl = data;
},
logItem(state, msg) {
msg = Date.now() + "|" + msg;
state.logArray.push(msg);
if (state.logArray.length > MaxLogLength) {
//remove beginning elements
state.logArray = state.logArray.slice(
state.logArray.length - MaxLogLength
);
}
},
clearAllFormSettings(state) {
state.formSettings = {};
},
setFormSettings(state, data) {
state.formSettings[data.formKey] = data.formSettings;
},
clearFormSettings(state, formKey) {
delete state.formSettings[formKey];
},
setHomePage(state, data) {
state.homePage = data;
},
setDarkMode(state, data) {
state.darkMode = data;
},
setKnownPassword(state, data) {
state.knownPassword = data;
},
setNewNotificationCount(state, data) {
state.newNotificationCount = data;
},
setTfaEnabled(state, data) {
state.tfaEnabled = data;
}
},
actions: {}
});