aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIvan Chen <ivan@tagg.id>2021-05-13 17:55:46 -0400
committerGitHub <noreply@github.com>2021-05-13 17:55:46 -0400
commit848afa989c2a0c324b65778dc05e03b7856f62c3 (patch)
tree40a733f00132451a583e7c6cae6a40bff25d8344
parent36a26cc7716913e8b243553169ae91588bc8e413 (diff)
parent8db9bb83850e8e368700cb9006b8666741360653 (diff)
Merge pull request #419 from grusuTagg/tma843-fix-invite-system
[TMA-860] Added Individual SMS Invite Functionality
-rw-r--r--src/components/friends/InviteFriendTile.tsx56
-rw-r--r--src/constants/api.ts5
-rw-r--r--src/constants/strings.ts6
-rw-r--r--src/services/UserFriendsService.ts65
-rw-r--r--src/services/UserProfileService.ts2
5 files changed, 102 insertions, 32 deletions
diff --git a/src/components/friends/InviteFriendTile.tsx b/src/components/friends/InviteFriendTile.tsx
index 5237389a..abd017d0 100644
--- a/src/components/friends/InviteFriendTile.tsx
+++ b/src/components/friends/InviteFriendTile.tsx
@@ -1,21 +1,30 @@
import React, {useEffect, useState} from 'react';
import {
Alert,
+ Linking,
StyleSheet,
Text,
TouchableOpacity,
TouchableWithoutFeedback,
View,
} from 'react-native';
+import {useSelector} from 'react-redux';
+import {RootState} from 'src/store/rootReducer';
import {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';
-import {inviteFriendService} from '../../services';
+import {
+ getRemainingInviteCount,
+ handleCreateInviteCode,
+ inviteFriendService,
+} from '../../services';
import {normalize} from '../../utils';
interface InviteFriendTileProps {
@@ -24,20 +33,41 @@ 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 () => {
+ setInvited(true);
+ 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);
diff --git a/src/constants/api.ts b/src/constants/api.ts
index 9d3f70c9..3c7e669e 100644
--- a/src/constants/api.ts
+++ b/src/constants/api.ts
@@ -51,7 +51,8 @@ export const USERS_FROM_CONTACTS_ENDPOINT: string =
API_URL + 'user_contacts/find_friends/';
export const INVITE_FRIEND_ENDPOINT: string =
API_URL + 'user_contacts/invite_friend/';
-
+export const CREATE_INVITE_CODE = API_URL + 'create-code/';
+export const GET_REMAINING_INVITES = API_URL + 'user_contacts/check_invite_count/'
// Suggested People
export const SP_USERS_ENDPOINT: string = API_URL + 'suggested_people/';
export const SP_UPDATE_PICTURE_ENDPOINT: string =
@@ -61,7 +62,7 @@ export const SP_MUTUAL_BADGE_HOLDERS_ENDPOINT: string =
export const ADD_BADGES_ENDPOINT: string = SP_USERS_ENDPOINT + 'add_badges/';
export const UPDATE_BADGES_ENDPOINT: string =
SP_USERS_ENDPOINT + 'update_badges/';
- export const REMOVE_BADGES_ENDPOINT: string =
+export const REMOVE_BADGES_ENDPOINT: string =
SP_USERS_ENDPOINT + 'remove_badges/';
export const GET_USER_BADGES_ENDPOINT: string = SP_USERS_ENDPOINT + 'get_badges/';
diff --git a/src/constants/strings.ts b/src/constants/strings.ts
index 56d54d39..2ce64aed 100644
--- a/src/constants/strings.ts
+++ b/src/constants/strings.ts
@@ -2,6 +2,7 @@
// Below is the regex to convert this into a csv for the Google Sheet
// export const (.*) = .*?(['|"|`])(.*)\2;
// replace with: $1\t$3
+export const APP_STORE_LINK = 'https://apps.apple.com/us/app/tagg-discover-your-community/id1537853613'
export const ADD_COMMENT_TEXT = (username?: string) => username ? `Reply to ${username}` : 'Add a comment...'
export const COMING_SOON_MSG = 'Creating more fun things for you, surprises coming soon 😉';
export const ERROR_ATTEMPT_EDIT_SP = 'Can\'t let you do that yet! Please onboard Suggested People first!';
@@ -68,6 +69,11 @@ export const SUCCESS_BADGES_UPDATE = 'Badges updated successfully!'
export const SUCCESS_CATEGORY_DELETE = 'Category successfully deleted, but its memory will live on';
export const SUCCESS_INVITATION_CODE = 'Welcome to Tagg!';
export const SUCCESS_INVITE_CONTACT = (str: string) => `Success! You now have ${str} invites left!`;
+export const SUCCESS_CONFIRM_INVITE_CONTACT_TITLE = (str: string) => `You have ${str} invites left!`;
+export const SUCCESS_CONFIRM_INVITE_CONTACT_MESSAGE = 'Use one now?';
+export const INVITE_USER_SMS_BODY = (invitedUserName: string, invitee: string, inviteCode: string) => `Hey ${invitedUserName}!\n
+You've been tagged by ${invitee}. Follow the instructions below to skip the line and join them on Tagg!\n
+Sign up and use this code to get in: ${inviteCode}\n ${APP_STORE_LINK}`;
export const SUCCESS_LAST_CONTACT_INVITE = 'Done! That was your last invite, hope you used it wisely!';
export const SUCCESS_LINK = (str: string) => `Successfully linked ${str} 🎉`;
export const SUCCESS_PIC_UPLOAD = 'Beautiful, the picture was uploaded successfully!';
diff --git a/src/services/UserFriendsService.ts b/src/services/UserFriendsService.ts
index deb3ec6d..453f35a5 100644
--- a/src/services/UserFriendsService.ts
+++ b/src/services/UserFriendsService.ts
@@ -1,7 +1,9 @@
import AsyncStorage from '@react-native-community/async-storage';
import {Alert} from 'react-native';
import {
+ CREATE_INVITE_CODE,
FRIENDS_ENDPOINT,
+ GET_REMAINING_INVITES,
INVITE_FRIEND_ENDPOINT,
USERS_FROM_CONTACTS_ENDPOINT,
} from '../constants';
@@ -220,23 +222,54 @@ export const inviteFriendService = async (
inviteeFirstName: string,
inviteeLastName: string,
) => {
- const token = await AsyncStorage.getItem('token');
- const response = await fetch(INVITE_FRIEND_ENDPOINT, {
- method: 'POST',
- headers: {
- Authorization: 'Token ' + token,
- },
- body: JSON.stringify({
- invitee_phone_number: phoneNumber,
- invitee_first_name: inviteeFirstName,
- invitee_last_name: inviteeLastName,
- }),
- });
- if (response.status === 201 || response.status === 200) {
+ try {
+ const token = await AsyncStorage.getItem('token');
+ const response = await fetch(INVITE_FRIEND_ENDPOINT, {
+ method: 'POST',
+ headers: {
+ Authorization: 'Token ' + token,
+ },
+ body: JSON.stringify({
+ invitee_phone_number: phoneNumber,
+ invitee_first_name: inviteeFirstName,
+ invitee_last_name: inviteeLastName,
+ }),
+ });
+ return response.status === 201 || response.status === 200;
+ } catch (error) {
+ console.log(error);
+ Alert.alert(ERROR_SOMETHING_WENT_WRONG_REFRESH);
+ return false;
+ }
+};
+
+export const handleCreateInviteCode = async () => {
+ const response = await fetch(CREATE_INVITE_CODE, {method: 'POST'});
+ if (response.status === 200) {
const data = await response.json();
- return data.invites_left;
- } else if (response.status === 400) {
+ return data.code;
+ } else if (response.status === 500) {
return -1;
}
- return false;
+};
+
+export const getRemainingInviteCount = async () => {
+ const token = await AsyncStorage.getItem('token');
+ try {
+ const response = await fetch(GET_REMAINING_INVITES, {
+ method: 'GET',
+ headers: {
+ Authorization: 'Token ' + token,
+ },
+ });
+ if (response.status === 200) {
+ const data = await response.json();
+ return data.invites_left;
+ } else if (response.status === 500) {
+ return -1;
+ }
+ } catch (error) {
+ console.log(error);
+ Alert.alert(ERROR_SOMETHING_WENT_WRONG_REFRESH);
+ }
};
diff --git a/src/services/UserProfileService.ts b/src/services/UserProfileService.ts
index 8b7b78e1..624f0e2a 100644
--- a/src/services/UserProfileService.ts
+++ b/src/services/UserProfileService.ts
@@ -442,7 +442,7 @@ export const verifyExistingInformation = async (
const form = new FormData();
if (email) {
form.append('email', email);
- }
+ }
if (username) {
form.append('username', username);
}