Skip to content

Commit bc9b720

Browse files
committed
Fixed pin tweet logic. Minor fixes
1 parent b40b3e1 commit bc9b720

File tree

61 files changed

+918
-455
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+918
-455
lines changed

commons/src/main/java/com/gmail/merikbest2015/commons/constants/KafkaTopicConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public class KafkaTopicConstants {
66
public static final String MUTE_USER_TOPIC = "user-service.user.mute";
77
public static final String FOLLOW_USER_TOPIC = "user-service.user.follow";
88
public static final String FOLLOW_REQUEST_USER_TOPIC = "user-service.user.follow-request";
9+
public static final String PIN_TWEET_USER_TOPIC = "user-service.user.pin-tweet";
910
public static final String SEND_EMAIL_TOPIC = "user-service.user.send-email";
1011
public static final String UPDATE_TWEET_TOPIC = "tweet-service.tweet.update";
1112
public static final String UPDATE_LISTS_TOPIC = "lists-service.lists.update";

commons/src/main/java/com/gmail/merikbest2015/commons/dto/response/tweet/TweetAuthorResponse.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class TweetAuthorResponse {
99
private String fullName;
1010
private String username;
1111
private String avatar;
12+
private Long pinnedTweetId;
1213

1314
@JsonProperty("isPrivateProfile")
1415
private boolean isPrivateProfile;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.gmail.merikbest2015.commons.event;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
8+
@Data
9+
@Builder
10+
@AllArgsConstructor
11+
@NoArgsConstructor
12+
public class PinTweetEvent {
13+
private Long tweetId;
14+
}

commons/src/main/resources/sql-test/clear-user-db.sql

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,3 @@ delete from "user-test".public.user_muted;
44
delete from "user-test".public.user_follower_requests;
55
delete from "user-test".public.user_subscriptions;
66
delete from "user-test".public.users;
7-
delete from "user-test".public.country_codes;
8-
delete from "user-test".public.languages;

commons/src/main/resources/sql-test/populate-user-db.sql

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,3 @@ INSERT INTO "user-test".public.user_muted (user_id, muted_user_id) VALUES (2, 1)
2929

3030
-- subscribers
3131
INSERT INTO "user-test".public.subscribers (user_id, subscriber_id) VALUES (2, 1);
32-
33-
-- country_codes
34-
INSERT INTO "user-test".public.country_codes (id, country_code, phone_code, country) VALUES (1, 'AF', '+93', 'Afghanistan');
35-
INSERT INTO "user-test".public.country_codes (id, country_code, phone_code, country) VALUES (2, 'AL', '+355', 'Albania');
36-
INSERT INTO "user-test".public.country_codes (id, country_code, phone_code, country) VALUES (3, 'DZ', '+213', 'Algeria');
37-
INSERT INTO "user-test".public.country_codes (id, country_code, phone_code, country) VALUES (4, 'AS', '+1', 'American Samoa');
38-
INSERT INTO "user-test".public.country_codes (id, country_code, phone_code, country) VALUES (5, 'AD', '+376', 'Andorra');
39-
INSERT INTO "user-test".public.country_codes (id, country_code, phone_code, country) VALUES (6, 'AO', '+244', 'Angola');
40-
INSERT INTO "user-test".public.country_codes (id, country_code, phone_code, country) VALUES (7, 'AI', '+1', 'Anguilla');
41-
INSERT INTO "user-test".public.country_codes (id, country_code, phone_code, country) VALUES (8, 'AG', '+1', 'Antigua and Barbuda');
42-
INSERT INTO "user-test".public.country_codes (id, country_code, phone_code, country) VALUES (9, 'AR', '+54', 'Argentina');
43-
INSERT INTO "user-test".public.country_codes (id, country_code, phone_code, country) VALUES (10, 'AM', '+374', 'Armenia');
44-
45-
-- languages
46-
INSERT INTO "user-test".public.languages (id, language) VALUES (1, 'Arabic - العربية');
47-
INSERT INTO "user-test".public.languages (id, language) VALUES (2, 'Arabic (Feminine) - العربية (مؤنث)');
48-
INSERT INTO "user-test".public.languages (id, language) VALUES (3, 'Bangla - বাংলা');
49-
INSERT INTO "user-test".public.languages (id, language) VALUES (4, 'Basque (beta) - euskara');
50-
INSERT INTO "user-test".public.languages (id, language) VALUES (5, 'British English');
51-
INSERT INTO "user-test".public.languages (id, language) VALUES (6, 'Bulgarian - български');
52-
INSERT INTO "user-test".public.languages (id, language) VALUES (7, 'Catalan - català');
53-
INSERT INTO "user-test".public.languages (id, language) VALUES (8, 'Croatian - hrvatski');
54-
INSERT INTO "user-test".public.languages (id, language) VALUES (9, 'Czech - čeština');
55-
INSERT INTO "user-test".public.languages (id, language) VALUES (10, 'Danish - dansk');

frontend/src/components/TweetComponent/TweetActions/TweetActions.tsx

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,37 @@ import {
77
selectUserProfileId,
88
selectUserProfilePinnedTweetId
99
} from "../../../store/ducks/userProfile/selectors";
10-
import { selectUserDataId, selectUserPinnedTweetId } from "../../../store/ducks/user/selectors";
10+
import { selectUserDataId } from "../../../store/ducks/user/selectors";
1111

1212
interface TweetActionsProps {
1313
retweetsUserIds?: number[];
1414
tweetId?: number;
1515
activeTab?: number;
1616
}
1717

18-
const TweetActions: FC<TweetActionsProps> = memo(({ retweetsUserIds = [], tweetId, activeTab }): ReactElement => {
18+
const TweetActions: FC<TweetActionsProps> = memo(({ retweetsUserIds = [], tweetId, activeTab }): ReactElement | null => {
1919
const userProfileId = useSelector(selectUserProfileId);
2020
const userProfilePinnedTweetId = useSelector(selectUserProfilePinnedTweetId);
2121
const fullName = useSelector(selectUserProfileFullName);
2222
const myProfileId = useSelector(selectUserDataId);
23-
const myProfilePinnedTweetId = useSelector(selectUserPinnedTweetId);
24-
const isTweetRetweetedByUser = retweetsUserIds.findIndex((id) => id === userProfileId) !== -1;
23+
24+
const isTweetRetweetedByUser = retweetsUserIds.includes(userProfileId!);
25+
const isOwnProfile = myProfileId === userProfileId;
26+
const isPinnedTweet = userProfilePinnedTweetId === tweetId;
27+
28+
if (activeTab !== 0) {
29+
return null;
30+
}
2531

2632
return (
2733
<>
28-
{activeTab === 0 && isTweetRetweetedByUser && userProfileId ? (
34+
{(isTweetRetweetedByUser && userProfileId) && (
2935
<TweetActionResult
3036
action={TweetActionResults.RETWEET}
31-
text={((myProfileId === userProfileId) ? ("You") : (fullName)) + " Retweeted"}
37+
text={`${isOwnProfile ? "You" : fullName} Retweeted`}
3238
/>
33-
) : null}
34-
{((myProfilePinnedTweetId === tweetId || userProfilePinnedTweetId === tweetId) && activeTab === 0) && (
35-
<TweetActionResult action={TweetActionResults.PIN} text={"Pinned Tweet"} />
3639
)}
40+
{isPinnedTweet && <TweetActionResult action={TweetActionResults.PIN} text="Pinned Tweet" />}
3741
</>
3842
);
3943
});

frontend/src/components/TweetComponent/TweetActions/__tests__/TweetActions.test.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,19 @@ describe("TweetActions", () => {
1414
const wrapper = mountWithStore(
1515
<TweetActions
1616
retweetsUserIds={[1]}
17-
tweetId={mockUser.pinnedTweetId}
17+
tweetId={mockUser.pinnedTweetId!}
1818
activeTab={0}
1919
/>, mockState);
2020
expect(wrapper.find(TweetActionResult).at(0).prop("text")).toBe("Random Retweeted");
21-
expect(wrapper.find(TweetActionResult).at(1).prop("text")).toBe("Pinned Tweet");
2221
});
2322

2423
it("should render my profile retweeted", () => {
2524
const wrapper = mountWithStore(
2625
<TweetActions
2726
retweetsUserIds={[2]}
28-
tweetId={mockUser.pinnedTweetId}
27+
tweetId={mockUser.pinnedTweetId!}
2928
activeTab={0}
3029
/>, mockRootState);
3130
expect(wrapper.find(TweetActionResult).at(0).prop("text")).toBe("You Retweeted");
32-
expect(wrapper.find(TweetActionResult).at(1).prop("text")).toBe("Pinned Tweet");
3331
});
3432
});

frontend/src/pages/UserPage/UserPage.tsx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import classnames from "classnames";
99
import { useUserPageStyles } from "./UserPageStyles";
1010
import { selectUserDataId, selectUserIsLoaded } from "../../store/ducks/user/selectors";
1111
import {
12+
fetchUserPinnedTweet,
1213
fetchUserTweets,
1314
resetUserTweets,
1415
setAddedUserTweet,
@@ -80,27 +81,27 @@ const UserPage = (): ReactElement => {
8081
const isUserProfileLoading = useSelector(selectUsersIsLoading);
8182
const isUserProfileSuccessLoaded = useSelector(selectUsersIsSuccessLoaded);
8283
const isUserProfileNotLoaded = useSelector(selectUsersIsErrorLoaded);
83-
const params = useParams<{ userId: string }>();
84+
const { userId } = useParams<{ userId: string }>();
8485
const [userTweetsActiveTab, setUserTweetsActiveTab] = useState<number>(0);
8586

8687
useEffect(() => {
8788
window.scrollTo(0, 0);
8889

89-
if (params.userId) {
90-
dispatch(fetchUserProfile(parseInt(params.userId)));
91-
dispatch(fetchImages(parseInt(params.userId)));
90+
if (userId) {
91+
dispatch(fetchUserProfile(parseInt(userId)));
92+
dispatch(fetchImages(parseInt(userId)));
9293
}
9394
document.body.style.overflow = "unset";
9495

9596
stompClient = Stomp.over(() => new SockJS(WS_URL));
9697
stompClient.connect({}, () => {
97-
stompClient?.subscribe(TOPIC_USER_ADD_TWEET(params.userId), (response) => {
98+
stompClient?.subscribe(TOPIC_USER_ADD_TWEET(userId), (response) => {
9899
dispatch(setAddedUserTweet(JSON.parse(response.body)));
99100
});
100101
stompClient?.subscribe(TOPIC_USER_UPDATE_TWEET, (response) => {
101102
dispatch(setUpdatedUserTweet(JSON.parse(response.body)));
102103
});
103-
stompClient?.subscribe(TOPIC_USER_VOTE_TWEET(params.userId), (response) => {
104+
stompClient?.subscribe(TOPIC_USER_VOTE_TWEET(userId), (response) => {
104105
dispatch(setUserVote(JSON.parse(response.body)));
105106
});
106107
});
@@ -112,12 +113,13 @@ const UserPage = (): ReactElement => {
112113
dispatch(resetImagesState());
113114
stompClient?.disconnect();
114115
};
115-
}, [params.userId]);
116+
}, [userId]);
116117

117118
useEffect(() => {
118119
if (isUserProfileSuccessLoaded) {
119120
document.title = `${fullName} (@${username}) / Twitter`;
120-
dispatch(fetchUserTweets({ userId: params.userId, page: 0, activeTab: userTweetsActiveTab }));
121+
dispatch(fetchUserPinnedTweet({ userId }));
122+
dispatch(fetchUserTweets({ userId, page: 0 }));
121123
}
122124

123125
return () => {

frontend/src/pages/UserPage/UserPageTweets.tsx

Lines changed: 0 additions & 160 deletions
This file was deleted.

0 commit comments

Comments
 (0)