Skip to content

Commit

Permalink
Merge pull request #174 from sayinmehmet47/feat/book-embed-preview
Browse files Browse the repository at this point in the history
fix: google api error not hinder book uploading
  • Loading branch information
sayinmehmet47 authored Feb 28, 2024
2 parents a1fd66f + 3ad2f9b commit fea3ff0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 30 deletions.
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

0 comments on commit fea3ff0

Please sign in to comment.