1 | import React, { useState } from "react";
|
---|
2 | import searchIcon from "../../assets/search_icon.png";
|
---|
3 | import routeIcon from "../../assets/route_icon.png";
|
---|
4 | import closeIcon from "../../assets/close_icon.png";
|
---|
5 | import styles from "./SearchBar.module.css";
|
---|
6 |
|
---|
7 | function SearchBar(props) {
|
---|
8 | const [isExpanded, setIsExpanded] = useState(false);
|
---|
9 | const [from, setFrom] = useState("");
|
---|
10 | const [to, setTo] = useState("");
|
---|
11 |
|
---|
12 | const toggleExpanded = () => {
|
---|
13 | setIsExpanded(!isExpanded);
|
---|
14 | };
|
---|
15 |
|
---|
16 | function searchRoom(){
|
---|
17 | props.map.search();
|
---|
18 |
|
---|
19 | }
|
---|
20 |
|
---|
21 | const handleDirectionsSubmit = () => {
|
---|
22 | console.log(`From: ${from}, To: ${to}`);
|
---|
23 | const url = new URL('http://localhost:8080/api/public/navigate');
|
---|
24 | url.searchParams.append('from', from);
|
---|
25 | url.searchParams.append('to', to);
|
---|
26 |
|
---|
27 | fetch(url)
|
---|
28 | .then(response => {
|
---|
29 | if (!response.ok) {
|
---|
30 | throw new Error('Network response was not ok');
|
---|
31 | }
|
---|
32 | return response.json();
|
---|
33 | })
|
---|
34 | .then(data => {
|
---|
35 | console.log('Success:', data);
|
---|
36 | const points = data.map(item => item.coordinates);
|
---|
37 | props.map.drawRoute(points);
|
---|
38 | })
|
---|
39 | .catch(error => {
|
---|
40 | console.error('Error:', error);
|
---|
41 | });
|
---|
42 | };
|
---|
43 |
|
---|
44 | return (
|
---|
45 | <div className={styles.wrapper}>
|
---|
46 | {/* Regular search bar */}
|
---|
47 | {!isExpanded ? (
|
---|
48 | <div className={styles.searchBar}>
|
---|
49 | <input
|
---|
50 | type="search"
|
---|
51 | className={styles.inputField}
|
---|
52 | placeholder="Search location"
|
---|
53 | aria-label="Search"
|
---|
54 | />
|
---|
55 | <div className={styles.buttons}>
|
---|
56 | <button type="button" className={styles.iconButton} onClick={searchRoom}>
|
---|
57 | <img src={searchIcon} alt="Search Icon" />
|
---|
58 | </button>
|
---|
59 | <button type="button" className={styles.iconButton} onClick={toggleExpanded}>
|
---|
60 | <img src={routeIcon} alt="Route Icon" />
|
---|
61 | </button>
|
---|
62 | </div>
|
---|
63 | </div>
|
---|
64 | ) : (
|
---|
65 | /* Expanded view for directions */
|
---|
66 | <div className={styles.directionsContainer}>
|
---|
67 | <div className={styles.directionsInputs}>
|
---|
68 | <input
|
---|
69 | type="text"
|
---|
70 | placeholder="From"
|
---|
71 | aria-label="From"
|
---|
72 | value={from}
|
---|
73 | onChange={(e) => setFrom(e.target.value)}
|
---|
74 | className={styles.inputField}
|
---|
75 | />
|
---|
76 | <input
|
---|
77 | type="text"
|
---|
78 | placeholder="To"
|
---|
79 | aria-label="To"
|
---|
80 | value={to}
|
---|
81 | onChange={(e) => setTo(e.target.value)}
|
---|
82 | className={styles.inputField}
|
---|
83 | />
|
---|
84 | </div>
|
---|
85 | <div className={styles.buttons}>
|
---|
86 | <button type="button" className={styles.iconButton} onClick={handleDirectionsSubmit}>
|
---|
87 | <img src={searchIcon} alt="Submit Directions" />
|
---|
88 | </button>
|
---|
89 | <button type="button" className={styles.iconButton} onClick={toggleExpanded}>
|
---|
90 | <img src={closeIcon} alt="Close Icon" />
|
---|
91 | </button>
|
---|
92 | </div>
|
---|
93 | </div>
|
---|
94 | )}
|
---|
95 | </div>
|
---|
96 | );
|
---|
97 | }
|
---|
98 |
|
---|
99 | export default SearchBar;
|
---|