Skip to content

Commit

Permalink
Merge pull request #15330 from Expensify/cristi_new-contact-method
Browse files Browse the repository at this point in the history
New contact method
  • Loading branch information
marcaaron authored Apr 10, 2023
2 parents ca8886a + 539fa78 commit 5df7225
Show file tree
Hide file tree
Showing 11 changed files with 266 additions and 421 deletions.
2 changes: 1 addition & 1 deletion src/ROUTES.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export default {
SETTINGS_ADD_DEBIT_CARD: 'settings/payments/add-debit-card',
SETTINGS_ADD_BANK_ACCOUNT: 'settings/payments/add-bank-account',
SETTINGS_ENABLE_PAYMENTS: 'settings/payments/enable-payments',
SETTINGS_ADD_LOGIN: 'settings/addlogin/:type',
getSettingsAddLoginRoute: type => `settings/addlogin/${type}`,
SETTINGS_PAYMENTS_TRANSFER_BALANCE: 'settings/payments/transfer-balance',
SETTINGS_PAYMENTS_CHOOSE_TRANSFER_ACCOUNT: 'settings/payments/choose-transfer-account',
Expand All @@ -53,6 +52,7 @@ export default {
SETTINGS_CONTACT_METHODS,
SETTINGS_CONTACT_METHOD_DETAILS: `${SETTINGS_CONTACT_METHODS}/:contactMethod/details`,
getEditContactMethodRoute: contactMethod => `${SETTINGS_CONTACT_METHODS}/${encodeURIComponent(contactMethod)}/details`,
SETTINGS_NEW_CONTACT_METHOD: `${SETTINGS_CONTACT_METHODS}/new`,
NEW_GROUP: 'new/group',
NEW_CHAT: 'new/chat',
REPORT,
Expand Down
10 changes: 3 additions & 7 deletions src/languages/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,11 +373,14 @@ export default {
removeContactMethod: 'Remove contact method',
removeAreYouSure: 'Are you sure you want to remove this contact method? This action cannot be undone.',
resendMagicCode: 'Resend magic code',
failedNewContact: 'Failed to add this contact method.',
genericFailureMessages: {
requestContactMethodValidateCode: 'Failed to send a new magic code. Please wait a bit and try again.',
validateSecondaryLogin: 'Failed to validate contact method with given magic code. Please request a new code and try again.',
deleteContactMethod: 'Failed to delete contact method. Please reach out to Concierge for help.',
addContactMethod: 'Failed to add this contact method. Please reach out to Concierge for help.',
},
newContactMethod: 'New contact method',
},
pronouns: {
coCos: 'Co / Cos',
Expand Down Expand Up @@ -409,13 +412,6 @@ export default {
isShownOnProfile: 'Your timezone is shown on your profile.',
getLocationAutomatically: 'Automatically determine your location.',
},
addSecondaryLoginPage: {
addPhoneNumber: 'Add phone number',
addEmailAddress: 'Add email address',
enterPreferredPhoneNumberToSendValidationLink: 'Enter your preferred phone number to send a validation link.',
enterPreferredEmailToSendValidationLink: 'Enter your preferred email address to send a validation link.',
sendValidation: 'Send validation',
},
initialSettingsPage: {
about: 'About',
aboutPage: {
Expand Down
10 changes: 3 additions & 7 deletions src/languages/es.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,11 +372,14 @@ export default {
removeContactMethod: 'Eliminar método de contacto',
removeAreYouSure: '¿Estás seguro de que quieres eliminar este método de contacto? Esta acción no se puede deshacer.',
resendMagicCode: 'Reenviar código mágico',
failedNewContact: 'Hubo un error al añadir este método de contacto.',
genericFailureMessages: {
requestContactMethodValidateCode: 'No se ha podido enviar un nuevo código mágico. Espera un rato y vuelve a intentarlo.',
validateSecondaryLogin: 'No se ha podido validar el método de contacto con el código mágico provisto. Solicita un nuevo código y vuelve a intentarlo.',
deleteContactMethod: 'No se ha podido eliminar el método de contacto. Por favor contacta con Concierge para obtener ayuda.',
addContactMethod: 'Hubo un error al añadir este método de contacto. Por favor contacta con Concierge para obtener ayuda.',
},
newContactMethod: 'Nuevo método de contacto',
},
pronouns: {
coCos: 'Co / Cos',
Expand Down Expand Up @@ -408,13 +411,6 @@ export default {
isShownOnProfile: 'Tu zona horaria se muestra en tu perfil.',
getLocationAutomatically: 'Detecta tu ubicación automáticamente.',
},
addSecondaryLoginPage: {
addPhoneNumber: 'Agregar número de teléfono',
addEmailAddress: 'Agregar dirección de email',
enterPreferredPhoneNumberToSendValidationLink: 'Escribe tu número de teléfono para recibir el enlace de validación.',
enterPreferredEmailToSendValidationLink: 'Escribe tu email para recibir el enlace de validación.',
sendValidation: 'Enviar validación',
},
initialSettingsPage: {
about: 'Acerca de',
aboutPage: {
Expand Down
6 changes: 3 additions & 3 deletions src/libs/Navigation/AppNavigator/ModalStackNavigators.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,10 @@ const SettingsModalStackNavigator = createModalStackNavigator([
},
{
getComponent: () => {
const SettingsAddSecondaryLoginPage = require('../../../pages/settings/Profile/Contacts/AddSecondaryLoginPage').default;
return SettingsAddSecondaryLoginPage;
const SettingsNewContactMethodPage = require('../../../pages/settings/Profile/Contacts/NewContactMethodPage').default;
return SettingsNewContactMethodPage;
},
name: 'Settings_Add_Secondary_Login',
name: 'Settings_NewContactMethod',
},
{
getComponent: () => {
Expand Down
5 changes: 3 additions & 2 deletions src/libs/Navigation/linkingConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ export default {
Settings_ContactMethodDetails: {
path: ROUTES.SETTINGS_CONTACT_METHOD_DETAILS,
},
Settings_Add_Secondary_Login: {
path: ROUTES.SETTINGS_ADD_LOGIN,
Settings_NewContactMethod: {
path: ROUTES.SETTINGS_NEW_CONTACT_METHOD,
exact: true,
},
Settings_PersonalDetails_Initial: {
path: ROUTES.SETTINGS_PERSONAL_DETAILS,
Expand Down
100 changes: 62 additions & 38 deletions src/libs/actions/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import lodashGet from 'lodash/get';
import Onyx from 'react-native-onyx';
import moment from 'moment';
import ONYXKEYS from '../../ONYXKEYS';
import * as DeprecatedAPI from '../deprecatedAPI';
import * as API from '../API';
import CONFIG from '../../CONFIG';
import CONST from '../../CONST';
Expand Down Expand Up @@ -182,49 +181,19 @@ function updateNewsletterSubscription(isSubscribed) {
});
}

/**
* Adds a secondary login to a user's account
*
* @param {String} login
* @param {String} password
* @returns {Promise}
*/
function setSecondaryLoginAndNavigate(login, password) {
Onyx.merge(ONYXKEYS.ACCOUNT, {...CONST.DEFAULT_ACCOUNT_DATA, isLoading: true});

return DeprecatedAPI.User_SecondaryLogin_Send({
email: login,
password,
}).then((response) => {
if (response.jsonCode === 200) {
Onyx.set(ONYXKEYS.LOGIN_LIST, response.loginList);
Navigation.navigate(ROUTES.SETTINGS_CONTACT_METHODS);
return;
}

let error = lodashGet(response, 'message', 'Unable to add secondary login. Please try again.');

// Replace error with a friendlier message
if (error.includes('already belongs to an existing Expensify account.')) {
error = 'This login already belongs to an existing Expensify account.';
}
if (error.includes('I couldn\'t validate the phone number')) {
error = Localize.translateLocal('common.error.phoneNumber');
}

Onyx.merge(ONYXKEYS.USER, {error});
}).finally(() => {
Onyx.merge(ONYXKEYS.ACCOUNT, {isLoading: false});
});
}

/**
* Delete a specific contact method
*
* @param {String} contactMethod - the contact method being deleted
* @param {Object} oldLoginData
*/
function deleteContactMethod(contactMethod, oldLoginData) {
// If the contact method failed to be added to the account, then it should only be deleted locally.
if (lodashGet(oldLoginData, 'errorFields.addedLogin', null)) {
Onyx.merge(ONYXKEYS.LOGIN_LIST, {[contactMethod]: null});
return;
}

const optimisticData = [{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: ONYXKEYS.LOGIN_LIST,
Expand Down Expand Up @@ -290,6 +259,61 @@ function clearContactMethodErrors(contactMethod, fieldName) {
});
}

/**
* Adds a secondary login to a user's account
*
* @param {String} contactMethod
* @param {String} password
*/
function addNewContactMethodAndNavigate(contactMethod, password) {
const optimisticData = [{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: ONYXKEYS.LOGIN_LIST,
value: {
[contactMethod]: {
partnerUserID: contactMethod,
validatedDate: '',
errorFields: {
addedLogin: null,
},
pendingFields: {
addedLogin: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
},
},
},
}];
const successData = [{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: ONYXKEYS.LOGIN_LIST,
value: {
[contactMethod]: {
pendingFields: {
addedLogin: null,
},
},
},
}];
const failureData = [{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: ONYXKEYS.LOGIN_LIST,
value: {
[contactMethod]: {
errorFields: {
addedLogin: {
[DateUtils.getMicroseconds()]: Localize.translateLocal('contacts.genericFailureMessages.addContactMethod'),
},
},
pendingFields: {
addedLogin: null,
},
},
},
}];

API.write('AddNewContactMethod', {partnerUserID: contactMethod, password}, {optimisticData, successData, failureData});
Navigation.navigate(ROUTES.SETTINGS_CONTACT_METHODS);
}

/**
* Validates a login given an accountID and validation code
*
Expand Down Expand Up @@ -660,9 +684,9 @@ export {
resendValidateCode,
requestContactMethodValidateCode,
updateNewsletterSubscription,
setSecondaryLoginAndNavigate,
deleteContactMethod,
clearContactMethodErrors,
addNewContactMethodAndNavigate,
validateLogin,
validateSecondaryLogin,
isBlockedFromConcierge,
Expand Down
Loading

0 comments on commit 5df7225

Please sign in to comment.