Skip to content

Commit 127f28e

Browse files
Creation of single circuit page base (#304)
* Created the single circuit page based on the key * Corrected typo in the fetching useEffect function * Ran linter and prettier * Copy and paste again the minified version of ALL_CIRCUITS.json * Added public to gitignore and ran prettier --------- Co-authored-by: Loris Olivier <53363974+loris-maru@users.noreply.github.com>
1 parent b071d2d commit 127f28e

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
.vscode/
55
__tests__/**/*.json
66
/src/env.mjs
7+
/public
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
'use client';
2+
3+
import { useParams } from 'next/navigation';
4+
import { useEffect, useState } from 'react';
5+
6+
import { CircuitSchemaProps } from '@/components/explore-section/Circuit/type';
7+
import { buildCircuitMap } from '@/components/explore-section/Circuit/utils/circuits-map';
8+
9+
export default function CircuitDetailPage() {
10+
const [circuitData, setCircuitData] = useState<CircuitSchemaProps | null>(null);
11+
const [loading, setLoading] = useState<boolean>(true);
12+
const [error, setError] = useState<string | null>(null);
13+
14+
const params = useParams();
15+
const circuitKey = params?.key as string | undefined;
16+
17+
useEffect(() => {
18+
const fetchCircuit = async () => {
19+
if (!circuitKey) {
20+
setError('No existing circuit');
21+
setLoading(false);
22+
return;
23+
}
24+
25+
try {
26+
setLoading(true);
27+
const response = await fetch('/api/circuit/data', {
28+
cache: 'no-store',
29+
});
30+
31+
if (!response.ok) {
32+
throw new Error(`Error: ${response.statusText}`);
33+
}
34+
35+
const data = await response.json();
36+
37+
if (data.error || !Array.isArray(data.circuits)) {
38+
throw new Error(data.error || 'Invalid response from API');
39+
}
40+
41+
const { circuits } = data;
42+
const circuitMap = buildCircuitMap(circuits);
43+
const matchingCircuit = circuitMap.get(circuitKey);
44+
45+
if (!matchingCircuit) {
46+
throw new Error(`Circuit with key ${circuitKey} not found!`);
47+
}
48+
49+
setCircuitData(matchingCircuit);
50+
} catch (er) {
51+
setError(
52+
er instanceof Error ? er.message : 'An error occurred while fetching the circuit data'
53+
);
54+
} finally {
55+
setLoading(false);
56+
}
57+
};
58+
59+
fetchCircuit();
60+
}, [circuitKey]);
61+
62+
if (loading) {
63+
return (
64+
<div className="relative flex h-[50vh] w-full items-center justify-center text-lg font-normal text-primary-9">
65+
Loading...
66+
</div>
67+
);
68+
}
69+
70+
if (error) {
71+
return (
72+
<div className="relative flex h-[50vh] w-full items-center justify-center text-lg font-normal text-red-600">
73+
{error}
74+
</div>
75+
);
76+
}
77+
78+
return (
79+
<div className="relative flex w-full flex-col">
80+
{circuitData && <div>Future detail view private name</div>}
81+
</div>
82+
);
83+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { CircuitSchemaProps } from '../type';
2+
3+
export const buildCircuitMap = (
4+
circuits: CircuitSchemaProps[],
5+
map: Map<string, CircuitSchemaProps> = new Map()
6+
): Map<string, CircuitSchemaProps> => {
7+
for (const circuit of circuits) {
8+
map.set(circuit.key, circuit);
9+
if (circuit.subcircuits && circuit.subcircuits.length > 0) {
10+
buildCircuitMap(circuit.subcircuits, map);
11+
}
12+
}
13+
return map;
14+
};

0 commit comments

Comments
 (0)