-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSearchbarView.jsx
More file actions
134 lines (115 loc) · 4.87 KB
/
SearchbarView.jsx
File metadata and controls
134 lines (115 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import React, { useState, useEffect } from "react";
import { getAuth, signInWithPopup, signOut, GoogleAuthProvider } from "firebase/auth";
import { observer } from "mobx-react-lite";
import project_logo from "../assets/project_icon.png";
import FavouritesDropdown from "./Components/FavouriteDropdown.jsx";
function SearchbarView(props) {
// const [searchQuery, setSearchQuery] = useState("");
const [user, setUser] = useState(null);
const [showFavourites, setShowFavourites] = useState(false);
const auth = getAuth();
const provider = new GoogleAuthProvider();
useEffect(() => {
const unsubscribe = auth.onAuthStateChanged((user) => {
setUser(user);
});
return () => unsubscribe();
}, [auth]);
const handleSearch = (query) => {
props.resetScrollPosition();
props.setSearchQuery(query);
props.searchCourses(query);
};
const handleSignIn = async () => {
try {
await signInWithPopup(auth, provider);
} catch (error) {
console.error("Error: ", error);
}
};
const handleSignOut = () => {
signOut(auth);
};
const handleClickOutside = (e) => {
if (!e.target.closest('.favourites-container')) {
setShowFavourites(false);
}
};
return (
<div
className=" w-full sm:px-6 sm:py-6 pt-4 pb-4 sm:flex p-2 items-center sm:justify-between"
onClick={handleClickOutside}>
<a href="https://course-compass.se/"
className="flex justify-center sm:h-[90px] h-[120px] w-auto ">
<img src={project_logo} className="h-[90px] w-auto" alt="KTH Logo" />
</a>
<div className="flex justify-center sm:pr-20">
<input
type="text"
placeholder="Search for courses..."
value={props.searchQuery}
onChange={(e) => handleSearch(e.target.value)}
onClick={(e) => e.stopPropagation()}
className="sm:w-[400px] max-w-full h-[44px] pl-4 pr-4 bg-white text-black rounded-full "
/>
</div>
<div className=" flex sm:gap-4 gap-1 pt-4 sm:pr-0">
{props.share}
<a
className="flex items-center justify-center w-[120px] h-[44px] bg-[#003399] text-white rounded-full border border-[#000061] cursor-pointer hover:bg-[#001a4d] transition-all duration-200"
href = "https://inferencekth.github.io/Course-Compass/"
target="_blank"
rel="noopener noreferrer"
>
About us
</a>
<div className="relative favourites-container">
<button
onClick={(e) => {
e.stopPropagation();
setShowFavourites(!showFavourites);
}}
className={`w-[120px] h-[44px] ${
!showFavourites
? 'bg-[#003399] text-white rounded-full border border-[#000061] cursor-pointer hover:bg-[#001a4d] transition-all duration-200'
: 'bg-[#003399] rounded-full border border-red-600 transition-all duration-200 cursor-pointer text-red-600 hover:bg-red-600 hover:text-white border-solid font-bold'
// cursor-pointer text-red-600 hover:bg-red-600 hover:text-white border-r border-solid border-violet-400 font-semibold transition-colors
}`}>
{showFavourites? "Close":"Favourites"}
</button>
{showFavourites && (
<FavouritesDropdown
{...props}
onClick={
(e) => e.stopPropagation()
}
/>
)}
</div>
<div className=" flex items-center cursor-pointer">
{user ? (
<button
onClick={handleSignOut}
className="w-[120px] h-[44px] bg-[#003399] text-white rounded-full border border-[#000061] cursor-pointer hover:bg-[#001a4d] transition-all duration-200">
Sign out
</button>
) : (
<button
onClick={handleSignIn}
className="w-auto min-w-[120px] h-[44px] bg-[#003399] text-white text-sm rounded-full border border-[#001a4d] cursor-pointer hover:bg-[#001a4d] transition-all duration-200 flex items-center justify-center px-4">
Sign in with Google
</button>
)}
{user && (
<img
src={user.photoURL}
alt="Profile"
className="w-[44px] h-[44px] rounded-full border border-[#000061] ml-2"
/>
)}
</div>
</div>
</div>
);
}
export default observer(SearchbarView);