Skip to content

Commit

Permalink
feat: search done
Browse files Browse the repository at this point in the history
  • Loading branch information
josetano2 committed Aug 23, 2024
1 parent f18cbcf commit f7feafe
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 32 deletions.
30 changes: 28 additions & 2 deletions src/ss_backend/main.mo
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,33 @@ actor {
return #ok(Vector.toArray(tempPosts));
};

func containsInsensitive(text : Text, pattern : Text) : Bool {
let lowerText = Text.toLowercase(text);
let lowerPattern = Text.toLowercase(pattern);
return Text.contains(lowerText, #text lowerPattern);
};

public func searchPost(searchQuery : Text) : async Result.Result<[Post], Text> {

var allSearchPosts = Vector.Vector<Post>();

if (searchQuery == "") {
for (post in posts.vals()) {
allSearchPosts.add(post);
};
return #ok(Vector.toArray(allSearchPosts));
} else {
for (post in posts.vals()) {
let contains = containsInsensitive(post.description, searchQuery);
if (contains) {
allSearchPosts.add(post);
};
};
return #ok(Vector.toArray(allSearchPosts));
};
return #ok(Vector.toArray(allSearchPosts));
};

public query func getPostById(postId : Text) : async Result.Result<Post, Text> {
let post = posts.get(postId);
switch (post) {
Expand Down Expand Up @@ -465,8 +492,6 @@ actor {
};
};
};



public func isVoted(userId : Principal, currPost : Text) : async Bool {
let post = await getPostById(currPost);
Expand Down Expand Up @@ -583,4 +608,5 @@ actor {
};
};
};

};
38 changes: 18 additions & 20 deletions src/ss_frontend/src/components/MiddleHomePart.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React, { useEffect, useState } from 'react'
import TextInput from './TextInput'
import HomePost from './HomePost'
import { useAuth } from '../hooks/UseAuth'
import { useMutation } from '@tanstack/react-query'
import React, { useEffect, useState } from "react";
import TextInput from "./TextInput";
import HomePost from "./HomePost";
import { useAuth } from "../hooks/UseAuth";
import { useMutation } from "@tanstack/react-query";
import { ss_backend } from "../../../declarations/ss_backend";

const MiddleHomePart = ({ setCurrPost, currPost }) => {
Expand All @@ -20,11 +20,10 @@ const MiddleHomePart = ({ setCurrPost, currPost }) => {
return true;
}


async function fetchPosts() {
const fetchedPosts = await ss_backend.getAllPosts();
// console.log(fetchedPosts);

if (fetchedPosts.ok) {
setPosts(fetchedPosts.ok);
}
Expand All @@ -35,27 +34,26 @@ const MiddleHomePart = ({ setCurrPost, currPost }) => {
}, [user, posts]);

return (
<div className='w-1/2 h-full flex flex-col justify-start'>
{
isAuthenticated && <TextInput />
}
<div className=' flex flex-col gap-6'>
<div className="w-1/2 h-full flex flex-col justify-start">
{isAuthenticated && <TextInput />}
<div className=" flex flex-col gap-6">
{posts.length != 0 &&

posts.map((post) => {
return (
<HomePost choosePost={setCurrPost} post={post} refetch={dataMutate} />
<HomePost
choosePost={setCurrPost}
post={post}
refetch={dataMutate}
/>
);
})

}
})}

{/* <HomePost choosePost={setCurrPost} postId={"dummy"} />
<HomePost choosePost={setCurrPost} postId={"dummy"} /> */}
</div>
</div>
)
}
);
};

export default MiddleHomePart
export default MiddleHomePart;
61 changes: 61 additions & 0 deletions src/ss_frontend/src/components/MiddleHomePartSearched.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import React, { useEffect, useState } from "react";
import TextInput from "./TextInput";
import HomePost from "./HomePost";
import { useAuth } from "../hooks/UseAuth";
import { useMutation } from "@tanstack/react-query";
import { ss_backend } from "../../../declarations/ss_backend";
import { useLocation } from "react-router-dom";

export default function MiddleHomePartSearched({ setCurrPost, currPost }) {
const { isAuthenticated, user } = useAuth();
const [posts, setPosts] = useState([]);
const { search } = useLocation();
const searchQuery = new URLSearchParams(search).get("query");

const { status: statusFetchingData, mutate: dataMutate } = useMutation({
mutationKey: ["checkFetch"],
mutationFn: fetchDatas,
});

async function fetchDatas() {
await fetchPosts();
// await fetchFriendHeader();
return true;
}

async function fetchPosts() {
const fetchedPosts = await ss_backend.searchPost(
searchQuery === undefined ? "" : searchQuery
);
// console.log(fetchedPosts);

if (fetchedPosts.ok) {
setPosts(fetchedPosts.ok);
}
}

useEffect(() => {
dataMutate();
}, [user, posts]);

return (
<div className="w-1/2 h-full flex flex-col justify-start">
<div className=" flex flex-col gap-6">
{posts.length != 0 &&
posts.map((post) => {
return (
<HomePost
choosePost={setCurrPost}
post={post}
refetch={dataMutate}
/>
);
})}

{/* <HomePost choosePost={setCurrPost} postId={"dummy"} />
<HomePost choosePost={setCurrPost} postId={"dummy"} /> */}
</div>
</div>
);
}
1 change: 1 addition & 0 deletions src/ss_frontend/src/components/Navbar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const Navbar = () => {
e.preventDefault();
console.log(searchKey);
navigate(`/search?query=${searchKey}`);
window.location.reload();
}
};

Expand Down
20 changes: 16 additions & 4 deletions src/ss_frontend/src/components/ProfileSidebar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Link, useNavigate } from "react-router-dom";
import { useAuth } from "../hooks/UseAuth";
import { useQuery } from "@tanstack/react-query";
import { useState } from "react";
import profilePlaceholder from "../assets/profilePlaceholder.jpg"

const ProfileSidebar = () => {
const { isAuthenticated, getUser, user } = useAuth();
Expand All @@ -14,16 +15,18 @@ const ProfileSidebar = () => {
});
const [name, setName] = useState("");
const [username, setUsername] = useState("");
const [pfp, setPfp] = useState("");
useEffect(() => {
console.log(data);

if (!isLoading && data != undefined) {
if (data.ok) {
setUsername(data.ok.username);
setName(data.ok.name)
setName(data.ok.name);
setPfp(data.ok.profileUrl);
} else {
console.log(data);

setName("Something's wrong");
}
}
Expand All @@ -33,7 +36,16 @@ const ProfileSidebar = () => {
<>
<div className=" border-gray-200 border rounded-xl flex-col flex max-w-md items-center w-[20vw]">
<div className="border-2 rounded-full border-neutral-500 mt-4">
<AiOutlineUser className="text-6xl"></AiOutlineUser>
{isAuthenticated ? (
<img
id="profileImage"
src={pfp === "" ? profilePlaceholder : pfp}
alt="Upload a file"
className="object-cover w-24 h-24 rounded-full"
/>
) : (
<AiOutlineUser className="text-6xl"></AiOutlineUser>
)}
</div>
<div className="mt-4 mb-4 pr-8 pl-8 flex flex-col items-center">
{/* {isAuthenticated} */}
Expand Down
2 changes: 0 additions & 2 deletions src/ss_frontend/src/pages/HomePage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ const HomePage = () => {
</div> : <CommentDetailPage currPost={currPost} setCurrPost={setCurrPost}/>
}



{/* <TextInput /> */}
</div>
</div>
Expand Down
34 changes: 30 additions & 4 deletions src/ss_frontend/src/pages/SearchPage.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,40 @@
import React from "react";
import React, { useState } from "react";
import { useLocation } from "react-router-dom";
import MainTemplate from "../templates/MainTemplate";
import ProfileSidebar from "../components/ProfileSidebar";
import RightHomePart from "../components/RightHomePart";
import CommentDetailPage from "../components/CommentDetailPage";
import MiddleHomePart from "../components/MiddleHomePart";
import DetailPage from "../components/DetailPage";
import MiddleHomePartSearched from "../components/MiddleHomePartSearched";

export default function SearchPage() {
const { search } = useLocation();
const searchQuery = new URLSearchParams(search).get("query");
const [currPost, setCurrPost] = useState();

return (
<MainTemplate>
<div>{searchQuery}</div>
<div className="">
<div className="flex pt-[5rem] justify-around ">
<div className="sticky self-start top-[5rem]">
<ProfileSidebar />
</div>
{currPost !== undefined ? (
<DetailPage currPost={currPost} setCurrPost={setCurrPost} />
) : (
<MiddleHomePartSearched
setCurrPost={setCurrPost}
currPost={currPost}
/>
)}
{currPost === undefined ? (
<div className="sticky self-start top-[5rem]">
<RightHomePart />
</div>
) : (
<CommentDetailPage currPost={currPost} setCurrPost={setCurrPost} />
)}
</div>
</div>
</MainTemplate>
);
}

0 comments on commit f7feafe

Please sign in to comment.