1 | import React, { useState, useEffect } from "react";
|
---|
2 | import styles from "./LoadMap.module.css";
|
---|
3 | import HttpService from "../../scripts/net/HttpService";
|
---|
4 |
|
---|
5 | const 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 |
|
---|
67 | export default MapTemplateSelector;
|
---|