metaforecast/src/web/worker/getDashboardForecasts.ts

68 lines
1.6 KiB
TypeScript
Raw Normal View History

import axios from "axios";
2022-03-16 21:02:34 +00:00
2022-04-05 21:02:54 +00:00
import { DashboardItem } from "../../backend/dashboards";
import { Forecast } from "../../backend/platforms";
export async function getDashboardForecastsByDashboardId({
dashboardId,
2022-04-11 21:10:07 +00:00
basePath,
}: {
dashboardId: string;
basePath?: string;
2022-04-05 21:02:54 +00:00
}): Promise<{
dashboardForecasts: Forecast[];
dashboardItem: DashboardItem;
2022-04-05 21:02:54 +00:00
}> {
2022-03-16 21:02:34 +00:00
console.log("getDashboardForecastsByDashboardId: ");
2022-04-12 08:57:35 +00:00
if (typeof window === undefined && !basePath) {
2022-04-11 21:10:07 +00:00
throw new Error("`basePath` option is required on server side");
}
2022-04-05 21:02:54 +00:00
let dashboardContents: Forecast[] = [];
let dashboardItem: DashboardItem | any = null;
2022-03-16 21:02:34 +00:00
try {
let { data } = await axios({
2022-04-11 21:10:07 +00:00
url: `${basePath || ""}/api/dashboard-by-id`,
2022-03-16 21:02:34 +00:00
method: "post",
data: {
id: dashboardId,
},
});
console.log(data);
dashboardContents = data.dashboardContents;
2022-04-05 21:02:54 +00:00
dashboardItem = data.dashboardItem as DashboardItem;
2022-03-16 21:02:34 +00:00
} catch (error) {
console.log(error);
} finally {
return {
dashboardForecasts: dashboardContents,
2022-03-16 21:02:34 +00:00
dashboardItem,
};
}
}
export async function createDashboard(payload) {
let data = { dashboardId: null };
try {
let { title, description, ids, creator, extra } = payload;
console.log(payload);
let response = await axios({
url: "/api/create-dashboard-from-ids",
method: "post",
data: {
title: title || "",
description: description || "",
ids: ids,
creator: creator || "",
extra: [],
},
});
data = response.data;
console.log(data);
} catch (error) {
console.log(error);
} finally {
return data;
}
}