-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
473ec32
commit dae5c66
Showing
5 changed files
with
79 additions
and
127 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
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
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,58 @@ | ||
import { createSlice } from "@reduxjs/toolkit"; | ||
|
||
const initialState = { | ||
hotposts: [], | ||
}; | ||
|
||
export const RoomPostState = createSlice({ | ||
name: "roomPosts", | ||
initialState, | ||
reducers: { | ||
setRoomPost: (state, action) => { | ||
Array.from(action.payload).map(item=>{ | ||
let elm= state.hotposts.find(it=> it.id==item.id ) | ||
if(!elm) | ||
state.hotposts.push(item); | ||
}) | ||
}, | ||
setRoomPostComment: (state, action) => { | ||
let post = state.hotposts.find((post) => post.id == action.payload.postId); | ||
|
||
if(post){ | ||
post.comments = [action.payload, ...post.comments]; | ||
} | ||
|
||
}, | ||
toggleUserRoomUpvote: (state, action) => { | ||
let post = state.hotposts.find((post) => post.id == action.payload.postId); | ||
let upvotes; | ||
if (post) { | ||
upvotes = post.upvotes; | ||
|
||
let index = upvotes.findIndex( | ||
(vote) => vote.userId == action.payload.userId | ||
); | ||
|
||
if (index != -1) { | ||
upvotes[index] = action.payload; | ||
|
||
} else { | ||
|
||
upvotes[upvotes.length] = action.payload; | ||
|
||
} | ||
} | ||
}, | ||
deleteRoomPostsInfo:(state,action)=>{ | ||
let posts = state.hotposts.filter((post)=> post.id != action.payload); | ||
state.hotposts = [...posts]; | ||
}, | ||
clearRoomPostsInfo: (state) => { | ||
state.hotposts = []; | ||
}, | ||
}, | ||
}); | ||
|
||
export const { setRoomPost, setRoomPostComment, toggleUserRoomUpvote, clearRoomPostsInfo, deleteRoomPostsInfo } = RoomPostState.actions; | ||
|
||
export default RoomPostState.reducer; |
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
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