Files
sockeye-client/src/api/enums.js
2022-12-27 18:55:47 +00:00

102 lines
3.0 KiB
JavaScript

export default {
get(enumKey, enumValue) {
enumKey = enumKey.toLowerCase();
if (enumKey != "authorizationroles") {
if (window.$gz.store.state.enums[enumKey] == undefined) {
throw new Error(
"ERROR enums::get -> enumKey " + enumKey + " is missing from store"
);
}
const ret = window.$gz.store.state.enums[enumKey][enumValue];
if (ret == undefined) {
return "";
} else {
return ret;
}
} else {
const ret = [];
if (enumValue == null || enumValue == 0) {
return "";
}
const availableRoles = this.getSelectionList("AuthorizationRoles");
for (let i = 0; i < availableRoles.length; i++) {
const role = availableRoles[i];
if (enumValue & role.id) {
ret.push(role.name);
}
}
return ret.join(", ");
}
},
//////////////////////////////////
//
// Used by forms to fetch selection list data
// Sorts alphabetically by default but can be turned off with do not sort
//
getSelectionList(enumKey, noSort) {
enumKey = enumKey.toLowerCase();
const e = window.$gz.store.state.enums[enumKey];
if (!e) {
throw new Error(
"ERROR enums::getSelectionList -> enumKey " +
enumKey +
" is missing from store"
);
}
const ret = [];
//turn it into an array suitable for selection lists
for (const [key, value] of Object.entries(e)) {
ret.push({ id: Number(key), name: value });
}
//sort by name
if (!noSort) {
ret.sort(window.$gz.util.sortByKey("name"));
}
return ret;
},
///////////////////////////////////
//
// Fetches enum list from server
// and puts in store. if necessary
// ACCEPTS an ARRAY or a single STRING KEY
//
async fetchEnumList(enumKey) {
if (!Array.isArray(enumKey)) {
enumKey = [enumKey];
}
for (let i = 0; i < enumKey.length; i++) {
//check if list
//if not then fetch it and store it
const k = enumKey[i].toLowerCase();
//de-lodash
// if (!window.$gz. _.has(window.$gz.store.state.enums, k)) {
//enums is an object this is checking if that object has a key with the name in k
if (!window.$gz.util.has(window.$gz.store.state.enums, k)) {
const that = this;
const dat = await that.fetchEnumKey(k);
//massage the data as necessary
const e = { enumKey: k, items: {} };
for (let i = 0; i < dat.length; i++) {
const o = dat[i];
e.items[o.id] = o.name;
}
//stuff the data into the store
window.$gz.store.commit("setEnum", e);
}
}
},
async fetchEnumKey(enumKey) {
const res = await window.$gz.api.get("enum-list/list/" + enumKey);
//We never expect there to be no data here
//if (!Object.prototype.hasOwnProperty.call(res, "data")) {
if (!Object.prototype.hasOwnProperty.call(res, "data")) {
return Promise.reject(res);
}
return res.data;
}
};