aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShravya Ramesh <shravs1208@gmail.com>2021-03-18 02:45:46 -0700
committerShravya Ramesh <shravs1208@gmail.com>2021-03-18 02:45:46 -0700
commitfe5ff5ec516302811b7323cadf8e3dd0939beea4 (patch)
treece5dcb988cbad15f5fe3dfb4a41b592ce895d8e4
parent1080adb75c18f6da6b91be4264c69a9bf908ff0d (diff)
services +
-rw-r--r--src/components/profile/Friends.tsx37
-rw-r--r--src/screens/profile/InviteFriendsScreen.tsx21
-rw-r--r--src/services/UserFriendsService.ts67
-rw-r--r--src/types/types.ts6
-rw-r--r--src/utils/friends.ts2
5 files changed, 107 insertions, 26 deletions
diff --git a/src/components/profile/Friends.tsx b/src/components/profile/Friends.tsx
index a1030b49..36e0ef8a 100644
--- a/src/components/profile/Friends.tsx
+++ b/src/components/profile/Friends.tsx
@@ -98,22 +98,27 @@ const Friends: React.FC<FriendsProps> = ({result, screenType, userId}) => {
return (
<>
- <View style={styles.subheader}>
- <View style={styles.addFriendHeaderContainer}>
- <Text style={[styles.subheaderText]}>Add Friends</Text>
- <TouchableOpacity
- style={{flexDirection: 'row'}}
- onPress={() =>
- navigation.navigate('InviteFriendsScreen', {
- screenType: ScreenType.Profile,
- })
- }>
- <FindFriendsBlueIcon width={20} height={20} />
- <Text style={styles.findFriendsSubheaderText}>Find Friends</Text>
- </TouchableOpacity>
- </View>
- <UsersFromContacts />
- </View>
+ {loggedInUser.userId === userId ||
+ (userId === undefined && (
+ <View style={styles.subheader}>
+ <View style={styles.addFriendHeaderContainer}>
+ <Text style={[styles.subheaderText]}>Add Friends</Text>
+ <TouchableOpacity
+ style={{flexDirection: 'row'}}
+ onPress={() =>
+ navigation.navigate('InviteFriendsScreen', {
+ screenType: ScreenType.Profile,
+ })
+ }>
+ <FindFriendsBlueIcon width={20} height={20} />
+ <Text style={styles.findFriendsSubheaderText}>
+ Find Friends
+ </Text>
+ </TouchableOpacity>
+ </View>
+ <UsersFromContacts />
+ </View>
+ ))}
<Text style={[styles.subheaderText, styles.friendsSubheaderText]}>
Friends
</Text>
diff --git a/src/screens/profile/InviteFriendsScreen.tsx b/src/screens/profile/InviteFriendsScreen.tsx
index 8fc12a5e..1a5de1ce 100644
--- a/src/screens/profile/InviteFriendsScreen.tsx
+++ b/src/screens/profile/InviteFriendsScreen.tsx
@@ -13,7 +13,7 @@ import {
TouchableWithoutFeedback,
} from 'react-native';
import {useDispatch, useStore} from 'react-redux';
-import {ProfilePreviewType, ScreenType} from '../../types';
+import {ContactType, ProfilePreviewType, ScreenType} from '../../types';
import {
handleAddFriend,
HeaderHeight,
@@ -53,24 +53,30 @@ const InviteFriendsScreen: React.FC<InviteFriendsScreenProps> = ({
});
const [query, setQuery] = useState('');
- const extractPhoneNumbers = async () => {
- let phoneNumbers: Array<string> = [];
+ const extractContacts = async () => {
+ let retrievedContacts: Array<ContactType> = [];
await getAll().then((contacts) => {
contacts.map((contact) => {
+ let obj: ContactType = {
+ first_name: contact.givenName,
+ last_name: contact.familyName,
+ };
contact.phoneNumbers.map(async (phoneNumber) => {
- phoneNumbers.push(await phoneNumber.number);
+ obj.phone_number = phoneNumber.number;
+ retrievedContacts.push(obj);
+ console.log('contact: ', obj);
});
});
});
- return phoneNumbers;
+ return retrievedContacts;
};
useEffect(() => {
const handleFindFriends = () => {
- extractPhoneNumbers().then(async (phoneNumbers) => {
+ extractContacts().then(async (retrievedContacts) => {
const permission = await checkPermission();
if (permission === 'authorized') {
- let response = await usersFromContactsService(phoneNumbers);
+ let response = await usersFromContactsService(retrievedContacts);
await setUsersFromContacts(response.existing_tagg_users);
await setNonUsersFromContacts(response.invite_from_contacts);
usersFromContacts.map((user) => console.log('user: ', user.username));
@@ -169,6 +175,7 @@ const InviteFriendsScreen: React.FC<InviteFriendsScreenProps> = ({
const NonUsersFromContacts = () => (
<>
<FlatList
+ contentContainerStyle={{paddingBottom: 130}}
showsVerticalScrollIndicator={false}
data={results.nonUsersFromContacts}
keyExtractor={(item) => item.phoneNumber}
diff --git a/src/services/UserFriendsService.ts b/src/services/UserFriendsService.ts
index dbec1974..86339868 100644
--- a/src/services/UserFriendsService.ts
+++ b/src/services/UserFriendsService.ts
@@ -1,8 +1,13 @@
//Abstracted common friends api calls out here
+import AsyncStorage from '@react-native-community/async-storage';
import {Alert} from 'react-native';
-import {FriendshipStatusType} from '../types';
-import {FRIENDS_ENDPOINT} from '../constants';
+import {ContactType, FriendshipStatusType} from '../types';
+import {
+ FRIENDS_ENDPOINT,
+ INVITE_FRIEND_ENDPOINT,
+ USERS_FROM_CONTACTS_ENDPOINT,
+} from '../constants';
import {ERROR_SOMETHING_WENT_WRONG_REFRESH} from '../constants/strings';
export const loadFriends = async (userId: string, token: string) => {
@@ -181,3 +186,61 @@ export const deleteFriendshipService = async (
return false;
}
};
+
+export const usersFromContactsService = async (
+ contacts: Array<ContactType>,
+) => {
+ console.log('Contacts: ', contacts);
+ try {
+ const token = await AsyncStorage.getItem('token');
+ const response = await fetch(USERS_FROM_CONTACTS_ENDPOINT, {
+ method: 'POST',
+ headers: {
+ Authorization: 'Token ' + token,
+ },
+ body: JSON.stringify({
+ contacts: contacts,
+ }),
+ });
+ const status = response.status;
+ if (Math.floor(status / 100) === 2) {
+ const users_data = await response.json();
+ return users_data;
+ } else {
+ console.log(
+ 'Something went wrong! 😭',
+ 'Not able to retrieve tagg users list',
+ );
+ }
+ } catch (error) {
+ console.log(error);
+ Alert.alert(ERROR_SOMETHING_WENT_WRONG_REFRESH);
+ return false;
+ }
+};
+
+export const inviteFriendService = async (
+ phoneNumber: string,
+ inviteeFirstName: string,
+ inviteeLastName: string,
+ inviterFullName: string,
+) => {
+ const token = await AsyncStorage.getItem('token');
+ const response = await fetch(INVITE_FRIEND_ENDPOINT, {
+ method: 'POST',
+ headers: {
+ Authorization: 'Token ' + token,
+ },
+ body: JSON.stringify({
+ phone_number: phoneNumber,
+ invitee_first_name: inviteeFirstName,
+ invitee_last_name: inviteeLastName,
+ inviter_full_name: inviterFullName,
+ }),
+ });
+ if (response.status === 201) {
+ const response_body = await response.json();
+ return response_body;
+ }
+ return false;
+};
diff --git a/src/types/types.ts b/src/types/types.ts
index e068eeba..dc2817bd 100644
--- a/src/types/types.ts
+++ b/src/types/types.ts
@@ -256,3 +256,9 @@ export type SearchCategoryType = {
name: string;
category: string;
};
+
+export type ContactType = {
+ phone_number: string;
+ first_name: string;
+ last_name: string;
+};
diff --git a/src/utils/friends.ts b/src/utils/friends.ts
index 3398c123..55d65170 100644
--- a/src/utils/friends.ts
+++ b/src/utils/friends.ts
@@ -60,7 +60,7 @@ export const handleAddFriend = async (
dispatch: AppDispatch,
state: RootState,
) => {
- const success = await dispatch(addFriend(friend, screenType));
+ const success = await dispatch(addFriend(friend, screenType, state));
await dispatch(updateUserXFriends(friend.id, state));
await dispatch(updateUserXProfileAllScreens(friend.id, state));
return success;