-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexampleapp.js
More file actions
74 lines (66 loc) · 1.79 KB
/
Copy pathexampleapp.js
File metadata and controls
74 lines (66 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import React, { useEffect, useState } from 'react';
import { BrowserRouter as Router, Route, Link, useParams } from 'react-router-dom';
const App = () => (
<Router>
<Route path="/" exact component={RootList} />
<Route path="/root/:rootId" component={RootDetails} />
</Router>
);
const RootList = () => {
const [roots, setRoots] = useState([]);
useEffect(() => {
fetch('/api/roots')
.then(response => response.json())
.then(data => setRoots(data))
.catch(error => console.error('Error fetching roots:', error));
}, []);
return (
<div>
<h1>Roots</h1>
<ul>
{roots.map(root => (
<li key={root.root}>
<Link to={`/root/${root.root}`}>{root.root}</Link>
</li>
))}
</ul>
</div>
);
};
const RootDetails = () => {
const { rootId } = useParams();
const [script, setScript] = useState('english');
const [data, setData] = useState(null);
useEffect(() => {
fetch(`/api/root/${rootId}?script=${script}`)
.then(response => response.json())
.then(data => setData(data))
.catch(error => console.error('Error fetching root details:', error));
}, [rootId, script]);
return (
<div>
<h1>Root Details</h1>
{data && (
<div>
<h2>Root: {data[0].root.root}</h2>
<h3>Words:</h3>
<ul>
{data[0].words.map((word, index) => (
<li key={index}>{word.text}</li>
))}
</ul>
</div>
)}
<div>
<label>
Script:
<select value={script} onChange={e => setScript(e.target.value)}>
<option value="english">English</option>
<option value="arabic">Arabic</option>
</select>
</label>
</div>
</div>
);
};
export default App;