Skip to content

Commit

Permalink
Fix Bugs, Searches
Browse files Browse the repository at this point in the history
  • Loading branch information
maxtyson123 committed May 4, 2024
1 parent 38c7d9c commit 9d5cdd3
Show file tree
Hide file tree
Showing 9 changed files with 297 additions and 156 deletions.
4 changes: 2 additions & 2 deletions website/src/pages/api/plants/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ export default async function handler(


// Get the info
query = `SELECT id FROM posts WHERE ${tables.post_title} LIKE '${name}%'`
query = `SELECT id FROM posts WHERE ${tables.post_title} LIKE '${name}%' ORDER BY ${tables.post_date} DESC`
if(getExtras){
query = `SELECT id, ${tables.post_title}, ${tables.post_date}, ${tables.post_user_id} FROM posts WHERE ${tables.post_title} LIKE '${name}%'`
query = `SELECT id, ${tables.post_title}, ${tables.post_date}, ${tables.post_user_id} FROM posts WHERE ${tables.post_title} LIKE '${name}%' ORDER BY ${tables.post_date} DESC`
}


Expand Down
4 changes: 1 addition & 3 deletions website/src/pages/api/posts/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,9 @@ export default async function handler(
return response.status(400).json({ error: 'No id provided'});
}

query = `SELECT * FROM users WHERE id = ${id}`;
query = `SELECT * FROM posts WHERE id = ${id}`;
break;

case "followingFeed":

case "generalFeed":

// If not following anyone select the latest posts from everyone
Expand Down
1 change: 0 additions & 1 deletion website/src/pages/api/user/follow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ export default async function handler(
return response.status(400).json({error: 'Invalid operation'});

}
;

const follow = await makeQuery(query, client)
return response.status(200).json({data: follow});
Expand Down
111 changes: 104 additions & 7 deletions website/src/pages/media/components/cards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import {useEffect, useRef, useState} from "react";
import {useRouter} from "next/router";
import {makeCachedRequest, makeRequestWithToken} from "@/lib/api_tools";
import {ADMIN_USER_TYPE, EDITOR_USER_TYPE, MEMBER_USER_TYPE} from "@/lib/users";
import stlyes from "@/styles/media/main.module.css";
import stlyes from "@/styles/media/cards.module.css";
import {getFilePath} from "@/lib/data";
import Image from "next/image";
import {loader_data} from "@/lib/loader_data";
import {getNamesInPreference, macronCodeToChar, numberDictionary} from "@/lib/plant_data";
import {fetchData} from "next-auth/client/_utils";

interface PostCardProps {
post_title: string,
Expand All @@ -32,7 +33,6 @@ export function PostCard(props: PostCardProps) {
const dataFetch = useRef(false);

useEffect(() => {

// Get the time
const date = new Date(props.post_date);

Expand Down Expand Up @@ -67,7 +67,7 @@ export function PostCard(props: PostCardProps) {
useEffect(() => {

// Get the width of the bottom bar
const bottom = document.getElementById("bottom");
const bottom = document.getElementById("widthReference");
if(bottom == null) return;
setWidth(bottom.offsetWidth);

Expand All @@ -82,10 +82,15 @@ export function PostCard(props: PostCardProps) {
const fetchData = async () => {

// Get the user information
const user = await makeRequestWithToken("get", `/api/user/data?id=${props.post_user_id}`);
setUsername(user.data.data.user_name);
setUserImage(user.data.data.user_image);
switch (user.data.data.user_type) {
const data = await makeRequestWithToken("get", `/api/user/data?id=${props.post_user_id}`);
const user = data.data.data;
if(user.user_image && user.user_image != "undefined"){
setUserImage(user.user_image);
} else {
setUserImage("/media/images/logo.svg");
}
setUsername(user.user_name);
switch (user.user_type) {
case ADMIN_USER_TYPE:
setUserType("Admin");
break;
Expand Down Expand Up @@ -158,4 +163,96 @@ export function PostCard(props: PostCardProps) {
</div>
</>
)
}

interface PostCardApiProps {
id: number
}

export function PostCardApi(props: PostCardApiProps){

const [data, setData] = useState<any>()

useEffect(() => {
fetchData();
}, []);

const fetchData = async () => {
const response = await makeRequestWithToken("get", `/api/posts/fetch?id=${props.id}&operation=data`);
setData(response.data.data[0]);
}

return(<>
{data != null ? <PostCard
post_title={data.post_title}
post_image={data.post_image}
post_user_id={data.post_user_id}
post_plant_id={data.post_plant_id}
post_date={data.post_date}
id={data.id}
/> : <></>}
</>)
}


interface UserCardProps {
id: number
}
export function UserCard(props: UserCardProps) {

const [username, setUsername] = useState("Loading..")
const [userImage, setUserImage] = useState("/media/images/small_loading.gif")
const [userType, setUserType] = useState("")
const [followers, setFollowers] = useState(0)
const dataFetch = useRef(false);

useEffect(() => {
if(dataFetch.current) return;
dataFetch.current = true;
fetchData();
}, []);

const fetchData = async () => {

// Get the user information
const data = await makeRequestWithToken("get", `/api/user/data?id=${props.id}`);
const user = data.data.data;
if(user.user_image && user.user_image != "undefined"){
setUserImage(user.user_image);
} else {
setUserImage("/media/images/logo.svg");
}
setUsername(user.user_name);
switch (user.user_type) {
case ADMIN_USER_TYPE:
setUserType("Admin");
break;
case MEMBER_USER_TYPE:
setUserType("Member");
break;
case EDITOR_USER_TYPE:
setUserType("Editor");
break;
default:
setUserType("Unknown");
}

// Get the followers
const followers = await makeRequestWithToken("get", `/api/user/follow?operation=followingCount&id=${props.id}`);
setFollowers(followers.data.data[0]["COUNT(*)"]);
}

return(
<>
<div className={stlyes.user}>
<img src={userImage} alt="Profile"/>
<div className={stlyes.userInfo}>
<h1>{username}</h1>
<h2>{userType}</h2>
</div>
<h3>{followers}</h3>
</div>
</>
)

}
2 changes: 1 addition & 1 deletion website/src/pages/media/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export default function Home(){
</div>

{/* Bottom Bar */}
<div className={stlyes.bottomBar} id={"bottom"}>
<div className={stlyes.bottomBar} id={"widthReference"}>
<Link href={"/media"}>
<img src="/media/images/Home.svg" alt="Home"/>
</Link>
Expand Down
30 changes: 16 additions & 14 deletions website/src/pages/media/search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Wrapper from "@/pages/media/components/wrapper";
import stlyes from '@/styles/media/search.module.css'
import {useEffect, useState} from "react";
import {makeRequestWithToken} from "@/lib/api_tools";
import {PostCard} from "@/pages/media/components/cards";
import {PostCard, PostCardApi, UserCard} from "@/pages/media/components/cards";
import Link from "next/link";

export default function Page(){
Expand Down Expand Up @@ -38,11 +38,13 @@ export default function Page(){
setHasSearched(true);
setLoading(false);

// Save to search history if it is not already there
if(searchHistory.indexOf(searchQuery) === -1) {
setSearchHistory([...searchHistory, searchQuery]);
// Update the search history
if(searchQuery && !searchHistory.includes(searchQuery)) {
let history = [...searchHistory];
history.push(searchQuery);
setSearchHistory(history);
localStorage.setItem("searchHistory", JSON.stringify(history));
}
localStorage.setItem("searchHistory", JSON.stringify(searchHistory));
}

const loadingDisplay = () => {
Expand All @@ -61,27 +63,27 @@ export default function Page(){
<p>Users</p>
{searchResults.users.map((user: any, index: number) => {
return (
<button key={index} className={stlyes.searchResult}>
<p>{user.id}</p>
</button>
<Link href={"/media/profile?id=" + user.id} key={index} className={stlyes.searchResult}>
<UserCard id={user.id}/>
</Link>
)
})}

<p>Plants</p>
{searchResults.plants.map((plant: any, index: number) => {
return (
<button key={index} className={stlyes.searchResult}>
<Link href={"/plants/" + plant.id} key={index} className={stlyes.searchResult}>
<p>{plant.id}</p>
</button>
</Link>
)
})}

<p>Posts</p>
{searchResults.posts.map((post: any, index: number) => {
return (
<button key={index} className={stlyes.searchResult}>
<p>{post.id}</p>
</button>
<Link href={"/media/posts/" + post.id} key={index} className={stlyes.searchResult}>
<PostCardApi id={post.id}/>
</Link>
)
})}

Expand Down Expand Up @@ -114,7 +116,7 @@ export default function Page(){
<div className={stlyes.page}>
<div className={stlyes.topBar}>
<Link href={"/media"}><img src={"/media/images/Back.svg"}/></Link>
<div className={stlyes.searchBar}>
<div className={stlyes.searchBar} id={"widthReference"}>
<input type="text" placeholder={"Enter your search..."}/>
<button onClick={() => {
search(undefined)
Expand Down
Loading

0 comments on commit 9d5cdd3

Please sign in to comment.