source: reactapp/src/Components/Search.js@ 6eba109

main
Last change on this file since 6eba109 was 6eba109, checked in by unknown <mlviktor23@…>, 23 months ago

implemented authentication in react

  • Property mode set to 100644
File size: 2.7 KB
Line 
1import React, { useEffect, useState } from "react";
2import JSOG from "jsog";
3import { transliterate } from "../Util/transliterate";
4import {
5 SearchResult,
6 SearchResultText,
7 SearchDropdown,
8 SearchResultLink,
9 SearchBox,
10} from "./Styled/Search.style";
11import { useNavigate } from "react-router";
12function Search() {
13 const [query, setQuery] = useState("");
14 const [professors, setProfessors] = useState([]);
15
16 useEffect(() => {
17 const url = `http://192.168.0.18:8080/public/professors/nameContains/${transliterate(
18 query
19 )}`;
20
21 const fetchData = async () => {
22 try {
23 const response = await fetch(url);
24 var cyclicGraph = await response.json();
25 var jsogStructure = JSOG.encode(cyclicGraph); // has { '@ref': 'ID' } links instead of cycles
26 cyclicGraph = JSOG.decode(jsogStructure);
27 setProfessors(cyclicGraph);
28 //setLoaded(true);
29 } catch (error) {
30 console.log("Fetching error occured", error);
31 }
32 };
33
34 if (query.length > 2) fetchData();
35 }, [query]);
36
37 const navigate = useNavigate();
38
39 const handleKeyDown = (event) => {
40 if (event.key === "Enter") {
41 setExpanded(false);
42 navigate("/search", { state: professors });
43 }
44 };
45
46 const [expanded, setExpanded] = useState(false);
47
48 function expand() {
49 setExpanded(true);
50 }
51
52 function close() {
53 setExpanded(false);
54 }
55
56 return (
57 <div
58 style={{
59 position: "relative",
60 width: "fit-content",
61 float: "right",
62 }}
63 >
64 <SearchBox
65 placeholder="Пребарувај..."
66 onChange={(e) => setQuery(e.target.value)}
67 onKeyDown={handleKeyDown}
68 tabIndex={0}
69 onFocus={expand}
70 onBlur={close}
71 />
72 <SearchDropdown
73 display={
74 query.length > 2 && professors.length > 0 && expanded
75 ? "block"
76 : "none"
77 }
78 >
79 {query.length > 2 &&
80 professors.slice(0, 7).map((professor) => (
81 <SearchResult
82 key={professor.professorId}
83 onMouseDown={(event) => {
84 event.preventDefault();
85 event.stopPropagation();
86 }}
87 margin="0"
88 >
89 <SearchResultLink href={"/professor/" + professor.professorId}>
90 <SearchResultText weight="bold" size="medium">
91 {professor.professorName}
92 </SearchResultText>
93 <SearchResultText weight="normal" size="er">
94 {professor.faculty.facultyName}
95 </SearchResultText>
96 </SearchResultLink>
97 </SearchResult>
98 ))}
99 </SearchDropdown>
100 </div>
101 );
102}
103
104export default Search;
Note: See TracBrowser for help on using the repository browser.