-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: mock data api #8
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
Great work on this pull request! It's evident that a lot of thought and effort went into creating the I noticed that you've implemented In most cases, having both Overall, this pull request is a great step towards improving data retrieval and centralization in the app. Keep up the excellent work! |
About the Pull Request
This pull request introduces a vital API endpoint,
api/posts
, aimed at streamlining API calls to both JSONPlaceholder and Picsum Images. This endpoint efficiently retrieves post data and images, centralizing them in the frontend and managing the data using the Redux Toolkit state manager. It employs thecreateAsyncThunk
function to handle API requests, ensuring seamless data display within the application's layout.Details
Objective
The primary goal is to harness the capabilities of the JSONPlaceholder and Picsum APIs for fetching posts and images, respectively. This implementation leverages API endpoint technologies and manages information through Redux Toolkit.
Changes Made
api/posts
andapi/images
endpoints, we combined the logic into a unifiedapi/posts
endpoint, streamlining the handling of both post and image data. This endpoint consistently returns posts along with associated images.next/font
package has been removed, as Next.js 13 now includes it by default.next.config
document to define the host for Picsum images. This enables the use of theImage
component provided by Next.js.types/
folder was created to house types for the posts and images objects.Redux Configuration
postsSlice
was created with an initial state containing properties forisLoading
,posts
, anderror
. Theposts
array stores individual posts.addNewPost
andaddNewPosts
were implemented to add a single post and an array of posts, respectively.fetchPosts
function was established usingcreateAsyncThunk
to handle requests to theapi/posts
endpoint, returning an array of posts upon success or marking the error state property as true.fetchPosts
function requires alastPostId
parameter to function correctly, as it uses this parameter to track the last fetched post and determine the remaining posts to retrieve.All of these features have been implemented and are demonstrated in the application's layout. The pull request also includes essential dependencies, configuration changes for hosting Picsum images, and notes on potential performance impacts. Screenshots and a video demonstration have been included to illustrate how the changes function in practice.
API Endpoint:
/api/posts
About this endpoint
The
/api/posts
endpoint is a core feature of this application, designed to efficiently retrieve post data from external sources and centralize it for use in the frontend. This section provides an in-depth understanding of the endpoint, its functionality, usage, and response.Purpose
The primary purpose of the
/api/posts
endpoint is to streamline the retrieval of post data while adhering to best practices. It simplifies data access by fetching posts from the JSONPlaceholder API and managing this information seamlessly in the app.Functionality
The
/api/posts
endpoint functions as follows:Usage
To use the
/api/posts
endpoint, make a POST request with the following structure, as shown in this example using thefetch
method:Ensure that the
method
is set to 'POST' and provide the request header with'Content-Type'
set to'application/json'
. The body of the request should include thelastPostId
parameter, which is crucial for tracking the last fetched post.Response
When you make a POST request to
/api/posts
, the endpoint will return a response containing an array of posts. This array represents the retrieved posts and associated images, facilitating further usage and display within the application.Dependencies:
Configuration Changes:
next.config
document.Impact:
Performance Impact:
It's worth noting that there might be room for improving the image API provider or the loading process, as the loading of images may currently be relatively slow.
Screenshots:
See how the changes work:
create-mock-data-api-test-video