aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/components/notifications/Notification.tsx149
-rw-r--r--src/constants/constants.ts4
-rw-r--r--src/screens/main/NotificationsScreen.tsx5
-rw-r--r--src/types/types.ts24
4 files changed, 96 insertions, 86 deletions
diff --git a/src/components/notifications/Notification.tsx b/src/components/notifications/Notification.tsx
index 951a5bf6..c2c6c4e4 100644
--- a/src/components/notifications/Notification.tsx
+++ b/src/components/notifications/Notification.tsx
@@ -6,11 +6,7 @@ import LinearGradient from 'react-native-linear-gradient';
import {useDispatch, useStore} from 'react-redux';
import {BACKGROUND_GRADIENT_MAP} from '../../constants';
import {ERROR_DELETED_OBJECT} from '../../constants/strings';
-import {
- loadImageFromURL,
- loadMoments,
- loadMomentThumbnail,
-} from '../../services';
+import {loadImageFromURL} from '../../services';
import {
acceptFriendRequest,
declineFriendRequest,
@@ -19,19 +15,22 @@ import {
updateUserXFriends,
} from '../../store/actions';
import {RootState} from '../../store/rootReducer';
-import {MomentType, NotificationType, ScreenType} from '../../types';
import {
- fetchUserX,
- getTokenOrLogout,
- SCREEN_HEIGHT,
- userXInStore,
-} from '../../utils';
+ CommentNotificationType,
+ CommentThreadType,
+ MomentType,
+ NotificationType,
+ ScreenType,
+ ThreadNotificationType,
+ UserType,
+} from '../../types';
+import {fetchUserX, SCREEN_HEIGHT, userXInStore} from '../../utils';
import AcceptDeclineButtons from '../common/AcceptDeclineButtons';
interface NotificationProps {
item: NotificationType;
screenType: ScreenType;
- moments: MomentType[];
+ loggedInUser: UserType;
}
const Notification: React.FC<NotificationProps> = (props) => {
@@ -44,7 +43,7 @@ const Notification: React.FC<NotificationProps> = (props) => {
unread,
},
screenType,
- moments: loggedInUserMoments,
+ loggedInUser,
} = props;
const navigation = useNavigation();
@@ -53,7 +52,6 @@ const Notification: React.FC<NotificationProps> = (props) => {
const [avatar, setAvatar] = useState<string | undefined>(undefined);
const [momentURI, setMomentURI] = useState<string | undefined>(undefined);
- const [onTapLoadProfile, setOnTapLoadProfile] = useState<boolean>(false);
useEffect(() => {
(async () => {
@@ -67,40 +65,22 @@ const Notification: React.FC<NotificationProps> = (props) => {
}, []);
useEffect(() => {
- if (onTapLoadProfile) {
- fetchUserX(dispatch, {userId: id, username: username}, screenType);
- }
- return () => {
- setOnTapLoadProfile(false);
- };
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, [onTapLoadProfile]);
-
- useEffect(() => {
- let mounted = true;
- const loadMomentImage = async (moment_id: string) => {
- const response = await loadMomentThumbnail(moment_id);
- if (mounted && response) {
- setMomentURI(response);
- } else {
- // if not set to empty, it will re-use the previous notification's state
- setMomentURI(undefined);
- }
- };
- if (
- (notification_type === 'CMT' ||
+ if (notification_object) {
+ let url: string | undefined;
+ let obj;
+ if (
notification_type === 'MOM_3+' ||
- notification_type === 'MOM_FRIEND') &&
- notification_object
- ) {
- loadMomentImage(
- notification_object.moment_id
- ? notification_object.moment_id
- : notification_object.parent_comment.moment_id,
- );
- return () => {
- mounted = false;
- };
+ notification_type === 'MOM_FRIEND'
+ ) {
+ obj = notification_object as MomentType;
+ url = obj.thumbnail_url;
+ } else if (notification_type === 'CMT') {
+ obj = notification_object as CommentNotificationType;
+ url = obj.notification_data.thumbnail_url;
+ }
+ if (url) {
+ setMomentURI(url);
+ }
}
}, [id, notification_object, notification_type]);
@@ -126,53 +106,64 @@ const Notification: React.FC<NotificationProps> = (props) => {
Alert.alert(ERROR_DELETED_OBJECT);
break;
}
- let {moment_id} = notification_object;
- let {comment_id} = notification_object;
- //If this is a thread, get comment_id and moment_id from parent_comment
- if (!notification_object?.moment_id) {
- moment_id = notification_object?.parent_comment?.moment_id;
- comment_id = notification_object?.parent_comment?.comment_id;
- }
+ /**
+ * Notification object knows
+ * 1 - Which comment
+ * 2 - Which user
+ * The comment / reply belongs to
+ * STEP 1 : Populate reply / comment
+ * STEP 2 : Load user data if moment does not belong to the logged in user
+ * STEP 3 : Navigate to relevant moment
+ */
- // Now find the moment we need to display
- let moment: MomentType | undefined = loggedInUserMoments?.find(
- (m) => m.moment_id === moment_id,
- );
+ let comment_id: string;
+ let not_object;
+ let reply: CommentThreadType | undefined;
let userXId;
- // If moment does not belong to the logged in user, then the comment was probably a reply to logged in user's comment
- // on userX's moment
- // Load moments for userX
- if (!moment) {
- let moments: MomentType[] = [];
- try {
- //Populate local state in the mean time
- setOnTapLoadProfile(true);
- const token = await getTokenOrLogout(dispatch);
- moments = await loadMoments(id, token);
- } catch (err) {
- console.log(err);
- }
- moment = moments?.find((m) => m.moment_id === moment_id);
- userXId = id;
+ // STEP 1
+ if ('parent_comment' in notification_object) {
+ //This is a reply
+ not_object = notification_object as ThreadNotificationType;
+ comment_id = not_object.parent_comment;
+ reply = {
+ parent_comment: {comment_id: comment_id},
+ comment_id: not_object.comment_id,
+ };
+ } else {
+ not_object = notification_object as CommentNotificationType;
+ comment_id = not_object.comment_id;
}
- //Now if moment was found, navigate to the respective moment
+ //STEP 2
+ const {user, ...moment} = not_object.notification_data;
+ if (user.id !== loggedInUser.userId) {
+ fetchUserX(
+ dispatch,
+ {userId: user.id, username: user.username},
+ screenType,
+ );
+ userXId = user.id;
+ }
+
+ const {moment_id} = moment;
+
+ //STEP 3
if (moment) {
- if (notification_object?.parent_comment) {
- dispatch(updateReplyPosted(notification_object));
+ if (reply) {
+ dispatch(updateReplyPosted(reply));
}
navigation.push('IndividualMoment', {
moment,
- userXId: userXId, // we're only viewing our own moment here
+ userXId,
screenType,
});
setTimeout(() => {
navigation.push('MomentCommentsScreen', {
- moment_id: moment_id,
+ moment_id,
screenType,
- comment_id: comment_id,
+ comment_id,
});
}, 500);
}
diff --git a/src/constants/constants.ts b/src/constants/constants.ts
index 7fcc457f..3bad0ed7 100644
--- a/src/constants/constants.ts
+++ b/src/constants/constants.ts
@@ -131,8 +131,8 @@ export const BACKGROUND_GRADIENT_MAP: Record<
[BackgroundGradientType.Light]: ['#9F00FF', '#27EAE9'],
[BackgroundGradientType.Dark]: ['#421566', '#385D5E'],
[BackgroundGradientType.Notification]: [
- 'rgba(143, 1, 255, 0.5)',
- 'rgba(110, 231, 231, 0.5)',
+ 'rgba(143, 1, 255, 0.4)',
+ 'rgba(110, 231, 231, 0.4)',
],
};
diff --git a/src/screens/main/NotificationsScreen.tsx b/src/screens/main/NotificationsScreen.tsx
index d9952aa8..f35bb22c 100644
--- a/src/screens/main/NotificationsScreen.tsx
+++ b/src/screens/main/NotificationsScreen.tsx
@@ -35,6 +35,9 @@ const NotificationsScreen: React.FC = () => {
const {notifications} = useSelector(
(state: RootState) => state.notifications,
);
+
+ const {user: loggedInUser} = useSelector((state: RootState) => state.user);
+
const [sectionedNotifications, setSectionedNotifications] = useState<
{title: 'Today' | 'Yesterday' | 'This Week'; data: NotificationType[]}[]
>([]);
@@ -130,7 +133,7 @@ const NotificationsScreen: React.FC = () => {
<Notification
item={item}
screenType={ScreenType.Notifications}
- moments={item.notification_type === 'CMT' ? loggedInUserMoments : []}
+ loggedInUser={loggedInUser}
/>
);
diff --git a/src/types/types.ts b/src/types/types.ts
index f1ba12f4..ab995292 100644
--- a/src/types/types.ts
+++ b/src/types/types.ts
@@ -87,7 +87,6 @@ export interface MomentType {
moment_url: string;
thumbnail_url: string;
}
-
export interface CommentBaseType {
comment_id: string;
comment: string;
@@ -165,7 +164,7 @@ export enum CategorySelectionScreenType {
export enum BackgroundGradientType {
Light,
Dark,
- Notification
+ Notification,
}
/**
@@ -177,11 +176,28 @@ export type TaggPopupType = {
next?: TaggPopupType;
};
+export interface MomentWithUserType extends MomentType {
+ user: ProfilePreviewType;
+}
+
+export interface CommentNotificationType {
+ comment_id: string;
+ notification_data: MomentWithUserType;
+}
+
+export interface ThreadNotificationType extends CommentNotificationType {
+ parent_comment: string;
+}
+
export type NotificationType = {
actor: ProfilePreviewType;
verbage: string;
notification_type: TypeOfNotification;
- notification_object: CommentType | CommentThreadType | MomentType | undefined;
+ notification_object:
+ | CommentNotificationType
+ | ThreadNotificationType
+ | MomentType
+ | undefined;
timestamp: string;
unread: boolean;
};
@@ -196,7 +212,7 @@ export type TypeOfNotification =
| 'FRD_ACPT'
// notification_object is undefined
| 'FRD_DEC'
- // notification_object is CommentType || CommentThreadType
+ // notification_object is CommentNotificationType || ThreadNotificationType
| 'CMT'
// notification_object is MomentType
| 'MOM_3+'