DocSpace-buildtools/packages/asc-web-common/store/ModuleStore.js

79 lines
1.8 KiB
JavaScript
Raw Normal View History

import api from "../api";
import { makeAutoObservable } from "mobx";
import { combineUrl } from "../utils";
import { AppServerConfig } from "../constants";
const { proxyURL } = AppServerConfig;
class ModuleStore {
isLoading = false;
2021-02-03 12:42:47 +00:00
isLoaded = false;
modules = [];
constructor() {
makeAutoObservable(this);
}
2021-02-02 09:55:14 +00:00
getModules = async () => {
const list = await api.modules.getModulesList();
2021-02-20 17:17:27 +00:00
const extendedModules = list.map((m) => this.toModuleWrapper(m));
2021-02-20 17:17:27 +00:00
this.setModules(extendedModules);
};
toModuleWrapper = (item) => {
const id =
item.id && typeof item.id === "string" ? item.id.toLowerCase() : null;
const link = item.link
? combineUrl(proxyURL, item.link.toLowerCase())
: null;
2021-03-22 14:35:33 +00:00
//const iconUrl = combineUrl(proxyURL, item.iconUrl.toLowerCase());
//const imageUrl = combineUrl(proxyURL, item.imageUrl.toLowerCase());
const result = {
...item,
id,
link,
2021-03-22 14:35:33 +00:00
origLink: item.link && item.link.toLowerCase(),
notifications: 0,
2021-03-22 14:35:33 +00:00
iconUrl: item.iconUrl && item.iconUrl.toLowerCase(),
imageUrl: item.imageUrl && item.imageUrl.toLowerCase(),
};
return result;
2021-02-03 12:42:47 +00:00
};
get totalNotificationsCount() {
let totalNotifications = 0;
this.modules
.filter((item) => !item.separator)
.forEach((item) => (totalNotifications += item.notifications || 0));
return totalNotifications;
}
init = async () => {
this.setIsLoading(true);
await this.getModules();
this.setIsLoading(false);
this.setIsLoaded(true);
};
setIsLoading = (isLoading) => {
this.isLoading = isLoading;
};
2021-02-03 12:42:47 +00:00
setIsLoaded = (isLoaded) => {
this.isLoaded = isLoaded;
2021-02-02 09:55:14 +00:00
};
setModules = (modules) => {
this.modules = modules;
};
}
export default ModuleStore;