Spaces:
Runtime error
Runtime error
Commit
·
a68b13b
1
Parent(s):
2513f2b
Add option bar for summary and full page
Browse filesSummary part is working as usual. The full page is only rendering the raw HTML content now, will modify it in next step.
- frontend/src/App.js +14 -1
- frontend/src/components/Map.js +25 -14
- main.py +21 -1
frontend/src/App.js
CHANGED
@@ -11,6 +11,7 @@ import React, { useState,
|
|
11 |
function App() {
|
12 |
const [searchQuery, setSearchQuery] = useState('');
|
13 |
const [submittedQuery, setSubmittedQuery] = useState('');
|
|
|
14 |
|
15 |
const handleMapClick = (lat, lng) => {
|
16 |
console.log(`Map clicked at latitude: ${lat}, longitude: ${lng}`);
|
@@ -32,10 +33,22 @@ function App() {
|
|
32 |
value={searchQuery}
|
33 |
onChange={(e) => setSearchQuery(e.target.value)}
|
34 |
/>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
<button type="submit">Search</button>
|
36 |
</form>
|
37 |
</div>
|
38 |
-
<Map
|
|
|
|
|
|
|
|
|
39 |
</div>
|
40 |
);
|
41 |
}
|
|
|
11 |
function App() {
|
12 |
const [searchQuery, setSearchQuery] = useState('');
|
13 |
const [submittedQuery, setSubmittedQuery] = useState('');
|
14 |
+
const [contentType, setContentType] = useState('summary'); // 'summary' or 'full'
|
15 |
|
16 |
const handleMapClick = (lat, lng) => {
|
17 |
console.log(`Map clicked at latitude: ${lat}, longitude: ${lng}`);
|
|
|
33 |
value={searchQuery}
|
34 |
onChange={(e) => setSearchQuery(e.target.value)}
|
35 |
/>
|
36 |
+
<select
|
37 |
+
value={contentType}
|
38 |
+
onChange={(e) => setContentType(e.target.value)}
|
39 |
+
style={{ margin: '0 10px' }}
|
40 |
+
>
|
41 |
+
<option value="summary">Summary</option>
|
42 |
+
<option value="full">Full Content</option>
|
43 |
+
</select>
|
44 |
<button type="submit">Search</button>
|
45 |
</form>
|
46 |
</div>
|
47 |
+
<Map
|
48 |
+
onMapClick={handleMapClick}
|
49 |
+
searchQuery={submittedQuery}
|
50 |
+
contentType={contentType}
|
51 |
+
/>
|
52 |
</div>
|
53 |
);
|
54 |
}
|
frontend/src/components/Map.js
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
-
import React, { useState, useEffect, useRef
|
2 |
-
|
3 |
} from 'react';
|
4 |
import { MapContainer, TileLayer,
|
5 |
useMapEvents,
|
@@ -38,7 +38,7 @@ const ResizeHandler = ({ trigger }) => {
|
|
38 |
}, [trigger, map]);
|
39 |
return null;
|
40 |
};
|
41 |
-
const Map = ( { onMapClick, searchQuery } ) => {
|
42 |
const [markerPosition, setMarkerPosition] = useState([0,0]);
|
43 |
const [wikiContent, setWikiContent] = useState(null);
|
44 |
const [panelSize, setPanelSize] = useState('half');
|
@@ -83,25 +83,36 @@ const Map = ( { onMapClick, searchQuery } ) => {
|
|
83 |
}, []);
|
84 |
|
85 |
|
86 |
-
const fetchWiki = async (pageName) => {
|
87 |
try{
|
88 |
-
const
|
|
|
|
|
|
|
|
|
89 |
const data = await res.json();
|
90 |
-
|
91 |
-
if (
|
92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
}
|
94 |
-
|
95 |
-
catch (error) {
|
96 |
-
console.error("Error fetching Wikipedia content:", error);
|
97 |
-
}
|
98 |
-
};
|
99 |
// const markerPosition = [21.2514, 81.6296];
|
100 |
useEffect(() => {
|
101 |
if (searchQuery) {
|
102 |
fetchWiki(searchQuery);
|
103 |
}
|
104 |
-
}, [searchQuery]);
|
105 |
|
106 |
const togglePanel = () => {
|
107 |
setPanelSize(prev => {
|
|
|
1 |
+
import React, { useState, useEffect, useRef,
|
2 |
+
useCallback
|
3 |
} from 'react';
|
4 |
import { MapContainer, TileLayer,
|
5 |
useMapEvents,
|
|
|
38 |
}, [trigger, map]);
|
39 |
return null;
|
40 |
};
|
41 |
+
const Map = ( { onMapClick, searchQuery, contentType } ) => {
|
42 |
const [markerPosition, setMarkerPosition] = useState([0,0]);
|
43 |
const [wikiContent, setWikiContent] = useState(null);
|
44 |
const [panelSize, setPanelSize] = useState('half');
|
|
|
83 |
}, []);
|
84 |
|
85 |
|
86 |
+
const fetchWiki = useCallback(async (pageName) => {
|
87 |
try{
|
88 |
+
const endpoint = contentType === 'summary'
|
89 |
+
? `${BACKEND_URL}/wiki/${pageName}`
|
90 |
+
: `${BACKEND_URL}/wiki/search/${pageName}`;
|
91 |
+
|
92 |
+
const res = await fetch(endpoint);
|
93 |
const data = await res.json();
|
94 |
+
|
95 |
+
if (contentType === 'summary') {
|
96 |
+
setWikiContent(data);
|
97 |
+
if (data && data.latitude && data.longitude) {
|
98 |
+
setMarkerPosition([data.latitude, data.longitude]);
|
99 |
+
}
|
100 |
+
} else {
|
101 |
+
setWikiContent({
|
102 |
+
title: data.title,
|
103 |
+
content: data.content
|
104 |
+
});
|
105 |
+
}
|
106 |
+
} catch (error) {
|
107 |
+
console.error("Error fetching Wikipedia content:", error);
|
108 |
}
|
109 |
+
}, [contentType]);
|
|
|
|
|
|
|
|
|
110 |
// const markerPosition = [21.2514, 81.6296];
|
111 |
useEffect(() => {
|
112 |
if (searchQuery) {
|
113 |
fetchWiki(searchQuery);
|
114 |
}
|
115 |
+
}, [searchQuery, fetchWiki]);
|
116 |
|
117 |
const togglePanel = () => {
|
118 |
setPanelSize(prev => {
|
main.py
CHANGED
@@ -41,4 +41,24 @@ async def get_wiki_page(page_name: str):
|
|
41 |
"longitude": coords.longitude if coords else None
|
42 |
},
|
43 |
status_code=200
|
44 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
"longitude": coords.longitude if coords else None
|
42 |
},
|
43 |
status_code=200
|
44 |
+
)
|
45 |
+
|
46 |
+
@app.get("/wiki/search/{full_page}")
|
47 |
+
def search_wiki(full_page: str):
|
48 |
+
response = requests.get(f"https://en.wikipedia.org/wiki/{full_page}", timeout=10)
|
49 |
+
try:
|
50 |
+
if response.status_code != 200:
|
51 |
+
return JSONResponse(
|
52 |
+
content={"error": "Page not found"},
|
53 |
+
status_code=404
|
54 |
+
)
|
55 |
+
|
56 |
+
return JSONResponse(
|
57 |
+
content={"title": full_page, "content": str(response.text)},
|
58 |
+
status_code=200
|
59 |
+
)
|
60 |
+
except Exception as e:
|
61 |
+
return JSONResponse(
|
62 |
+
content={"error": str(e), 'response': str(response)},
|
63 |
+
status_code=500
|
64 |
+
)
|