Skip to content

Commit

Permalink
fixed search queries
Browse files Browse the repository at this point in the history
  • Loading branch information
ahnafnafee committed Mar 7, 2022
1 parent 17f729c commit deff3e2
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 46 deletions.
38 changes: 21 additions & 17 deletions components/BookDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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,
});
};

Expand Down Expand Up @@ -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);
}
};

Expand All @@ -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);
}
};

Expand All @@ -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);
}
};

Expand All @@ -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);
}
};

Expand All @@ -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);
}
};

Expand Down
15 changes: 2 additions & 13 deletions pages/library.js
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -48,10 +39,6 @@ function Library() {
}
}, [user]);

// React.useEffect(() => {
// if (bookData) setData(bookData.items);
// }, [bookData]);

return (
<div className="flex h-full w-screen">
<NextSeo
Expand Down Expand Up @@ -90,6 +77,7 @@ function Library() {
data.length > 0 &&
data.map(
({
id,
user_id,
g_id,
title,
Expand All @@ -99,6 +87,7 @@ function Library() {
rating,
}) => {
const volumeInfo = {
id,
user_id,
g_id,
title,
Expand Down
51 changes: 48 additions & 3 deletions pages/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -36,14 +38,49 @@ 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`,
fetcher
);

React.useEffect(() => {
if (bookData) setData(bookData.results.books);
if (bookData) {
setData(bookData.results.books);
}
}, [bookData]);

const toggleFocus = () => {
Expand All @@ -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);
})
);
Expand All @@ -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}`);
})
);
Expand Down
12 changes: 3 additions & 9 deletions pages/wishlist.js
Original file line number Diff line number Diff line change
@@ -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();

Expand Down Expand Up @@ -69,6 +61,7 @@ function Wishlist() {
{data &&
data.map(
({
id,
user_id,
g_id,
title,
Expand All @@ -78,6 +71,7 @@ function Wishlist() {
rating,
}) => {
const volumeInfo = {
id,
user_id,
g_id,
title,
Expand Down
4 changes: 0 additions & 4 deletions utils/listener.js

This file was deleted.

1 comment on commit deff3e2

@vercel
Copy link

@vercel vercel bot commented on deff3e2 Mar 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checks for Deployment have failed

Please sign in to comment.