diff --git a/03_lesson/src/features/posts/postsSlice.js b/03_lesson/src/features/posts/postsSlice.js index d1e2649..e3ece40 100644 --- a/03_lesson/src/features/posts/postsSlice.js +++ b/03_lesson/src/features/posts/postsSlice.js @@ -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 = { diff --git a/04_lesson/src/features/posts/postsSlice.js b/04_lesson/src/features/posts/postsSlice.js index 7dbe13b..91293f8 100644 --- a/04_lesson/src/features/posts/postsSlice.js +++ b/04_lesson/src/features/posts/postsSlice.js @@ -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 = { diff --git a/04_lesson_starter/src/features/posts/postsSlice.js b/04_lesson_starter/src/features/posts/postsSlice.js index d1e2649..e3ece40 100644 --- a/04_lesson_starter/src/features/posts/postsSlice.js +++ b/04_lesson_starter/src/features/posts/postsSlice.js @@ -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 = { diff --git a/05_lesson/src/features/posts/postsSlice.js b/05_lesson/src/features/posts/postsSlice.js index 14c4084..a72f6b3 100644 --- a/05_lesson/src/features/posts/postsSlice.js +++ b/05_lesson/src/features/posts/postsSlice.js @@ -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 = { diff --git a/05_lesson_starter/src/features/posts/postsSlice.js b/05_lesson_starter/src/features/posts/postsSlice.js index 354f55c..91293f8 100644 --- a/05_lesson_starter/src/features/posts/postsSlice.js +++ b/05_lesson_starter/src/features/posts/postsSlice.js @@ -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 = { diff --git a/07_lesson/data/db.json b/07_lesson/data/db.json index 2941257..d9274b7 100644 --- a/07_lesson/data/db.json +++ b/07_lesson/data/db.json @@ -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 } }, { diff --git a/07_lesson_starter/src/features/posts/postsSlice.js b/07_lesson_starter/src/features/posts/postsSlice.js index 14c4084..a72f6b3 100644 --- a/07_lesson_starter/src/features/posts/postsSlice.js +++ b/07_lesson_starter/src/features/posts/postsSlice.js @@ -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 = {