This repository has been archived by the owner on Sep 29, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #893 from SE-TINF22B6/search_and_user_page
Implemented Search and user page
- Loading branch information
Showing
20 changed files
with
549 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/main/web/src/scenes/Search/components/SearchService.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
.search-results { | ||
width: 780px; | ||
display: grid; | ||
grid-template-columns: 1fr; | ||
grid-gap: 10px; | ||
position: relative; | ||
} | ||
|
||
@media (max-width: 412px) { | ||
.search-results { | ||
margin-left: 20px; | ||
margin-top: -5px; | ||
} | ||
} | ||
.user-search-results { | ||
width: 780px; | ||
display: grid; | ||
grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); | ||
gap: 10px; | ||
} | ||
@media (max-width: 412px) { | ||
.user-search-results { | ||
margin-left: 20px; | ||
margin-top: -5px; | ||
} | ||
} | ||
|
||
.animation { | ||
width: 780px; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
} |
151 changes: 151 additions & 0 deletions
151
src/main/web/src/scenes/Search/components/SearchService.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import React, {useEffect, useMemo, useState} from "react"; | ||
import "./SearchService.css"; | ||
import Lottie from "lottie-react"; | ||
import animationData from "../../../assets/loading.json"; | ||
import {useDetectAdBlock} from "adblock-detect-react"; | ||
import {usePreventScrolling} from "../../../organisms/ad-block-overlay/preventScrolling"; | ||
import {getJWT} from "../../../services/AuthService"; | ||
import config from "../../../config/config"; | ||
import {PostModel} from "../../Home/components/post/models/PostModel"; | ||
import {User} from "./User"; | ||
import {Post} from "../../Home/components/post/Post"; | ||
import {UserModel} from "./models/UserModel"; | ||
|
||
|
||
|
||
|
||
interface SearchedPostsProps { | ||
sortOption: string; | ||
query: string | null; | ||
findByOption: string; | ||
} | ||
|
||
export const SearchService: React.FC<SearchedPostsProps> = ({sortOption, query, findByOption}) => { | ||
const [searchResults, setSearchResults] = useState<any[]>([]); | ||
const jwt: string | null = getJWT(); | ||
const headersWithJwt = useMemo(() => ({ | ||
...config.headers, | ||
'Authorization': jwt ? `Bearer ${jwt}` : '' | ||
}), [jwt]); | ||
|
||
const [notFound, setNotFound] = useState(false); | ||
const [loading, setLoading] = useState(true); | ||
const adBlockDetected = useDetectAdBlock(); | ||
usePreventScrolling(adBlockDetected); | ||
|
||
useEffect(() => { | ||
setSearchResults([]); | ||
setNotFound(false); | ||
setLoading(true); | ||
|
||
const fetchResults = async (url: string): Promise<void> => { | ||
try { | ||
const response: Response = await fetch(url, { | ||
headers: headersWithJwt | ||
}); | ||
if (response.ok) { | ||
const data = await response.json(); | ||
setSearchResults(data); | ||
setNotFound(false); | ||
} else { | ||
setNotFound(true); | ||
} | ||
setLoading(false); | ||
} catch (error) { | ||
console.error("Error when retrieving the search data:", error); | ||
setNotFound(true); | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
switch (findByOption) { | ||
case "title": | ||
fetchResults(config.apiUrl + `post/posts-by-keyword/${query}`); | ||
break; | ||
case "tags": | ||
fetchResults(config.apiUrl + `post/posts-by-tag-keyword/${query}`); | ||
break; | ||
case "user": | ||
fetchResults(config.apiUrl + `user/user-by-keyword/${query}`); | ||
break; | ||
default: | ||
setNotFound(true); | ||
setLoading(true); | ||
break; | ||
} | ||
}, [findByOption, query, headersWithJwt]); | ||
|
||
const sortedResults = (): PostModel[] => { | ||
if (!searchResults || searchResults.length === 0) setNotFound(true); | ||
if (findByOption !== "user") { | ||
switch (sortOption) { | ||
case "newest": | ||
return searchResults.sort((a: PostModel, b: PostModel) => | ||
Date.parse(new Date(b.timestamp).toISOString()) - Date.parse(new Date(a.timestamp).toISOString())); | ||
case "following": | ||
return searchResults; | ||
case "popular": | ||
return searchResults.sort((a: PostModel, b: PostModel) => b.likeAmount - a.likeAmount); | ||
default: | ||
return searchResults; | ||
} | ||
} | ||
return searchResults; | ||
}; | ||
|
||
if (loading) { | ||
return ( | ||
<div className="animation"> | ||
<div className="loading-animation"> | ||
<Lottie animationData={animationData}/> | ||
</div> | ||
</div> | ||
|
||
); | ||
} | ||
|
||
if (notFound) { | ||
return ( | ||
<div className="search-results"> | ||
<h1 className="error">No results for search term: {query}</h1> | ||
</div> | ||
); | ||
} | ||
|
||
if (findByOption === "user") { | ||
|
||
return ( | ||
<div className="user-search-results"> | ||
{searchResults.map((user: UserModel) => ( | ||
<User | ||
key={user.userId} | ||
userId={user.userId} | ||
username={user.username} | ||
picture={user.picture} | ||
/>)) | ||
} | ||
</div> | ||
|
||
); | ||
} | ||
|
||
return ( | ||
<div className="search-results"> | ||
{sortedResults().map(post => ( | ||
<Post | ||
key={post.id} | ||
id={post.id} | ||
title={post.title} | ||
description={post.description || ""} | ||
tags={post.tags} | ||
likeAmount={post.likeAmount} | ||
commentAmount={post.commentAmount} | ||
timestamp={post.timestamp} | ||
postImage={post.postImage} | ||
accountId={post.accountId} | ||
username={post.username} | ||
/> | ||
))} | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
.user-container { | ||
font-size: var(--headline-font-size); | ||
align-items: center; | ||
text-align: center; | ||
border-radius: 25px; | ||
background-color: var(--component-grey); | ||
justify-content: center; | ||
width: 120px; | ||
padding: 10px; | ||
color: white; | ||
} | ||
|
||
.user-image { | ||
width: 100px; | ||
height: 100px; | ||
border-radius: 50px; | ||
object-fit: cover; | ||
|
||
} | ||
|
||
.custom-image { | ||
width: 100px; | ||
height: 100px; | ||
border-radius: 50px; | ||
object-fit: cover; | ||
|
||
} | ||
|
||
.user-button { | ||
text-decoration: none; | ||
color: inherit; | ||
} | ||
.user-name{ | ||
padding-top: 10px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import React from 'react'; | ||
import './User.css'; | ||
import {Link} from "react-router-dom"; | ||
import {UserModel} from "./models/UserModel"; | ||
import {Pictures} from "../../../atoms/Pictures/Pictures"; | ||
|
||
export const User: React.FC<UserModel> = (props: UserModel) => { | ||
const { | ||
userId, | ||
username, | ||
picture, | ||
} = props; | ||
|
||
const truncatedUsername = username.length > 17 ? username.slice(0, 15) + "..." : username; | ||
|
||
return ( | ||
<div className="user-container"> | ||
<Link to={`/user/?id=${userId}`} aria-label="To the user" className="user-button"> | ||
{picture && picture.imageData ? ( | ||
<img className="user-image" alt={username} src={picture.imageData} loading="lazy"/> | ||
) : ( | ||
<Pictures defaultPicture={false} className="custom-image"/> | ||
)} | ||
<div className="user-name">{truncatedUsername}</div> | ||
</Link> | ||
</div> | ||
); | ||
} |
Oops, something went wrong.