diff options
author | George Rusu <george@tagg.id> | 2021-05-12 18:10:08 -0700 |
---|---|---|
committer | George Rusu <george@tagg.id> | 2021-05-12 18:10:08 -0700 |
commit | 3cfad768bc7d2369ff49d1b57771ac317dbae71e (patch) | |
tree | e71a7016c287ad53ec3857dcd519e76ef4b36aaf /src/components/friends/InviteFriendTile.tsx | |
parent | 4569240e09ff134d49fa11180c0074329a3ac95b (diff) |
added functionality for individual sms for invites and updated api and strings to match
Diffstat (limited to 'src/components/friends/InviteFriendTile.tsx')
-rw-r--r-- | src/components/friends/InviteFriendTile.tsx | 86 |
1 files changed, 74 insertions, 12 deletions
diff --git a/src/components/friends/InviteFriendTile.tsx b/src/components/friends/InviteFriendTile.tsx index 5237389a..9967b365 100644 --- a/src/components/friends/InviteFriendTile.tsx +++ b/src/components/friends/InviteFriendTile.tsx @@ -1,17 +1,27 @@ +import AsyncStorage from '@react-native-community/async-storage'; import React, {useEffect, useState} from 'react'; import { Alert, + Linking, StyleSheet, Text, TouchableOpacity, TouchableWithoutFeedback, View, } from 'react-native'; -import {TAGG_LIGHT_BLUE} from '../../constants'; +import {useSelector} from 'react-redux'; +import {RootState} from 'src/store/rootReducer'; +import { + CREATE_INVITE_CODE, + GET_REMAINING_INVITES, + TAGG_LIGHT_BLUE, +} from '../../constants'; import { ERROR_NO_CONTACT_INVITE_LEFT, ERROR_SOMETHING_WENT_WRONG, - SUCCESS_INVITE_CONTACT, + INVITE_USER_SMS_BODY, + SUCCESS_CONFIRM_INVITE_CONTACT_MESSAGE, + SUCCESS_CONFIRM_INVITE_CONTACT_TITLE, SUCCESS_LAST_CONTACT_INVITE, } from '../../constants/strings'; import {InviteContactType} from '../../screens/profile/InviteFriendsScreen'; @@ -24,26 +34,78 @@ interface InviteFriendTileProps { const InviteFriendTile: React.FC<InviteFriendTileProps> = ({item}) => { const [invited, setInvited] = useState<boolean>(false); + const {name} = useSelector((state: RootState) => state.user.profile); const [formatedPhoneNumber, setFormattedPhoneNumber] = useState<string>(''); const handleInviteFriend = async () => { - const invites_left = await inviteFriendService( - item.phoneNumber, - item.firstName, - item.lastName, - ); + const invites_left = await getRemainingInviteCount(); if (invites_left > 0) { setInvited(true); - Alert.alert(SUCCESS_INVITE_CONTACT(invites_left)); - } else if (invites_left === 0) { - setInvited(true); - Alert.alert(SUCCESS_LAST_CONTACT_INVITE); - } else if (invites_left === -1) { + Alert.alert( + SUCCESS_CONFIRM_INVITE_CONTACT_TITLE(invites_left), + SUCCESS_CONFIRM_INVITE_CONTACT_MESSAGE, + [ + {text: 'No!', style: 'cancel'}, + { + text: 'Yes!', + onPress: async () => { + const inviteCode = await handleCreateInviteCode(); + await inviteFriendService( + item.phoneNumber, + item.firstName, + item.lastName, + ); + Linking.openURL( + `sms:${item.phoneNumber}&body=${INVITE_USER_SMS_BODY( + item.firstName, + name, + inviteCode, + )}`, + ); + if (invites_left === 1) { + Alert.alert(SUCCESS_LAST_CONTACT_INVITE); + } + }, + }, + ], + ); + } else if (invites_left === -1 || invites_left === 0) { Alert.alert(ERROR_NO_CONTACT_INVITE_LEFT); } else { Alert.alert(ERROR_SOMETHING_WENT_WRONG); } }; + const getRemainingInviteCount = async () => { + const firstName = name.split(' ')[0]; + const lastName = name.split(' ')[1]; + const token = await AsyncStorage.getItem('token'); + const response = await fetch(GET_REMAINING_INVITES, { + method: 'POST', + headers: { + Authorization: 'Token ' + token, + }, + body: JSON.stringify({ + invitee_first_name: firstName, + invitee_last_name: lastName, + }), + }); + if (response.status === 200) { + const data = await response.json(); + return data.invites_left; + } else if (response.status === 500) { + return -1; + } + }; + + const handleCreateInviteCode = async () => { + const response = await fetch(CREATE_INVITE_CODE, {method: 'POST'}); + if (response.status === 200) { + const data = await response.json(); + return data.code; + } else if (response.status === 500) { + return -1; + } + }; useEffect(() => { const formatPhoneNumer = () => { const unformatted_number: string = item.phoneNumber; |