From 3362da273a4ffcc7d6362278bcb9fd919deda2b9 Mon Sep 17 00:00:00 2001
From: Ashm Walia <40498934+ashmgarv@users.noreply.github.com>
Date: Tue, 10 Nov 2020 16:28:15 -0800
Subject: [TMA - 388] Block Users (Frontend) (#115)

* initial

* Final

* sc

* sc

* sall change

* Remove follow button when blocked

* Small change

* small changes again
---
 src/components/profile/ProfilePreview.tsx | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

(limited to 'src/components/profile/ProfilePreview.tsx')

diff --git a/src/components/profile/ProfilePreview.tsx b/src/components/profile/ProfilePreview.tsx
index 9c953e7d..abc29422 100644
--- a/src/components/profile/ProfilePreview.tsx
+++ b/src/components/profile/ProfilePreview.tsx
@@ -7,12 +7,15 @@ import {
   StyleSheet,
   ViewProps,
   TouchableOpacity,
+  Alert,
 } from 'react-native';
 import {useNavigation} from '@react-navigation/native';
 import RNFetchBlob from 'rn-fetch-blob';
 import AsyncStorage from '@react-native-community/async-storage';
 import {AVATAR_PHOTO_ENDPOINT} from '../../constants';
 import {UserType} from '../../types';
+import {AuthContext} from '../../routes/';
+import {isUserBlocked} from '../../services';
 const NO_USER: UserType = {
   userId: '',
   username: '',
@@ -38,6 +41,7 @@ const ProfilePreview: React.FC<ProfilePreviewProps> = ({
   style,
 }) => {
   const navigation = useNavigation();
+  const {user: loggedInUser, logout} = React.useContext(AuthContext);
   const [avatarURI, setAvatarURI] = useState<string | null>(null);
   const [user, setUser] = useState<UserType>(NO_USER);
   useEffect(() => {
@@ -80,6 +84,16 @@ const ProfilePreview: React.FC<ProfilePreviewProps> = ({
    * Cache maintains 10 recently searched users, popping off the oldest one if
    * needed to make space.
    */
+
+  const checkIfUserIsBlocked = async (userId: string) => {
+    const token = await AsyncStorage.getItem('token');
+    if (!token) {
+      logout();
+      return false;
+    }
+    return await isUserBlocked(userId, loggedInUser.userId, token);
+  };
+
   const addToRecentlyStoredAndNavigateToProfile = async () => {
     let user: ProfilePreviewType = {
       id,
@@ -87,7 +101,14 @@ const ProfilePreview: React.FC<ProfilePreviewProps> = ({
       first_name,
       last_name,
     };
+
     try {
+      //If the logged in user is blocked by the user being viewed, do not proceed.
+      const isUserBlocked = await checkIfUserIsBlocked(user.id);
+      if (isUserBlocked) {
+        Alert.alert('You cannot view this profile');
+        return;
+      }
       if (!isComment) {
         const jsonValue = await AsyncStorage.getItem(
           '@recently_searched_users',
-- 
cgit v1.2.3-70-g09d2

