Skip to content

Commit

Permalink
Initial version of blog posts fetching
Browse files Browse the repository at this point in the history
- implement folder structure for queries and api
- use axios instead of fetch
  • Loading branch information
petark7 committed Jul 19, 2024
1 parent 31a005b commit 08d10c9
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 9 deletions.
7 changes: 7 additions & 0 deletions api/axiosInstance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import axios from 'axios';

const axiosInstance = axios.create({
baseURL: 'https://dummyjson.com',
});

This comment has been minimized.

Copy link
@edichoska

edichoska Jul 20, 2024

Contributor

We will need an AxiosProvider. There we can create the axiosInstance, with the baseUrl(that always needs to be saved in .env file. and not hardcoded like this), and headers.
We would also check if we have the accessToken before making a request.
We can start by setting this up, and once auth is done we can add the check for the token

export default axiosInstance;
9 changes: 9 additions & 0 deletions api/endpoints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const API_BASE_URL = 'https://dummyjson.com';

const ENDPOINTS = {
BLOGS: {
GET_ALL: (limit: number, skip: number) => `${API_BASE_URL}/posts?limit=${limit}&skip=${skip}`,
},
};

export default ENDPOINTS;
17 changes: 17 additions & 0 deletions api/queries/blogs/getBlogs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { useQuery } from '@tanstack/react-query';
import fetchBlogPosts from '../../../app/action';
import QUERY_KEYS from '../../queryKeys';

interface GetBlogsParams {
pageTitle: string;
blogCardsNumber: number;
}

const useGetBlogs = ({ pageTitle, blogCardsNumber }: GetBlogsParams) => {
return useQuery({
queryKey: [...QUERY_KEYS.BLOGS.ALL, pageTitle, blogCardsNumber],
queryFn: () => fetchBlogPosts(0, pageTitle, blogCardsNumber),
});
};

export default useGetBlogs;
32 changes: 32 additions & 0 deletions api/queries/blogs/getInfiniteBlogs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { useInfiniteQuery } from '@tanstack/react-query';
import QUERY_KEYS from '../../queryKeys';
import fetchBlogPosts from '../../../app/action';
import { BlogCardProps } from '../../../components/reusable-components/blog-card/BlogCard';

interface GetInfiniteBlogsParams {
pageTitle: string;
blogCardsNumber: number;
}
const useGetInfiniteBlogs = ({ pageTitle, blogCardsNumber }: GetInfiniteBlogsParams) => {
return useInfiniteQuery<
BlogCardProps[],
Error,
BlogCardProps[],
[string, string, string, string],
number
>({
queryKey: [
QUERY_KEYS.BLOGS.INFINITE[0],
QUERY_KEYS.BLOGS.INFINITE[1],
pageTitle,
blogCardsNumber.toString(),
],
queryFn: ({ pageParam }) => fetchBlogPosts(pageParam, pageTitle, blogCardsNumber),
initialPageParam: 0,
getNextPageParam: (lastPage, allPages) => {
return lastPage.length === blogCardsNumber ? allPages.length * blogCardsNumber : undefined;
},
});
};

export default useGetInfiniteBlogs;
8 changes: 8 additions & 0 deletions api/queryKeys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const QUERY_KEYS = {
BLOGS: {
ALL: ['blogPosts'],
INFINITE: ['infiniteBlogPosts', 'infinite'] as const,
},
} as const;

export default QUERY_KEYS;
31 changes: 22 additions & 9 deletions app/action.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
'use server';

const fetchBlogPosts = async (nextPosts: number, pageTitle: string, blogCardsNumber: number) => {
try {
const response = await fetch(
`https://dummyjson.com/posts?limit=${blogCardsNumber}&skip=${nextPosts}`
);

const data = await response.json();
import axios from 'axios';
import axiosInstance from '../api/axiosInstance';
import { BlogCardProps } from '../components/reusable-components/blog-card/BlogCard';

return data?.posts;
const fetchBlogPosts = async (
nextPosts: number,
pageTitle: string,
blogCardsNumber: number
): Promise<BlogCardProps[]> => {
try {
const { data } = await axiosInstance.get('/posts', {
params: {
limit: blogCardsNumber,
skip: nextPosts,
},
});
return data.posts;
} catch (error: any) {
throw new Error(error);
if (axios.isAxiosError(error)) {
throw new Error(
error.response?.data?.message || 'An error occurred while fetching blog posts'
);
}
throw new Error('An unexpected error occurred');
}
};

Expand Down

0 comments on commit 08d10c9

Please sign in to comment.