From deff3e25b09ea8a0e7764323ad05532298aee64d Mon Sep 17 00:00:00 2001 From: Ahnaf An Nafee Date: Sun, 6 Mar 2022 21:33:06 -0500 Subject: [PATCH] fixed search queries --- components/BookDetails.js | 38 ++++++++++++++++------------- pages/library.js | 15 ++---------- pages/search.js | 51 ++++++++++++++++++++++++++++++++++++--- pages/wishlist.js | 12 +++------ utils/listener.js | 4 --- 5 files changed, 74 insertions(+), 46 deletions(-) delete mode 100644 utils/listener.js diff --git a/components/BookDetails.js b/components/BookDetails.js index 92f5a2e..3d87a15 100644 --- a/components/BookDetails.js +++ b/components/BookDetails.js @@ -12,6 +12,7 @@ import { import { supabaseClient } from "../lib/client"; import { useRouter } from "next/router"; import { useEffect, useState } from "react"; +import { useToast } from "@chakra-ui/react"; export function BookDetails({ id, @@ -21,6 +22,7 @@ export function BookDetails({ }) { const router = useRouter(); const user = supabaseClient.auth.user(); + const toast = useToast(); // Hacky way to remove data from list.\ // TODO: Use a reducer instead @@ -34,11 +36,12 @@ export function BookDetails({ return imageLinks.thumbnail.replace("http://", "https://"); }; - const extractISBN13 = ({ industryIdentifiers }) => { - industryIdentifiers.filter((isbn) => { - if (isbn.type === "ISBN_13") { - return isbn.identifier; - } + const toastMsg = (title, status) => { + return toast({ + title: title, + status: status ? status : "success", + duration: 1000, + isClosable: true, }); }; @@ -91,16 +94,17 @@ export function BookDetails({ if (!error) { setRemoved(true); console.log(data); + toastMsg("Moved to Library"); } else { console.log(error); + toastMsg("Failed", "error"); } }); } else { console.log(error); + toastMsg("Failed", "error"); } }); - // router.reload(window.location.pathname); - console.log("Moved to Library", id, volumeInfo); } }; @@ -113,12 +117,12 @@ export function BookDetails({ if (!error) { setRemoved(true); console.log(data); + toastMsg("Added to Wishlist"); } else { console.log(error); + toastMsg("Failed", "error"); } }); - // router.reload(window.location.pathname); - console.log("Added to Wishlist", id, volumeInfo); } }; @@ -131,12 +135,12 @@ export function BookDetails({ if (!error) { setRemoved(true); console.log(data); + toastMsg("Added to Library"); } else { console.log(error); + toastMsg("Failed", "error"); } }); - // router.reload(window.location.pathname); - console.log("Added to Library", id, volumeInfo); } }; @@ -145,17 +149,17 @@ export function BookDetails({ supabaseClient .from("book_wishlist") .delete() - .eq("g_id", id) + .eq("id", volumeInfo.id) .then(({ data, error }) => { if (!error) { setRemoved(true); console.log(data); + toastMsg("Deleted from Wishlist"); } else { console.log(error); + toastMsg("Failed", "error"); } }); - // router.reload(window.location.pathname); - console.log("Deleted from Wishlist", id, volumeInfo); } }; @@ -164,17 +168,17 @@ export function BookDetails({ supabaseClient .from("book_library") .delete() - .eq("g_id", id) + .eq("id", volumeInfo.id) .then(({ data, error }) => { if (!error) { setRemoved(true); console.log(data); + toastMsg("Deleted from Library"); } else { console.log(error); + toastMsg("Failed", "error"); } }); - // router.reload(window.location.pathname); - console.log("Deleted from Library", id, volumeInfo); } }; diff --git a/pages/library.js b/pages/library.js index 2def558..b2b6627 100644 --- a/pages/library.js +++ b/pages/library.js @@ -1,24 +1,15 @@ import * as React from "react"; -import Head from "next/head"; import { Text } from "@chakra-ui/react"; import { Avatar } from "@chakra-ui/react"; import { BookDetails } from "../components/BookDetails"; import { useRouter } from "next/router"; import { NextSeo } from "next-seo"; -import useSWR from "swr"; import { supabaseClient } from "../lib/client"; import { useEffect, useState } from "react"; -import { libStatus } from "../utils/listener"; - -const fetcher = (...args) => fetch(...args).then((res) => res.json()); function Library() { const router = useRouter(); const [data, setData] = React.useState([]); - const { data: bookData, error } = useSWR( - `https://www.googleapis.com/books/v1/volumes?q=George R R Martin&maxResults=15`, - fetcher - ); const [name, setName] = useState(""); const user = supabaseClient.auth.user(); @@ -48,10 +39,6 @@ function Library() { } }, [user]); - // React.useEffect(() => { - // if (bookData) setData(bookData.items); - // }, [bookData]); - return (
0 && data.map( ({ + id, user_id, g_id, title, @@ -99,6 +87,7 @@ function Library() { rating, }) => { const volumeInfo = { + id, user_id, g_id, title, diff --git a/pages/search.js b/pages/search.js index d3a33a5..485a39c 100644 --- a/pages/search.js +++ b/pages/search.js @@ -23,6 +23,8 @@ import { } from "@chakra-ui/react"; import useSWR from "swr"; import useWindowDimensions from "../hooks/useWindowDimensions"; +import { supabaseClient } from "../lib/client"; +import { useEffect, useState } from "react"; const fetcher = (...args) => fetch(...args).then((res) => res.json()); @@ -36,6 +38,39 @@ function Search() { const [books, setBooks] = React.useState([]); const [value, setValue] = React.useState(""); const [data, setData] = React.useState([]); + const [filterBooks, setFilterBooks] = React.useState([]); + + const user = supabaseClient.auth.user(); + + useEffect(() => { + if (user) { + supabaseClient + .from("book_library") + .select("g_id") + .eq("user_id", user.id) + .then(({ data: libData, error: libError }) => { + if (!libError) { + supabaseClient + .from("book_wishlist") + .select("g_id") + .eq("user_id", user.id) + .then(({ data: wishData, error: wishError }) => { + if (!wishError) { + var tempArr = libData.concat(wishData); + tempArr.map(function (obj) { + return obj.g_id; + }); + setFilterBooks( + tempArr.map(function (obj) { + return obj.g_id; + }) + ); + } + }); + } + }); + } + }, [user]); const { data: bookData, error } = useSWR( `https://api.nytimes.com/svc/books/v3/lists/current/hardcover-fiction.json?api-key=RtVynZwGyH7I1VnAZqYiLuxE9QnIRWv4`, @@ -43,7 +78,9 @@ function Search() { ); React.useEffect(() => { - if (bookData) setData(bookData.results.books); + if (bookData) { + setData(bookData.results.books); + } }, [bookData]); const toggleFocus = () => { @@ -59,7 +96,11 @@ function Search() { `https://www.googleapis.com/books/v1/volumes?q=${value}&maxResults=15` ).then((response) => response.json().then((data) => { - setBooks(data.items); + setBooks( + data.items.filter( + (item) => filterBooks.indexOf(item.id) === -1 + ) + ); setSearched(true); }) ); @@ -73,7 +114,11 @@ function Search() { ).then((response) => response.json().then((data) => { setSearched(true); - setBooks(data.items); + setBooks( + data.items.filter( + (item) => filterBooks.indexOf(item.id) === -1 + ) + ); setValue(`subject:${subject}`); }) ); diff --git a/pages/wishlist.js b/pages/wishlist.js index b49f13f..9c562b6 100644 --- a/pages/wishlist.js +++ b/pages/wishlist.js @@ -1,23 +1,15 @@ import * as React from "react"; -import Head from "next/head"; import { Text } from "@chakra-ui/react"; import { Avatar } from "@chakra-ui/react"; import { BookDetails } from "../components/BookDetails"; import { useRouter } from "next/router"; import { NextSeo } from "next-seo"; -import useSWR from "swr"; import { supabaseClient } from "../lib/client"; -import { useEffect, useState } from "react"; - -const fetcher = (...args) => fetch(...args).then((res) => res.json()); +import { useEffect } from "react"; function Wishlist() { const router = useRouter(); const [data, setData] = React.useState([]); - const { data: bookData, error } = useSWR( - `https://www.googleapis.com/books/v1/volumes?q=Dan Brown&maxResults=15`, - fetcher - ); const user = supabaseClient.auth.user(); @@ -69,6 +61,7 @@ function Wishlist() { {data && data.map( ({ + id, user_id, g_id, title, @@ -78,6 +71,7 @@ function Wishlist() { rating, }) => { const volumeInfo = { + id, user_id, g_id, title, diff --git a/utils/listener.js b/utils/listener.js deleted file mode 100644 index d14b544..0000000 --- a/utils/listener.js +++ /dev/null @@ -1,4 +0,0 @@ -import { useEffect, useState } from "react"; - -export const libStatus = true; -export const wishlistStatus = true;