-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial version of blog posts fetching
- implement folder structure for queries and api - use axios instead of fetch
- Loading branch information
Showing
6 changed files
with
95 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Sorry, something went wrong. |
||
export default axiosInstance; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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