source: imaps-frontend/src/components/MapTemplateSelector/LoadMap.jsx

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 2.0 KB
Line 
1import React, { useState, useEffect } from "react";
2import styles from "./LoadMap.module.css";
3import HttpService from "../../scripts/net/HttpService";
4
5const MapTemplateSelector = ({ loadHandler }) => {
6 const [templates, setTemplates] = useState([]);
7 const [selectedTemplate, setSelectedTemplate] = useState(null);
8 const [loading, setLoading] = useState(true);
9
10 useEffect(() => {
11 const fetchTemplates = async () => {
12 const httpService = new HttpService("http://localhost:8080/api/protected", true);
13 try {
14 const response = await httpService.get("/maps");
15 setTemplates(response);
16 setLoading(false);
17 } catch (error) {
18 console.error("Error fetching templates:", error);
19 setLoading(false);
20 }
21 };
22
23 fetchTemplates();
24 }, []);
25
26 const handleSelect = async (template) => {
27 setSelectedTemplate(template);
28 console.log("Selected template:", template);
29 const httpService = new HttpService("http://localhost:8080/api/protected", true);
30 const response = await httpService.get(`/maps/load?mapName=${template.name}`);
31 console.log("LOAD TEMPLATE: ", response);
32
33 const data = response.map.mapData.textData;
34 loadHandler(data);
35 };
36
37 if (loading) {
38 return <div>Loading map templates...</div>;
39 }
40
41 return (
42 <div className={styles.mapTemplateSelector}>
43 <p>Templates</p>
44 {templates.length > 0 ? (
45 <ul className={styles.templateList}>
46 {templates.map((template) => (
47 <li key={template.id}>
48 <button onClick={() => handleSelect(template)} className={styles.mapTemplateButton}>
49 {template.name}
50 </button>
51 </li>
52 ))}
53 </ul>
54 ) : (
55 <p>No map templates available.</p>
56 )}
57 {selectedTemplate && (
58 <div className={styles.selectedTemplate}>
59 <h3 className={styles.text}>Loaded Template:</h3>
60 <p className={styles.text}>{selectedTemplate.name}</p>
61 </div>
62 )}
63 </div>
64 );
65};
66
67export default MapTemplateSelector;
Note: See TracBrowser for help on using the repository browser.