Skip to content

Commit

Permalink
feat: add a check that the increase of views is only if you are authe…
Browse files Browse the repository at this point in the history
…nticated
  • Loading branch information
Franqsanz committed Aug 16, 2024
1 parent 49c0c04 commit 367deb4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/hooks/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,10 @@ function useMoreBooksAuthors(id: string | undefined) {
});
}

function useBook(pathUrl: string | undefined) {
function useBook(pathUrl: string | undefined, token?: string | null) {
return useSuspenseQuery({
queryKey: [keys.one, pathUrl],
queryFn: () => getBook(pathUrl),
queryFn: () => getBook(pathUrl, token),
refetchOnWindowFocus: false,
gcTime: 3000,
retry: 1,
Expand Down
3 changes: 2 additions & 1 deletion src/pages/Book.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const MoreBooks = lazy(() => import('@components/cards/MoreBooks'));

export default function Book() {
const shareUrl = window.location.href;
const getToken = window.localStorage.getItem('app_tk');
const { pathUrl } = useParams();
const { currentUser } = useAuth();
const grayColor = useColorModeValue('gray.200', 'gray.600');
Expand Down Expand Up @@ -80,7 +81,7 @@ export default function Book() {
let uiLink;
let btnMoreOptions;

const { data } = useBook(pathUrl);
const { data } = useBook(pathUrl, getToken);
const { mutate, isSuccess, isPending } = useDeleteBook();

const isCurrentUserAuthor = currentUser && currentUser.uid === data.userId;
Expand Down
14 changes: 12 additions & 2 deletions src/services/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,18 @@ async function getBooksPaginate(page: number | undefined) {
return await fetchData(`${API_URL}/books?limit=10&page=${page}`);
}

async function getBook(pathUrl: string | undefined) {
return await fetchData(`${API_URL}/books/path/${pathUrl}`);
async function getBook(pathUrl: string | undefined, token?: string | null) {
const headers = new Headers();
headers.append('content-type', 'application/json');

if (token) {
headers.append('Authorization', `Bearer ${token}`);
}

return await fetchData(`${API_URL}/books/path/${pathUrl}`, {
method: 'GET',
headers,
});
}

async function getBooksFilterPaginated(
Expand Down

0 comments on commit 367deb4

Please sign in to comment.