fix: platform labels in dashboards
This commit is contained in:
parent
067832b72f
commit
3771b53e4f
9
src/backend/dashboards.ts
Normal file
9
src/backend/dashboards.ts
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
export interface DashboardItem {
|
||||||
|
id: string;
|
||||||
|
title: string;
|
||||||
|
description: string;
|
||||||
|
contents: any;
|
||||||
|
timestamp: string;
|
||||||
|
creator: string;
|
||||||
|
extra: any;
|
||||||
|
}
|
|
@ -1,41 +1,49 @@
|
||||||
/* Imports */
|
/* Imports */
|
||||||
import axios from "axios";
|
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 { useRouter } from "next/router"; // https://nextjs.org/docs/api-reference/next/router
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
|
|
||||||
|
import { DashboardItem } from "../backend/dashboards";
|
||||||
import { DashboardCreator } from "../web/display/dashboardCreator";
|
import { DashboardCreator } from "../web/display/dashboardCreator";
|
||||||
import displayForecasts from "../web/display/displayForecasts";
|
import displayForecasts from "../web/display/displayForecasts";
|
||||||
import Layout from "../web/display/layout";
|
import Layout from "../web/display/layout";
|
||||||
|
import { addLabelsToForecasts, FrontendForecast } from "../web/platforms";
|
||||||
import { getDashboardForecastsByDashboardId } from "../web/worker/getDashboardForecasts";
|
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: ");
|
console.log("getServerSideProps: ");
|
||||||
let urlQuery = context.query; // this is an object, not a string which I have to parse!!
|
let urlQuery = context.query;
|
||||||
// so for instance if the url is metaforecasts.org/dashboards?a=b&c=d
|
|
||||||
// this returns ({a: "b", c: "d"}})
|
|
||||||
console.log(urlQuery);
|
console.log(urlQuery);
|
||||||
let dashboardId = urlQuery.dashboardId;
|
let dashboardId = urlQuery.dashboardId;
|
||||||
let props;
|
let props;
|
||||||
|
|
||||||
if (!!dashboardId) {
|
if (!!dashboardId) {
|
||||||
console.log(dashboardId);
|
|
||||||
let { dashboardForecasts, dashboardItem } =
|
let { dashboardForecasts, dashboardItem } =
|
||||||
await getDashboardForecastsByDashboardId({
|
await getDashboardForecastsByDashboardId({
|
||||||
dashboardId,
|
dashboardId,
|
||||||
});
|
});
|
||||||
|
dashboardForecasts = addLabelsToForecasts(dashboardForecasts);
|
||||||
|
|
||||||
props = {
|
props = {
|
||||||
initialDashboardForecasts: dashboardForecasts,
|
initialDashboardForecasts: dashboardForecasts,
|
||||||
initialDashboardId: urlQuery.dashboardId,
|
initialDashboardId: urlQuery.dashboardId,
|
||||||
initialDashboardItem: dashboardItem,
|
initialDashboardItem: dashboardItem,
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
console.log();
|
|
||||||
props = {
|
props = {
|
||||||
initialDashboardForecasts: [],
|
initialDashboardForecasts: [],
|
||||||
initialDashboardId: urlQuery.dashboardId || null,
|
initialDashboardId: urlQuery.dashboardId || undefined,
|
||||||
initialDashboardItem: null,
|
initialDashboardItem: undefined,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
|
@ -44,10 +52,10 @@ export const getServerSideProps: GetServerSideProps = async (context) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Body */
|
/* Body */
|
||||||
export default function Home({
|
const DashboardsPage: NextPage<Props> = ({
|
||||||
initialDashboardForecasts,
|
initialDashboardForecasts,
|
||||||
initialDashboardItem,
|
initialDashboardItem,
|
||||||
}) {
|
}) => {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const [dashboardForecasts, setDashboardForecasts] = useState(
|
const [dashboardForecasts, setDashboardForecasts] = useState(
|
||||||
initialDashboardForecasts
|
initialDashboardForecasts
|
||||||
|
@ -188,4 +196,6 @@ export default function Home({
|
||||||
</div>
|
</div>
|
||||||
</Layout>
|
</Layout>
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
|
|
||||||
|
export default DashboardsPage;
|
||||||
|
|
|
@ -5,6 +5,7 @@ import { useRouter } from "next/router"; // https://nextjs.org/docs/api-referenc
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
|
|
||||||
import displayForecasts from "../web/display/displayForecasts";
|
import displayForecasts from "../web/display/displayForecasts";
|
||||||
|
import { addLabelsToForecasts } from "../web/platforms";
|
||||||
import { getDashboardForecastsByDashboardId } from "../web/worker/getDashboardForecasts";
|
import { getDashboardForecastsByDashboardId } from "../web/worker/getDashboardForecasts";
|
||||||
|
|
||||||
/* get Props */
|
/* get Props */
|
||||||
|
@ -24,6 +25,7 @@ export async function getServerSideProps(context) {
|
||||||
await getDashboardForecastsByDashboardId({
|
await getDashboardForecastsByDashboardId({
|
||||||
dashboardId,
|
dashboardId,
|
||||||
});
|
});
|
||||||
|
dashboardForecasts = addLabelsToForecasts(dashboardForecasts);
|
||||||
props = {
|
props = {
|
||||||
initialDashboardForecasts: dashboardForecasts,
|
initialDashboardForecasts: dashboardForecasts,
|
||||||
initialDashboardId: urlQuery.dashboardId,
|
initialDashboardId: urlQuery.dashboardId,
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { Forecast } from "../backend/platforms";
|
import { Forecast, platforms } from "../backend/platforms";
|
||||||
|
|
||||||
export interface PlatformConfig {
|
export interface PlatformConfig {
|
||||||
name: string;
|
name: string;
|
||||||
|
@ -10,3 +10,16 @@ export type FrontendForecast = Forecast & {
|
||||||
platformLabel: string;
|
platformLabel: string;
|
||||||
visualization?: any;
|
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,
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
|
@ -2,7 +2,7 @@ import { GetServerSideProps } from "next";
|
||||||
|
|
||||||
import { getFrontpage } from "../../backend/frontpage";
|
import { getFrontpage } from "../../backend/frontpage";
|
||||||
import { platforms } from "../../backend/platforms";
|
import { platforms } from "../../backend/platforms";
|
||||||
import { FrontendForecast, PlatformConfig } from "../platforms";
|
import { addLabelsToForecasts, FrontendForecast, PlatformConfig } from "../platforms";
|
||||||
import searchAccordingToQueryData from "../worker/searchAccordingToQueryData";
|
import searchAccordingToQueryData from "../worker/searchAccordingToQueryData";
|
||||||
|
|
||||||
/* Common code for / and /capture */
|
/* Common code for / and /capture */
|
||||||
|
@ -74,10 +74,7 @@ export const getServerSideProps: GetServerSideProps<Props> = async (
|
||||||
const defaultNumDisplay = 21;
|
const defaultNumDisplay = 21;
|
||||||
const initialNumDisplay = Number(urlQuery.numDisplay) || defaultNumDisplay;
|
const initialNumDisplay = Number(urlQuery.numDisplay) || defaultNumDisplay;
|
||||||
|
|
||||||
const defaultResults = (await getFrontpage()).map((result) => ({
|
const defaultResults = addLabelsToForecasts(await getFrontpage());
|
||||||
...result,
|
|
||||||
platformLabel: platformNameToLabel[result.platform] || result.platform,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const initialResults =
|
const initialResults =
|
||||||
!!initialQueryParameters &&
|
!!initialQueryParameters &&
|
||||||
|
|
|
@ -1,9 +1,17 @@
|
||||||
import axios from "axios";
|
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: ");
|
console.log("getDashboardForecastsByDashboardId: ");
|
||||||
let dashboardContents = [];
|
let dashboardContents: Forecast[] = [];
|
||||||
let dashboardItem = null;
|
let dashboardItem: DashboardItem | any = null;
|
||||||
try {
|
try {
|
||||||
let { data } = await axios({
|
let { data } = await axios({
|
||||||
url: `${process.env.NEXT_PUBLIC_SITE_URL}/api/dashboard-by-id`,
|
url: `${process.env.NEXT_PUBLIC_SITE_URL}/api/dashboard-by-id`,
|
||||||
|
@ -14,7 +22,7 @@ export async function getDashboardForecastsByDashboardId({ dashboardId }) {
|
||||||
});
|
});
|
||||||
console.log(data);
|
console.log(data);
|
||||||
dashboardContents = data.dashboardContents;
|
dashboardContents = data.dashboardContents;
|
||||||
dashboardItem = data.dashboardItem;
|
dashboardItem = data.dashboardItem as DashboardItem;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
} finally {
|
} finally {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user