metaforecast/src/pages/index.tsx

90 lines
2.5 KiB
TypeScript
Raw Normal View History

2022-03-16 21:02:34 +00:00
/* Imports */
import React from 'react';
import { getFrontpage } from '../backend/frontpage';
import CommonDisplay from '../web/display/commonDisplay';
import { displayForecastsWrapperForSearch } from '../web/display/displayForecastsWrappers';
import { platformsWithLabels } from '../web/platforms.js';
import searchAccordingToQueryData from '../web/worker/searchAccordingToQueryData.js';
import Layout from './layout.js';
/* get Props */
export async function getServerSideProps(context) {
let urlQuery = context.query; // this is an object, not a string which I have to parse!!
let initialQueryParameters = {
query: "",
starsThreshold: 2,
numDisplay: 21, // 20
forecastsThreshold: 0,
forecastingPlatforms: platformsWithLabels, // weird key value format,
...urlQuery,
};
let frontPageForecasts = await getFrontpage();
2022-03-24 18:41:23 +00:00
frontPageForecasts = frontPageForecasts.map((forecast) => ({
...forecast,
item: {
...forecast.item,
timestamp: forecast.item.timestamp.toJSON(),
},
}));
2022-03-16 21:02:34 +00:00
let initialResults;
let props;
switch (
!!initialQueryParameters &&
initialQueryParameters.query != "" &&
initialQueryParameters.query != undefined
) {
case true:
initialResults = await searchAccordingToQueryData(initialQueryParameters);
props = {
initialQueryParameters: initialQueryParameters,
initialResults: initialResults,
defaultResults: frontPageForecasts, // different from initialResults!
urlQuery: urlQuery,
};
break;
default:
initialResults = frontPageForecasts;
props = {
initialQueryParameters: initialQueryParameters,
initialResults: initialResults,
defaultResults: frontPageForecasts, // different from initialResults!
urlQuery: urlQuery,
};
break;
}
return {
props: props,
};
}
/* Body */
export default function Home({
initialResults,
defaultResults,
initialQueryParameters,
}) {
return (
<Layout key="index" page={"search"}>
<CommonDisplay
initialResults={initialResults}
defaultResults={defaultResults}
initialQueryParameters={initialQueryParameters}
hasSearchbar={true}
hasCapture={false}
hasAdvancedOptions={true}
placeholder={"Find forecasts about..."}
setHasDisplayBeenCapturedOnChangeSearchInputs={() => null}
displaySeeMoreHint={true}
displayForecastsWrapper={displayForecastsWrapperForSearch}
/>
</Layout>
);
}