Skip to content

Commit 64ab8e0

Browse files
committed
Add upload button
1 parent 81c90f3 commit 64ab8e0

File tree

1 file changed

+48
-12
lines changed

1 file changed

+48
-12
lines changed

src/App.tsx

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
1-
import { useState, useEffect } from "react";
1+
import { useState } from "react";
22
import { Card } from "./components/Card";
33
import { Project } from "./data";
44
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@radix-ui/react-tabs";
55
import Contributors from "./components/Contributors";
66

77
function App() {
88
const [data, setData] = useState<Project | null>(null);
9-
// const params = new URLSearchParams(windowLocation.search);
10-
// const apiParam = params.get("api") || "test"; // Default to 'test' if not specified
119

12-
useEffect(() => {
13-
async function fetchData() {
14-
const res = await fetch("test.json");
15-
const data = await res.json();
16-
setData(data);
17-
}
18-
fetchData();
19-
}, []);
10+
const handleFileSelect = async (
11+
event: React.ChangeEvent<HTMLInputElement>
12+
) => {
13+
const file = event.target.files?.[0];
14+
if (!file) return;
15+
16+
const reader = new FileReader();
17+
reader.onload = (e) => {
18+
try {
19+
const json = JSON.parse(e.target?.result as string);
20+
setData(json);
21+
} catch (error) {
22+
console.error("Error parsing JSON:", error);
23+
}
24+
};
25+
reader.readAsText(file);
26+
};
2027

2128
return (
2229
<>
@@ -26,6 +33,16 @@ function App() {
2633
<h1 className="text-4xl">{data.name}</h1>
2734
<sub className="text-xl font-light">STAPLE Project Dashboard</sub>
2835
</header>
36+
<>
37+
<div className="flex justify-center p-4">
38+
<input
39+
type="file"
40+
accept=".json"
41+
onChange={handleFileSelect}
42+
className="block w-full max-w-xs text-sm text-gray-500 file:mr-4 file:py-2 file:px-4 file:rounded-full file:border-0 file:text-sm file:font-semibold file:bg-indigo-50 file:text-indigo-700 hover:file:bg-indigo-100"
43+
/>
44+
</div>
45+
</>
2946
<Tabs defaultValue="contributors">
3047
<TabsList className="flex w-full max-w-2xl mx-auto my-4 space-x-2 rounded-xl bg-gray-100 p-1">
3148
<TabsTrigger
@@ -91,7 +108,26 @@ function App() {
91108
<pre>{JSON.stringify(data, null, 2)}</pre>
92109
</>
93110
) : (
94-
"Loading..."
111+
<>
112+
<header className="bg-gray-800 text-white p-4 text-center">
113+
<sub className="text-xl font-light">STAPLE Project Dashboard</sub>
114+
</header>
115+
<>
116+
<div className="flex justify-center p-4">
117+
<input
118+
type="file"
119+
accept=".json"
120+
onChange={handleFileSelect}
121+
className="block w-full max-w-xs text-sm text-gray-500
122+
file:mr-4 file:py-2 file:px-4
123+
file:rounded-full file:border-0
124+
file:text-sm file:font-semibold
125+
file:bg-indigo-50 file:text-indigo-700
126+
hover:file:bg-indigo-100"
127+
/>
128+
</div>
129+
</>
130+
</>
95131
)}
96132
</>
97133
);

0 commit comments

Comments
 (0)