fix: platform labels in dashboards

This commit is contained in:
Vyacheslav Matyukhin 2022-04-06 00:02:54 +03:00
parent 067832b72f
commit 3771b53e4f
No known key found for this signature in database
GPG Key ID: 3D2A774C5489F96C
6 changed files with 62 additions and 23 deletions

View File

@ -0,0 +1,9 @@
export interface DashboardItem {
id: string;
title: string;
description: string;
contents: any;
timestamp: string;
creator: string;
extra: any;
}

View File

@ -1,41 +1,49 @@
/* Imports */
import axios from "axios";
import { GetServerSideProps } from "next";
import { GetServerSideProps, NextPage } from "next";
import { useRouter } from "next/router"; // https://nextjs.org/docs/api-reference/next/router
import { useState } from "react";
import { DashboardItem } from "../backend/dashboards";
import { DashboardCreator } from "../web/display/dashboardCreator";
import displayForecasts from "../web/display/displayForecasts";
import Layout from "../web/display/layout";
import { addLabelsToForecasts, FrontendForecast } from "../web/platforms";
import { getDashboardForecastsByDashboardId } from "../web/worker/getDashboardForecasts";
/* get Props */
interface Props {
initialDashboardForecasts: FrontendForecast[];
initialDashboardId?: string;
initialDashboardItem?: DashboardItem;
}
export const getServerSideProps: GetServerSideProps = async (context) => {
export const getServerSideProps: GetServerSideProps<Props> = async (
context
) => {
console.log("getServerSideProps: ");
let urlQuery = context.query; // this is an object, not a string which I have to parse!!
// so for instance if the url is metaforecasts.org/dashboards?a=b&c=d
// this returns ({a: "b", c: "d"}})
let urlQuery = context.query;
console.log(urlQuery);
let dashboardId = urlQuery.dashboardId;
let props;
if (!!dashboardId) {
console.log(dashboardId);
let { dashboardForecasts, dashboardItem } =
await getDashboardForecastsByDashboardId({
dashboardId,
});
dashboardForecasts = addLabelsToForecasts(dashboardForecasts);
props = {
initialDashboardForecasts: dashboardForecasts,
initialDashboardId: urlQuery.dashboardId,
initialDashboardItem: dashboardItem,
};
} else {
console.log();
props = {
initialDashboardForecasts: [],
initialDashboardId: urlQuery.dashboardId || null,
initialDashboardItem: null,
initialDashboardId: urlQuery.dashboardId || undefined,
initialDashboardItem: undefined,
};
}
return {
@ -44,10 +52,10 @@ export const getServerSideProps: GetServerSideProps = async (context) => {
};
/* Body */
export default function Home({
const DashboardsPage: NextPage<Props> = ({
initialDashboardForecasts,
initialDashboardItem,
}) {
}) => {
const router = useRouter();
const [dashboardForecasts, setDashboardForecasts] = useState(
initialDashboardForecasts
@ -188,4 +196,6 @@ export default function Home({
</div>
</Layout>
);
}
};
export default DashboardsPage;

View File

@ -5,6 +5,7 @@ import { useRouter } from "next/router"; // https://nextjs.org/docs/api-referenc
import { useState } from "react";
import displayForecasts from "../web/display/displayForecasts";
import { addLabelsToForecasts } from "../web/platforms";
import { getDashboardForecastsByDashboardId } from "../web/worker/getDashboardForecasts";
/* get Props */
@ -24,6 +25,7 @@ export async function getServerSideProps(context) {
await getDashboardForecastsByDashboardId({
dashboardId,
});
dashboardForecasts = addLabelsToForecasts(dashboardForecasts);
props = {
initialDashboardForecasts: dashboardForecasts,
initialDashboardId: urlQuery.dashboardId,

View File

@ -1,4 +1,4 @@
import { Forecast } from "../backend/platforms";
import { Forecast, platforms } from "../backend/platforms";
export interface PlatformConfig {
name: string;
@ -10,3 +10,16 @@ export type FrontendForecast = Forecast & {
platformLabel: string;
visualization?: any;
};
export const addLabelsToForecasts = (
forecasts: Forecast[]
): FrontendForecast[] => {
const platformNameToLabel = Object.fromEntries(
platforms.map((platform) => [platform.name, platform.label])
);
return forecasts.map((result) => ({
...result,
platformLabel: platformNameToLabel[result.platform] || result.platform,
}));
};

View File

@ -2,7 +2,7 @@ import { GetServerSideProps } from "next";
import { getFrontpage } from "../../backend/frontpage";
import { platforms } from "../../backend/platforms";
import { FrontendForecast, PlatformConfig } from "../platforms";
import { addLabelsToForecasts, FrontendForecast, PlatformConfig } from "../platforms";
import searchAccordingToQueryData from "../worker/searchAccordingToQueryData";
/* Common code for / and /capture */
@ -74,10 +74,7 @@ export const getServerSideProps: GetServerSideProps<Props> = async (
const defaultNumDisplay = 21;
const initialNumDisplay = Number(urlQuery.numDisplay) || defaultNumDisplay;
const defaultResults = (await getFrontpage()).map((result) => ({
...result,
platformLabel: platformNameToLabel[result.platform] || result.platform,
}));
const defaultResults = addLabelsToForecasts(await getFrontpage());
const initialResults =
!!initialQueryParameters &&

View File

@ -1,9 +1,17 @@
import axios from "axios";
export async function getDashboardForecastsByDashboardId({ dashboardId }) {
import { DashboardItem } from "../../backend/dashboards";
import { Forecast } from "../../backend/platforms";
export async function getDashboardForecastsByDashboardId({
dashboardId,
}): Promise<{
dashboardForecasts: Forecast[];
dashboardItem: DashboardItem | string;
}> {
console.log("getDashboardForecastsByDashboardId: ");
let dashboardContents = [];
let dashboardItem = null;
let dashboardContents: Forecast[] = [];
let dashboardItem: DashboardItem | any = null;
try {
let { data } = await axios({
url: `${process.env.NEXT_PUBLIC_SITE_URL}/api/dashboard-by-id`,
@ -14,7 +22,7 @@ export async function getDashboardForecastsByDashboardId({ dashboardId }) {
});
console.log(data);
dashboardContents = data.dashboardContents;
dashboardItem = data.dashboardItem;
dashboardItem = data.dashboardItem as DashboardItem;
} catch (error) {
console.log(error);
} finally {