aboutsummaryrefslogtreecommitdiff
path: root/src/components/search/SearchResultCell.tsx
diff options
context:
space:
mode:
authorankit-thanekar007 <ankit.thanekar007@gmail.com>2021-03-04 16:06:21 -0800
committerankit-thanekar007 <ankit.thanekar007@gmail.com>2021-03-04 16:06:21 -0800
commit79396f899fe25f611d790d918e8ae4275a61e43c (patch)
treef5c9c003091d3d28ca32b31bb2b1bf8f67892acc /src/components/search/SearchResultCell.tsx
parent95bc850b9db986b9f462f19d7801218027307d58 (diff)
TMA-663-Changes for empty view
Diffstat (limited to 'src/components/search/SearchResultCell.tsx')
-rw-r--r--src/components/search/SearchResultCell.tsx87
1 files changed, 73 insertions, 14 deletions
diff --git a/src/components/search/SearchResultCell.tsx b/src/components/search/SearchResultCell.tsx
index 084c6afe..705fb5c9 100644
--- a/src/components/search/SearchResultCell.tsx
+++ b/src/components/search/SearchResultCell.tsx
@@ -1,12 +1,24 @@
+import {useNavigation} from '@react-navigation/native';
import React, {useEffect, useState} from 'react';
-import {Image, StyleSheet, Text, View} from 'react-native';
+import {Alert, Image, StyleSheet, Text, View} from 'react-native';
+import {TouchableOpacity} from 'react-native-gesture-handler';
+import {useDispatch, useStore} from 'react-redux';
+import {ERROR_UNABLE_TO_VIEW_PROFILE} from '../../constants/strings';
import {loadImageFromURL} from '../../services';
-import {ProfilePreviewType} from '../../types';
+import {RootState} from '../../store/rootReducer';
+import {ProfilePreviewType, ScreenType, UserType} from '../../types';
import {normalize, SCREEN_WIDTH} from '../../utils';
-import {defaultUserProfile} from '../../utils/users';
+import {
+ addUserToRecentlyViewed,
+ checkIfUserIsBlocked,
+ defaultUserProfile,
+ fetchUserX,
+ userXInStore,
+} from '../../utils/users';
interface SearchResults {
profileData: ProfilePreviewType;
+ loggedInUser: UserType;
}
const SearchResultsCell: React.FC<SearchResults> = ({
@@ -19,8 +31,9 @@ const SearchResultsCell: React.FC<SearchResults> = ({
thumbnail_url,
category,
},
+ loggedInUser,
}) => {
- const [avatar, setAvatar] = useState('');
+ const [avatar, setAvatar] = useState<string | undefined>(undefined);
useEffect(() => {
(async () => {
if (thumbnail_url !== undefined) {
@@ -37,9 +50,60 @@ const SearchResultsCell: React.FC<SearchResults> = ({
})();
}, [thumbnail_url]);
+ const dispatch = useDispatch();
+ const state: RootState = useStore().getState();
+ const navigation = useNavigation();
+ const addToRecentlyStoredAndNavigateToProfile = async () => {
+ try {
+ //If the logged in user is blocked by the user being viewed, do not proceed.
+ const isUserBlocked = await checkIfUserIsBlocked(
+ id,
+ dispatch,
+ loggedInUser,
+ );
+ if (isUserBlocked) {
+ Alert.alert(ERROR_UNABLE_TO_VIEW_PROFILE);
+ return;
+ }
+
+ await addUserToRecentlyViewed({
+ id,
+ first_name,
+ last_name,
+ thumbnail_url,
+ username,
+ });
+
+ const userXId = loggedInUser.username === username ? undefined : id;
+
+ /**
+ * Dispatch an event to Fetch the user details only if we're navigating to
+ * a userX's profile.
+ * If the user is already present in store, do not fetch again.
+ * Finally, Navigate to profile of the user selected.
+ */
+ if (userXId && !userXInStore(state, ScreenType.Search, id)) {
+ await fetchUserX(
+ dispatch,
+ {userId: id, username: username},
+ ScreenType.Search,
+ );
+ }
+
+ navigation.navigate('Profile', {
+ userXId,
+ screenType: ScreenType.Search,
+ });
+ } catch (e) {
+ console.log(e);
+ }
+ };
+
const userCell = () => {
return (
- <View style={styles.cellContainer}>
+ <TouchableOpacity
+ onPress={addToRecentlyStoredAndNavigateToProfile}
+ style={styles.cellContainer}>
<Image
defaultSource={defaultUserProfile()}
source={{uri: avatar}}
@@ -51,7 +115,7 @@ const SearchResultsCell: React.FC<SearchResults> = ({
{first_name + ' ' + last_name}
</Text>
</View>
- </View>
+ </TouchableOpacity>
);
};
@@ -65,7 +129,7 @@ const SearchResultsCell: React.FC<SearchResults> = ({
const categoryCell = () => {
return (
- <View style={styles.cellContainer}>
+ <TouchableOpacity style={styles.cellContainer}>
<View style={[styles.imageContainer, styles.categoryBackground]}>
<Image
resizeMode="contain"
@@ -76,16 +140,11 @@ const SearchResultsCell: React.FC<SearchResults> = ({
<View style={styles.initialTextContainer}>
<Text style={styles.initialTextStyle}>{name}</Text>
</View>
- </View>
+ </TouchableOpacity>
);
};
- return (
- <>
- {name !== undefined && categoryCell()}
- {name === undefined && userCell()}
- </>
- );
+ return name === undefined ? userCell() : categoryCell();
};
const styles = StyleSheet.create({