Skip to content

Commit 0726537

Browse files
committed
separated incremeting/decrementing number of replies reducer and adding/removing a repost, and currently working on expandedpost replies
1 parent 918b15c commit 0726537

File tree

5 files changed

+52
-42
lines changed

5 files changed

+52
-42
lines changed

src/components/Posts/ExpandedPostReplies.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const ExpandedPostReplies = ({ postId }: ExpandedPostRepliesProps) => {
3636
return (
3737
<>
3838
{posts
39-
.filter((o) => o.parentPostId === postId)
39+
.filter((o) => o.parentPostId && !o.originalPostContent)
4040
.map((o) => (
4141
<PostItem key={o.postId} post={o} />
4242
))}

src/components/Posts/PostItem.tsx

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
setExpandedPost,
2929
addRepost,
3030
undoRepost,
31+
toggleRepost,
3132
} from "../../state/slices/postsSlice";
3233
import { enqueueToast } from "../../state/slices/toastSlice";
3334
import { Post } from "../../types/posts";
@@ -86,13 +87,9 @@ const PostItem = ({ post }: PostProps) => {
8687
const [openRepostMenu, setOpenRepostMenu] = useState(false);
8788
const repostMenuRef = useRef<HTMLButtonElement>(null);
8889
const { sendRequest } = useAxios();
89-
const originalPostId = post.parentPostId ?? post.postId;
90-
const userRepost = useAppSelector((state) =>
91-
state.posts.posts.find(
92-
(post) =>
93-
post.parentPostId === originalPostId && post.userId === user.userId,
94-
),
95-
);
90+
const originalPostId = post.originalPostContent
91+
? post.parentPostId
92+
: post.postId;
9693

9794
const routeChange = () => {
9895
navigate(`/post/${originalPostId}`);
@@ -119,23 +116,30 @@ const PostItem = ({ post }: PostProps) => {
119116
* reposts and the original post to update their state.
120117
*/
121118
const handleRepost = async () => {
122-
const repostId = userRepost ? userRepost.postId : originalPostId;
119+
if (!originalPostId) return;
123120

124121
const newPost = await toggleRepostPostRequest(
125122
sendRequest,
126123
post.isRepostedByCurrentUser,
127-
repostId,
124+
originalPostId,
128125
user.userId,
129126
);
130127

128+
dispatch(toggleRepost(originalPostId));
129+
131130
if (post.isRepostedByCurrentUser) {
132-
dispatch(undoRepost({ repostId, userId: user.userId }));
131+
dispatch(
132+
undoRepost({
133+
postId: originalPostId,
134+
userId: user.userId,
135+
}),
136+
);
133137
} else {
134138
dispatch(
135139
addRepost({
136140
...post,
137141
...newPost,
138-
repostedByDisplayName: user.displayName, // passing here since we cannot access the user state in post slice
142+
repostedByDisplayName: user.displayName, // passing here since we cannot access the user state in post
139143
}),
140144
);
141145
}

src/state/slices/postsSlice.ts

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -92,58 +92,53 @@ const postsSlice = createSlice({
9292
},
9393
textContent: "",
9494
isRepostedByCurrentUser: true,
95+
numberOfReposts: newPost.numberOfReposts + 1,
9596
});
96-
97-
const updatedRepostsWithPosts = state.posts.map((post) => {
98-
if ((post.parentPostId || post.postId) === newPost.parentPostId) {
99-
return {
100-
...post,
101-
numberOfReposts: post.numberOfReposts + 1,
102-
isRepostedByCurrentUser: true,
103-
};
104-
}
105-
106-
return post;
107-
});
108-
109-
state.posts = updatedRepostsWithPosts;
11097
},
11198
undoRepost: (
11299
state,
113-
action: PayloadAction<{ repostId: number; userId?: number }>,
100+
action: PayloadAction<{ postId: number; userId?: number }>,
114101
) => {
115-
const { repostId, userId } = action.payload;
116-
const givenPost = state.posts.find((post) => post.postId === repostId);
117-
if (!givenPost) return;
102+
const { postId, userId } = action.payload;
103+
if (!postId || !userId) return;
118104

119-
const originalPostId = givenPost.parentPostId ?? givenPost.postId; // find original post id
120105
const userRepostIndex = state.posts.findIndex(
121-
// find userRepostIndex to filter out
122-
(post) =>
123-
post.parentPostId === originalPostId && post.userId === userId,
106+
(post) => post.parentPostId === postId && post.userId === userId,
124107
);
125108

126-
if (userRepostIndex !== -1) {
109+
if (postId && userRepostIndex !== -1) {
127110
state.posts.splice(userRepostIndex, 1); // filter out repost belonging to current user
128111
}
112+
},
113+
toggleRepost: (state, action: PayloadAction<number>) => {
114+
const postToUpdate = state.posts.find(
115+
(post) => post.postId === action.payload,
116+
);
117+
if (!postToUpdate) return;
118+
119+
const originalPostId = postToUpdate.originalPostContent
120+
? postToUpdate.parentPostId
121+
: postToUpdate.postId;
129122

130-
const updatedRepostsWithPosts = state.posts.map((post) => {
131-
// update all existing reposts and original post with numberOfReposts and updating isRepostedByCurrentUser flag.
123+
const newPosts = state.posts.map((post) => {
132124
if (
133125
post.postId === originalPostId ||
134126
post.parentPostId === originalPostId
135127
) {
128+
const isRepostedByCurrentUser = !post.isRepostedByCurrentUser;
129+
136130
return {
137131
...post,
138-
numberOfReposts: post.numberOfReposts - 1,
139-
isRepostedByCurrentUser: false,
132+
isRepostedByCurrentUser,
133+
numberOfReposts: isRepostedByCurrentUser
134+
? post.numberOfReposts + 1
135+
: post.numberOfReposts - 1,
140136
};
141137
}
142-
143138
return post;
144139
});
145140

146-
state.posts = updatedRepostsWithPosts;
141+
state.posts = newPosts;
147142
},
148143
toggleLikePost: (state, action: PayloadAction<number>) => {
149144
const postToUpdate = state.posts.find((p) => p.postId === action.payload);
@@ -223,6 +218,7 @@ export const {
223218
setPosts,
224219
setExpandedPost,
225220
addRepost,
221+
toggleRepost,
226222
toggleLikePost,
227223
toggleFollow,
228224
updateDisplayNames,

src/utilities/postTransform.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
import { Post } from "../types/posts";
22

3+
/**
4+
* Repost Item data that needs to be transformed to how a Regular Post Item is being displayed.
5+
* I needed this because when we return a post object from the database,
6+
* we cannot simply populate the Post object like a regular post.
7+
*
8+
* It is important to take the original content object being returned
9+
* and transforming this data to look like a regular post item so that the
10+
* post item component can read it like normal without needing to place conditionals
11+
* depending on whether it is a repost or a regular post.
12+
*/
313
export const convertRepostToPost = (post: Post): Post => {
414
if (!post.originalPostContent) return post;
515

src/utilities/postUtilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const toggleRepostPostRequest = async (
3636
await sendRequest(
3737
{
3838
method: "PUT",
39-
data: { postId },
39+
data: { postId, userId },
4040
},
4141
"posts/undoRepost",
4242
);

0 commit comments

Comments
 (0)