Added fix for jsonplaceholder API not returning accurate new post IDs after 101

02_lesson
gitdagray 2022-08-12 11:43:35 -05:00
parent 81ce86f092
commit ef530de8e3
7 changed files with 75 additions and 4 deletions

View File

@ -84,6 +84,18 @@ const postsSlice = createSlice({
state.error = action.error.message
})
.addCase(addNewPost.fulfilled, (state, action) => {
// Fix for API post IDs:
// Creating sortedPosts & assigning the id
// would be not be needed if the fake API
// returned accurate new post IDs
const sortedPosts = state.posts.sort((a, b) => {
if (a.id > b.id) return 1
if (a.id < b.id) return -1
return 0
})
action.payload.id = sortedPosts[sortedPosts.length - 1].id + 1;
// End fix for fake API post IDs
action.payload.userId = Number(action.payload.userId)
action.payload.date = new Date().toISOString();
action.payload.reactions = {

View File

@ -106,7 +106,18 @@ const postsSlice = createSlice({
state.error = action.error.message
})
.addCase(addNewPost.fulfilled, (state, action) => {
action.payload.id = state.posts[state.posts.length - 1].id + 1;
// Fix for API post IDs:
// Creating sortedPosts & assigning the id
// would be not be needed if the fake API
// returned accurate new post IDs
const sortedPosts = state.posts.sort((a, b) => {
if (a.id > b.id) return 1
if (a.id < b.id) return -1
return 0
})
action.payload.id = sortedPosts[sortedPosts.length - 1].id + 1;
// End fix for fake API post IDs
action.payload.userId = Number(action.payload.userId)
action.payload.date = new Date().toISOString();
action.payload.reactions = {

View File

@ -84,6 +84,18 @@ const postsSlice = createSlice({
state.error = action.error.message
})
.addCase(addNewPost.fulfilled, (state, action) => {
// Fix for API post IDs:
// Creating sortedPosts & assigning the id
// would be not be needed if the fake API
// returned accurate new post IDs
const sortedPosts = state.posts.sort((a, b) => {
if (a.id > b.id) return 1
if (a.id < b.id) return -1
return 0
})
action.payload.id = sortedPosts[sortedPosts.length - 1].id + 1;
// End fix for fake API post IDs
action.payload.userId = Number(action.payload.userId)
action.payload.date = new Date().toISOString();
action.payload.reactions = {

View File

@ -95,6 +95,18 @@ const postsSlice = createSlice({
state.error = action.error.message
})
.addCase(addNewPost.fulfilled, (state, action) => {
// Fix for API post IDs:
// Creating sortedPosts & assigning the id
// would be not be needed if the fake API
// returned accurate new post IDs
const sortedPosts = state.posts.sort((a, b) => {
if (a.id > b.id) return 1
if (a.id < b.id) return -1
return 0
})
action.payload.id = sortedPosts[sortedPosts.length - 1].id + 1;
// End fix for fake API post IDs
action.payload.userId = Number(action.payload.userId)
action.payload.date = new Date().toISOString();
action.payload.reactions = {

View File

@ -106,6 +106,18 @@ const postsSlice = createSlice({
state.error = action.error.message
})
.addCase(addNewPost.fulfilled, (state, action) => {
// Fix for API post IDs:
// Creating sortedPosts & assigning the id
// would be not be needed if the fake API
// returned accurate new post IDs
const sortedPosts = state.posts.sort((a, b) => {
if (a.id > b.id) return 1
if (a.id < b.id) return -1
return 0
})
action.payload.id = sortedPosts[sortedPosts.length - 1].id + 1;
// End fix for fake API post IDs
action.payload.userId = Number(action.payload.userId)
action.payload.date = new Date().toISOString();
action.payload.reactions = {

View File

@ -242,9 +242,9 @@
{
"id": 3,
"title": "ea molestias quasi exercitationem repellat qui ipsa sit aut",
"body": "hey again",
"body": "hey there!",
"userId": 1,
"date": "2022-04-13T23:05:18.040Z"
"date": "2022-05-02T20:41:05.437Z"
},
{
"id": 5,
@ -291,7 +291,7 @@
"wow": 0,
"heart": 0,
"rocket": 0,
"coffee": 5
"coffee": 6
}
},
{

View File

@ -95,6 +95,18 @@ const postsSlice = createSlice({
state.error = action.error.message
})
.addCase(addNewPost.fulfilled, (state, action) => {
// Fix for API post IDs:
// Creating sortedPosts & assigning the id
// would be not be needed if the fake API
// returned accurate new post IDs
const sortedPosts = state.posts.sort((a, b) => {
if (a.id > b.id) return 1
if (a.id < b.id) return -1
return 0
})
action.payload.id = sortedPosts[sortedPosts.length - 1].id + 1;
// End fix for fake API post IDs
action.payload.userId = Number(action.payload.userId)
action.payload.date = new Date().toISOString();
action.payload.reactions = {