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
RevLine 
[080a3f3]1import React, { useEffect, useState } from "react";
2import JSOG from "jsog";
3import { transliterate } from "../Util/transliterate";
4import {
[3e98cea]5 SearchResult,
6 SearchResultText,
7 SearchDropdown,
8 SearchResultLink,
9 SearchBox,
10} from "./Styled/Search.style";
11import { useNavigate } from "react-router";
[080a3f3]12function Search() {
13 const [query, setQuery] = useState("");
14 const [professors, setProfessors] = useState([]);
15
16 useEffect(() => {
[6eba109]17 const url = `http://192.168.0.18:8080/public/professors/nameContains/${transliterate(
[080a3f3]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) {
[3e98cea]30 console.log("Fetching error occured", error);
[080a3f3]31 }
32 };
33
34 if (query.length > 2) fetchData();
35 }, [query]);
36
[3e98cea]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
[080a3f3]56 return (
[0df6b9e]57 <div
58 style={{
59 position: "relative",
60 width: "fit-content",
61 float: "right",
62 }}
63 >
[3e98cea]64 <SearchBox
[080a3f3]65 placeholder="Пребарувај..."
66 onChange={(e) => setQuery(e.target.value)}
[3e98cea]67 onKeyDown={handleKeyDown}
68 tabIndex={0}
69 onFocus={expand}
70 onBlur={close}
[080a3f3]71 />
[3e98cea]72 <SearchDropdown
73 display={
74 query.length > 2 && professors.length > 0 && expanded
75 ? "block"
76 : "none"
77 }
[0df6b9e]78 >
79 {query.length > 2 &&
80 professors.slice(0, 7).map((professor) => (
[3e98cea]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">
[0df6b9e]91 {professor.professorName}
[3e98cea]92 </SearchResultText>
93 <SearchResultText weight="normal" size="er">
[0df6b9e]94 {professor.faculty.facultyName}
[3e98cea]95 </SearchResultText>
96 </SearchResultLink>
97 </SearchResult>
[0df6b9e]98 ))}
[3e98cea]99 </SearchDropdown>
[080a3f3]100 </div>
101 );
102}
103
104export default Search;
Note: See TracBrowser for help on using the repository browser.