Добавление нового дашборда

This commit is contained in:
Pavel Gnedov 2023-11-07 01:50:59 +07:00
parent 8f36e6cca2
commit c54baec4ed
4 changed files with 47 additions and 3 deletions

View file

@ -6,5 +6,46 @@ export const DashboardsPage = (): JSX.Element => {
const store = Store.List.create({ loaded: false, list: [] }); const store = Store.List.create({ loaded: false, list: [] });
Store.ListStoreLoadData(store); Store.ListStoreLoadData(store);
return <DashboardsList store={store} />; const onNewDashboardButtonClick = async (e: React.MouseEvent) => {
if (e.target !== e.currentTarget) return;
e.stopPropagation();
const createUrl = `${process.env.REACT_APP_BACKEND}api/dashboard`;
const createResp = await fetch(createUrl, {
method: 'POST',
});
if (!createResp || !createResp.ok) {
alert(`Ошибка - Не удалось создать новый дашборд`);
return;
}
const dashboardId = await createResp.text();
const dashboardName = prompt(
`Dashboard name for dashboardId = ${dashboardId}`,
);
if (dashboardName) {
const modifyUrl = `${process.env.REACT_APP_BACKEND}api/dashboard/${dashboardId}`;
const modifyResp = await fetch(modifyUrl, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ widgets: [], title: dashboardName }),
});
if (!modifyResp || !modifyResp.ok) {
alert(`Не удалось выполнить создание дашборда`);
} else {
alert(`Создан дашборд ${dashboardName}`);
}
return;
} else {
alert(`Создан анонимный дашборд ${dashboardId}`);
return;
}
};
return (
<>
<button onClick={onNewDashboardButtonClick}>New dashboard</button>
<DashboardsList store={store} />
</>
);
}; };

View file

@ -22,6 +22,9 @@ export class DashboardsDataService {
const cfg = await this.dashboardsService.load(id); const cfg = await this.dashboardsService.load(id);
const results: WidgetWithData[] = []; const results: WidgetWithData[] = [];
let isSuccess = false; let isSuccess = false;
if (!cfg?.widgets || cfg?.widgets?.length <= 0) {
return results;
}
for (let i = 0; i < cfg.widgets.length; i++) { for (let i = 0; i < cfg.widgets.length; i++) {
const widget = cfg.widgets[i]; const widget = cfg.widgets[i];
if (widget.collapsed) continue; if (widget.collapsed) continue;

View file

@ -42,7 +42,7 @@ export class DashboardsService {
async load(id: string): Promise<DashboardModel.Data> { async load(id: string): Promise<DashboardModel.Data> {
this.logger.debug(`Load dashboard id - ${id}`); this.logger.debug(`Load dashboard id - ${id}`);
const rawData = await this.loadRawData(id); const rawData = await this.loadRawData(id);
return rawData.data; return rawData?.data || { widgets: [] };
} }
async isExists(id: string): Promise<boolean> { async isExists(id: string): Promise<boolean> {

View file

@ -1,6 +1,6 @@
export type Data = { export type Data = {
widgets: Widget[]; widgets: Widget[];
title: string; title?: string;
} | null; } | null;
export type Dashboard = { export type Dashboard = {