2021-01-29 15:06:02 +00:00
|
|
|
import api from "../api";
|
2021-02-05 11:21:52 +00:00
|
|
|
import { makeAutoObservable } from "mobx";
|
2021-03-21 21:34:21 +00:00
|
|
|
import { combineUrl } from "../utils";
|
|
|
|
import { AppServerConfig } from "../constants";
|
|
|
|
const { proxyURL } = AppServerConfig;
|
2021-01-29 15:06:02 +00:00
|
|
|
|
|
|
|
class ModuleStore {
|
|
|
|
isLoading = false;
|
2021-02-03 12:42:47 +00:00
|
|
|
isLoaded = false;
|
2021-01-29 15:06:02 +00:00
|
|
|
modules = [];
|
|
|
|
|
|
|
|
constructor() {
|
2021-02-05 11:21:52 +00:00
|
|
|
makeAutoObservable(this);
|
2021-01-29 15:06:02 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 09:55:14 +00:00
|
|
|
getModules = async () => {
|
2021-01-29 15:06:02 +00:00
|
|
|
const list = await api.modules.getModulesList();
|
2021-02-20 17:17:27 +00:00
|
|
|
|
2021-03-21 21:34:21 +00:00
|
|
|
const extendedModules = list.map((m) => this.toModuleWrapper(m));
|
2021-02-20 17:17:27 +00:00
|
|
|
|
|
|
|
this.setModules(extendedModules);
|
|
|
|
};
|
|
|
|
|
2021-03-21 21:34:21 +00:00
|
|
|
toModuleWrapper = (item) => {
|
2021-03-12 10:52:30 +00:00
|
|
|
const id =
|
|
|
|
item.id && typeof item.id === "string" ? item.id.toLowerCase() : null;
|
|
|
|
|
2021-03-21 21:34:21 +00:00
|
|
|
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());
|
2021-03-21 21:34:21 +00:00
|
|
|
|
2021-03-12 10:52:30 +00:00
|
|
|
const result = {
|
2021-03-21 21:34:21 +00:00
|
|
|
...item,
|
2021-03-12 10:52:30 +00:00
|
|
|
id,
|
2021-03-21 21:34:21 +00:00
|
|
|
link,
|
2021-03-22 14:35:33 +00:00
|
|
|
origLink: item.link && item.link.toLowerCase(),
|
2021-03-12 10:52:30 +00:00
|
|
|
notifications: 0,
|
2021-03-22 14:35:33 +00:00
|
|
|
iconUrl: item.iconUrl && item.iconUrl.toLowerCase(),
|
|
|
|
imageUrl: item.imageUrl && item.imageUrl.toLowerCase(),
|
2021-03-12 10:52:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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-01-29 15:06:02 +00:00
|
|
|
|
2021-02-03 12:42:47 +00:00
|
|
|
setIsLoaded = (isLoaded) => {
|
|
|
|
this.isLoaded = isLoaded;
|
2021-02-02 09:55:14 +00:00
|
|
|
};
|
2021-02-05 11:21:52 +00:00
|
|
|
|
|
|
|
setModules = (modules) => {
|
|
|
|
this.modules = modules;
|
|
|
|
};
|
2021-01-29 15:06:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default ModuleStore;
|