Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: google api error not hinder book uploading #174

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 28 additions & 19 deletions backend/routes/api/books.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,35 +145,44 @@ router.post(
validateRequest,
auth,
async (req: Request, res: Response) => {
const response = await axios.get(
`https://www.googleapis.com/books/v1/volumes?q=${encodeURIComponent(
req.body.name
)}`
);
let responseData;

const categories = new Set(
response.data.items
.slice(0, 10)
.filter((item: Item) => item.volumeInfo.categories)
.flatMap((item: Item) => item.volumeInfo.categories)
.map((category: string) => category.toLowerCase())
);

const convertedCategories = Array.from(categories);

const { description, imageLinks } = response.data.items[0].volumeInfo;
try {
const response = await axios.get(
`https://www.googleapis.com/books/v1/volumes?q=${encodeURIComponent(
req.body.name
)}`
);
responseData = await response?.data?.items;
} catch (error) {
throw new Error('Could not find any categories');
}

const books = new Books({
name: req.body.name,
url: req.body.url,
size: req.body.size,
date: new Date(),
uploader: req.body.uploader,
category: convertedCategories,
description,
imageLinks,
});

if (responseData) {
const categories: Set<string> = new Set(
responseData
.slice(0, 10)
.filter((item: Item) => item.volumeInfo.categories)
.flatMap((item: Item) => item.volumeInfo.categories)
.map((category: string) => category.toLowerCase())
);

const convertedCategories = Array.from(categories);

const { description, imageLinks } = responseData[0].volumeInfo;
books.category = convertedCategories;
books.description = description;
books.imageLinks = imageLinks;
}

await books.save();

res.status(201).json(books);
Expand Down
20 changes: 11 additions & 9 deletions client/src/pages/RecentlyAdded.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@ const RecentlyAdded = () => {
const { user, isLoggedIn } = useSelector(
(state: RootState) => state.authSlice
);
const [deleteBook, { isSuccess, isError }] = useDeleteBookMutation();
const [deleteBook] = useDeleteBookMutation();

const isAdmin = isLoggedIn && user.user.isAdmin;
const handleDelete = async ({ id }: { id: string }) => {
deleteBook({ id }).catch((err) => {
isError && toast.error('Something went wrong');
});

isSuccess && toast.success('Book deleted successfully');
try {
await deleteBook({ id }).unwrap();
toast.success('Book deleted successfully');
} catch (err) {
toast.error('Something went wrong');
}
};

if (isLoading || !recentlyAddedBooks) {
Expand Down Expand Up @@ -124,11 +125,12 @@ const RecentlyAdded = () => {
{
<DropdownMenuItem
disabled={!isAdmin}
onClick={() =>
onClick={(e) => {
e.stopPropagation();
handleDelete({
id: book._id,
})
}
});
}}
>
<AiOutlineDelete className="h-4 w-4 mr-2 text-red-500" />
<span className="cursor-pointer text-red-500">
Expand Down
11 changes: 9 additions & 2 deletions client/src/redux/services/book.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ export const bookApi = commonApi.injectEndpoints({
id,
},
}),
invalidatesTags: [{ type: 'Book', id: 'List' }],
invalidatesTags: (result, error, { id }) => [
{ type: 'Book', id: 'List' },
{ type: 'Book', id: 'RecentlyAdded' },
{ type: 'Book', id },
],
}),

searchBooks: build.query<
Expand Down Expand Up @@ -55,7 +59,10 @@ export const bookApi = commonApi.injectEndpoints({
method: 'POST',
body: book,
}),
invalidatesTags: [{ type: 'Book', id: 'List' }],
invalidatesTags: [
{ type: 'Book', id: 'List' },
{ type: 'Book', id: 'RecentlyAdded' },
],
}),

updateBook: build.mutation<
Expand Down