Fetch roadmap json for interactive version

pull/1971/head
Kamran Ahmed 3 years ago
parent c16bed02ca
commit 5881c27526
  1. 40
      pages/[roadmap]/interactive.tsx

@ -8,21 +8,35 @@ import Helmet from '../../components/helmet';
import { useEffect, useRef, useState } from 'react'; import { useEffect, useRef, useState } from 'react';
import { wireframeJSONToSVG } from '../../lib/renderer'; import { wireframeJSONToSVG } from '../../lib/renderer';
import { RoadmapPageHeader } from '../../components/roadmap/roadmap-page-header'; import { RoadmapPageHeader } from '../../components/roadmap/roadmap-page-header';
import RoadmapGroup from './[group]';
import { ContentDrawer } from '../../components/roadmap/content-drawer'; import { ContentDrawer } from '../../components/roadmap/content-drawer';
import { useFetch } from 'use-http';
type RoadmapProps = { type RoadmapProps = {
roadmap: RoadmapType; roadmap: RoadmapType;
json: any;
}; };
function RoadmapRenderer(props: RoadmapProps) { function RoadmapRenderer(props: RoadmapProps) {
const { json, roadmap } = props; const { roadmap } = props;
const { loading, error, get } = useFetch();
const roadmapRef = useRef(null); const roadmapRef = useRef(null);
const [roadmapJson, setRoadmapJson] = useState(null);
const [groupId, setGroupId] = useState(''); const [groupId, setGroupId] = useState('');
const [hasError, setHasError] = useState(false); const [hasError, setHasError] = useState(false);
useEffect(() => {
get(`/project/${roadmap.id}.json`)
.then((roadmapJson) => {
setRoadmapJson(roadmapJson);
})
.catch((err) => {
console.error(err);
setHasError(true);
});
}, []);
// Event bindings for the roadmap interactivity
useEffect(() => { useEffect(() => {
window.addEventListener('keydown', (event: KeyboardEvent) => { window.addEventListener('keydown', (event: KeyboardEvent) => {
if (event.key.toLowerCase() === 'escape') { if (event.key.toLowerCase() === 'escape') {
@ -41,10 +55,14 @@ function RoadmapRenderer(props: RoadmapProps) {
// will be translated to `internet:how-does-the-internet-work` // will be translated to `internet:how-does-the-internet-work`
setGroupId(groupId.replace(/^\d+-/, '')); setGroupId(groupId.replace(/^\d+-/, ''));
}); });
}); }, []);
useEffect(() => { useEffect(() => {
wireframeJSONToSVG(json) if (!roadmapJson) {
return;
}
wireframeJSONToSVG(roadmapJson)
.then((svgElement) => { .then((svgElement) => {
const container: HTMLElement = roadmapRef.current!; const container: HTMLElement = roadmapRef.current!;
if (!container) { if (!container) {
@ -60,7 +78,7 @@ function RoadmapRenderer(props: RoadmapProps) {
.catch((err) => { .catch((err) => {
setHasError(true); setHasError(true);
}); });
}, [json]); }, [roadmapJson]);
return ( return (
<Container maxW={'container.lg'} position="relative"> <Container maxW={'container.lg'} position="relative">
@ -76,7 +94,7 @@ function RoadmapRenderer(props: RoadmapProps) {
} }
export default function InteractiveRoadmap(props: RoadmapProps) { export default function InteractiveRoadmap(props: RoadmapProps) {
const { roadmap, json } = props; const { roadmap } = props;
return ( return (
<Box bg="white" minH="100vh"> <Box bg="white" minH="100vh">
@ -88,7 +106,7 @@ export default function InteractiveRoadmap(props: RoadmapProps) {
/> />
<Box mb="60px"> <Box mb="60px">
<RoadmapPageHeader roadmap={roadmap} /> <RoadmapPageHeader roadmap={roadmap} />
<RoadmapRenderer json={json} roadmap={roadmap} /> <RoadmapRenderer roadmap={roadmap} />
</Box> </Box>
<OpensourceBanner /> <OpensourceBanner />
@ -125,15 +143,9 @@ type ContextType = {
export async function getStaticProps(context: ContextType) { export async function getStaticProps(context: ContextType) {
const roadmapId: string = context?.params?.roadmap; const roadmapId: string = context?.params?.roadmap;
let roadmapJson = {};
try {
roadmapJson = require(`../../public/project/${roadmapId}.json`);
} catch (e) {}
return { return {
props: { props: {
roadmap: getRoadmapById(roadmapId), roadmap: getRoadmapById(roadmapId),
json: roadmapJson,
}, },
}; };
} }

Loading…
Cancel
Save