Add custom country code

This commit is contained in:
Liam Cardenas 2018-01-17 21:09:08 -08:00
parent faa2753e35
commit a34a61bce8
7 changed files with 77 additions and 13 deletions

View file

@ -16,7 +16,7 @@ const select = state => ({
const perform = dispatch => ({ const perform = dispatch => ({
addUserEmail: email => dispatch(doUserEmailNew(email)), addUserEmail: email => dispatch(doUserEmailNew(email)),
addUserPhone: phone => dispatch(doUserPhoneNew(phone)), addUserPhone: (phone, country_code) => dispatch(doUserPhoneNew(phone, country_code)),
}); });
export default connect(select, perform)(UserFieldNew); export default connect(select, perform)(UserFieldNew);

View file

@ -8,19 +8,53 @@ class UserFieldNew extends React.PureComponent {
this.state = { this.state = {
phone: '', phone: '',
email: '', email: '',
country_code: '+1',
}; };
this.formatPhone = this.formatPhone.bind(this);
}
formatPhone(value) {
const { country_code } = this.state;
value = value.replace(/\D/g, '');
if (country_code === '+1') {
if (!value) {
return '';
} else if (value.length < 4) {
return value;
} else if (value.length < 7) {
return `(${value.substring(0, 3)}) ${value.substring(3)}`;
}
const fullNumber = `(${value.substring(0, 3)}) ${value.substring(3, 6)}-${value.substring(
6
)}`;
return fullNumber.length <= 14 ? fullNumber : fullNumber.substring(0, 14);
}
return value;
}
formatCountryCode(value) {
if (value) {
return `+${value.replace(/\D/g, '')}`;
}
return '+1';
} }
handleChanged(event, fieldType) { handleChanged(event, fieldType) {
const formatter = {
email: _ => _,
phone: this.formatPhone,
country_code: this.formatCountryCode,
};
this.setState({ this.setState({
[fieldType]: event.target.value, [fieldType]: formatter[fieldType](event.target.value),
}); });
} }
handleSubmit() { handleSubmit() {
const { email, phone } = this.state; const { email, phone, country_code } = this.state;
if (phone) { if (phone) {
this.props.addUserPhone(phone); this.props.addUserPhone(phone.replace(/\D/g, ''), country_code.replace(/\D/g, ''));
} else { } else {
this.props.addUserEmail(email); this.props.addUserEmail(email);
} }
@ -37,10 +71,20 @@ class UserFieldNew extends React.PureComponent {
)} )}
</p> </p>
<Form onSubmit={this.handleSubmit.bind(this)}> <Form onSubmit={this.handleSubmit.bind(this)}>
<FormRow
type="text"
label="Country Code"
name="country_code"
value={this.state.country_code}
errorMessage={phoneErrorMessage}
onChange={event => {
this.handleChanged(event, 'country_code');
}}
/>
<FormRow <FormRow
type="text" type="text"
label="Phone" label="Phone"
placeholder="(555) 555-5555" placeholder={this.state.country_code === '+1' ? '(555) 555-5555' : '5555555555'}
name="phone" name="phone"
value={this.state.phone} value={this.state.phone}
errorMessage={phoneErrorMessage} errorMessage={phoneErrorMessage}

View file

@ -7,6 +7,7 @@ import {
selectPhoneToVerify, selectPhoneToVerify,
selectEmailVerifyErrorMessage, selectEmailVerifyErrorMessage,
selectPhoneVerifyErrorMessage, selectPhoneVerifyErrorMessage,
selectUserCountryCode,
} from 'redux/selectors/user'; } from 'redux/selectors/user';
import UserFieldVerify from './view'; import UserFieldVerify from './view';
@ -14,6 +15,7 @@ const select = state => ({
isPending: selectEmailVerifyIsPending(state), isPending: selectEmailVerifyIsPending(state),
email: selectEmailToVerify(state), email: selectEmailToVerify(state),
phone: selectPhoneToVerify(state), phone: selectPhoneToVerify(state),
countryCode: selectUserCountryCode(state),
emailErrorMessage: selectEmailVerifyErrorMessage(state), emailErrorMessage: selectEmailVerifyErrorMessage(state),
phoneErrorMessage: selectPhoneVerifyErrorMessage(state), phoneErrorMessage: selectPhoneVerifyErrorMessage(state),
}); });

View file

@ -40,10 +40,14 @@ class UserFieldVerify extends React.PureComponent {
email, email,
isPending, isPending,
phone, phone,
countryCode,
} = this.props; } = this.props;
return ( return (
<Form onSubmit={this.handleSubmit.bind(this)}> <Form onSubmit={this.handleSubmit.bind(this)}>
<p>Please enter the verification code sent to {phone || email}.</p> <p>
Please enter the verification code sent to {countryCode ? `+${countryCode}` : ''}
{phone || email}.
</p>
<FormRow <FormRow
type="text" type="text"
label={__('Verification Code')} label={__('Verification Code')}

View file

@ -3,7 +3,11 @@ import * as MODALS from 'constants/modal_types';
import Lbryio from 'lbryio'; import Lbryio from 'lbryio';
import { doOpenModal, doShowSnackBar } from 'redux/actions/app'; import { doOpenModal, doShowSnackBar } from 'redux/actions/app';
import { doClaimRewardType, doRewardList } from 'redux/actions/rewards'; import { doClaimRewardType, doRewardList } from 'redux/actions/rewards';
import { selectEmailToVerify, selectPhoneToVerify } from 'redux/selectors/user'; import {
selectEmailToVerify,
selectPhoneToVerify,
selectUserCountryCode,
} from 'redux/selectors/user';
import rewards from 'rewards'; import rewards from 'rewards';
export function doFetchInviteStatus() { export function doFetchInviteStatus() {
@ -78,11 +82,11 @@ export function doUserFetch() {
}; };
} }
export function doUserPhoneNew(phone) { export function doUserPhoneNew(phone, country_code) {
return dispatch => { return dispatch => {
dispatch({ dispatch({
type: ACTIONS.USER_PHONE_NEW_STARTED, type: ACTIONS.USER_PHONE_NEW_STARTED,
phone, data: { phone, country_code },
}); });
const success = () => { const success = () => {
@ -99,7 +103,7 @@ export function doUserPhoneNew(phone) {
}); });
}; };
Lbryio.call('user', 'phone_number_new', { phone_number: phone, country_code: 1 }, 'post').then( Lbryio.call('user', 'phone_number_new', { phone_number: phone, country_code }, 'post').then(
success, success,
failure failure
); );
@ -116,6 +120,7 @@ export function doUserPhoneVerifyFailure(error) {
export function doUserPhoneVerify(verificationCode) { export function doUserPhoneVerify(verificationCode) {
return (dispatch, getState) => { return (dispatch, getState) => {
const phoneNumber = selectPhoneToVerify(getState()); const phoneNumber = selectPhoneToVerify(getState());
const countryCode = selectUserCountryCode(getState());
dispatch({ dispatch({
type: ACTIONS.USER_PHONE_VERIFY_STARTED, type: ACTIONS.USER_PHONE_VERIFY_STARTED,
@ -128,7 +133,7 @@ export function doUserPhoneVerify(verificationCode) {
{ {
verification_code: verificationCode, verification_code: verificationCode,
phone_number: phoneNumber, phone_number: phoneNumber,
country_code: '1', country_code: countryCode,
}, },
'post' 'post'
) )

View file

@ -55,11 +55,15 @@ reducers[ACTIONS.USER_FETCH_FAILURE] = state =>
user: null, user: null,
}); });
reducers[ACTIONS.USER_PHONE_NEW_STARTED] = state => reducers[ACTIONS.USER_PHONE_NEW_STARTED] = (state, action) => {
Object.assign({}, state, { const user = Object.assign({}, state.user);
user.country_code = action.data.country_code;
return Object.assign({}, state, {
phoneNewIsPending: true, phoneNewIsPending: true,
phoneNewErrorMessage: '', phoneNewErrorMessage: '',
user,
}); });
};
reducers[ACTIONS.USER_PHONE_NEW_SUCCESS] = (state, action) => reducers[ACTIONS.USER_PHONE_NEW_SUCCESS] = (state, action) =>
Object.assign({}, state, { Object.assign({}, state, {

View file

@ -21,6 +21,11 @@ export const selectUserPhone = createSelector(
user => (user ? user.phone_number : null) user => (user ? user.phone_number : null)
); );
export const selectUserCountryCode = createSelector(
selectUser,
user => (user ? user.country_code : null)
);
export const selectEmailToVerify = createSelector( export const selectEmailToVerify = createSelector(
selectState, selectState,
selectUserEmail, selectUserEmail,