Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions app/javascript/app/phone-internationalization.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
const INTERNATIONAL_CODE_REGEX = /^\+(\d+) |^1 /;

// @ts-ignore
const { I18n } = window.LoginGov;

const selectedInternationCodeOption = () => {
const dropdown = document.querySelector('[data-international-phone-form] .international-code');
return dropdown.item(dropdown.selectedIndex);
const dropdown = /** @type {HTMLSelectElement} */ (document.querySelector(
'[data-international-phone-form] .international-code',
));
return /** @type {HTMLOptionElement} */ (dropdown.item(dropdown.selectedIndex));
};

const unsupportedInternationalPhoneOTPDeliveryWarningMessage = () => {
Expand Down Expand Up @@ -51,14 +54,14 @@ const updateOTPDeliveryMethods = () => {
return;
}

const phoneLabel = phoneRadio.parentNode.parentNode;
const phoneLabel = /** @type {Element} */ (phoneRadio.parentNode).parentNode;
const deliveryMethodHint = document.querySelector('#otp_delivery_preference_instruction');

const warningMessage = unsupportedInternationalPhoneOTPDeliveryWarningMessage();
if (warningMessage) {
disablePhoneState(phoneRadio, phoneLabel, smsRadio, deliveryMethodHint, warningMessage);
} else {
enablePhoneState(phoneRadio, phoneLabel, smsRadio, deliveryMethodHint);
enablePhoneState(phoneRadio, phoneLabel, deliveryMethodHint);
Comment on lines -61 to +64
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI this appears to have been previously incorrect. TypeScript caught it. enablePhoneState takes three arguments.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it have been throwing JS errors when used?

Copy link
Copy Markdown
Contributor Author

@aduth aduth Jan 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it have been throwing JS errors when used?

Just by looking: It probably wouldn't throw, since the smsRadio we were passing as third argument is an HTMLInputElement, for which the innerText assignment is valid. It probably doesn't behave as we'd expect. I'll do some real-world testing to see that this works correctly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I think this code is never being reached, due to a bug with how we're populating the country code data.

The warning message is generated if there's a data-sms-only attribute:

const unsupportedInternationalPhoneOTPDeliveryWarningMessage = () => {
const selectedOption = selectedInternationCodeOption();
if (selectedOption.dataset.smsOnly === 'true') {
const messageTemplate = I18n.t(
'two_factor_authentication.otp_delivery_preference.phone_unsupported',
);
return messageTemplate.replace('%{location}', selectedOption.dataset.countryName);
}
return null;
};

We pass the data attributes via:

def international_phone_codes_data(code_data)
{
sms_only: code_data['sms_only'],
country_code: code_data['country_code'],
country_name: code_data['name'],
}
end

Which pulls from this YAML data:

AD:
country_code: '376'
name: Andorra
supports_sms: true
supports_voice: false

sms_only isn't a property. Looks like this was changed in #4331.

We probably want to update this logic, optionally pulling in PhoneNumberCapabilities#sms_only? if it makes sense.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yikes... good catch re sms_only ..... my bad

Copy link
Copy Markdown
Contributor Author

@aduth aduth Jan 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re: Wrong arguments being passed: Yes, we're assigning an innerText to the radio input field itself, which AFAICT is a noop. The problem would manifest itself if the user would select an option which cannot receive phone calls, then switch back to one which can. Normally, this should reset the hint text. Instead, it sticks with the error text:

image

(Again, due to above behavior with sms_only, this won't ever happen currently)

The same, after the changes here:

image

}
};

Expand All @@ -74,7 +77,9 @@ const updateInternationalCodeInPhone = (phone, newCode) =>
phone.replace(new RegExp(`^\\+?(\\d+\\s+|${newCode})?`), `+${newCode} `);

const updateInternationalCodeInput = () => {
const phoneInput = document.querySelector('[data-international-phone-form] .phone');
const phoneInput = /** @type {HTMLInputElement} */ (document.querySelector(
'[data-international-phone-form] .phone',
));
const phone = phoneInput.value;
const inputInternationalCode = internationalCodeFromPhone(phone);
const selectedInternationalCode = selectedInternationCodeOption().dataset.countryCode;
Expand Down
3 changes: 3 additions & 0 deletions app/javascript/app/phone-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ const updatePlaceholder = (phoneInput) => {
};

const checkPhoneValidity = () => {
/** @type {HTMLInputElement?} */
const sendCodeButton = document.querySelector(
'[data-international-phone-form] input[name=commit]',
);
/** @type {HTMLInputElement?} */
const phoneInput =
document.querySelector('[data-international-phone-form] .phone') ||
document.querySelector('[data-international-phone-form] .new-phone');
updatePlaceholder(phoneInput);
/** @type {HTMLInputElement?} */
const countryCodeInput = document.querySelector(
'[data-international-phone-form] .international-code',
);
Expand Down
2 changes: 0 additions & 2 deletions app/javascript/packs/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ require('../app/pw-toggle');
require('../app/checkbox');
require('../app/form-field-format');
require('../app/radio-btn');
require('../app/phone-internationalization');
require('../app/phone-validation');
require('../app/print-personal-key');
require('../app/i18n-dropdown');
require('../app/platform-authenticator');
Expand Down
2 changes: 2 additions & 0 deletions app/javascript/packs/intl-tel-input.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { loadPolyfills } from '@18f/identity-polyfill';
import 'intl-tel-input/build/js/utils.js';
import * as intlTelInput from 'intl-tel-input/build/js/intlTelInput';
import '../app/phone-internationalization';
import '../app/phone-validation';

/**
* @param {HTMLDivElement} iti
Expand Down