2017-12-21 22:08:54 +01:00
|
|
|
import React from 'react';
|
|
|
|
import { Form, FormRow, Submit } from 'component/form.js';
|
2018-01-21 20:29:48 +01:00
|
|
|
import FormField from 'component/formField';
|
2017-06-02 02:51:52 +02:00
|
|
|
|
2018-01-22 07:17:06 +01:00
|
|
|
const countryCodes = require('country-data')
|
|
|
|
.callingCountries.all.filter(_ => _.emoji)
|
|
|
|
.reduce(
|
|
|
|
(acc, cur) => acc.concat(cur.countryCallingCodes.map(_ => ({ ...cur, countryCallingCode: _ }))),
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
.sort((a, b) => {
|
|
|
|
if (a.countryCallingCodes[0] < b.countryCallingCodes[0]) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (a.countryCallingCodes[0] > b.countryCallingCodes[0]) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
|
2018-01-11 07:16:03 +01:00
|
|
|
class UserFieldNew extends React.PureComponent {
|
2017-06-02 02:51:52 +02:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
2018-01-15 14:32:01 +01:00
|
|
|
phone: '',
|
2017-12-21 22:08:54 +01:00
|
|
|
email: '',
|
2018-01-18 06:09:08 +01:00
|
|
|
country_code: '+1',
|
2017-06-02 02:51:52 +02:00
|
|
|
};
|
2018-01-18 06:09:08 +01:00
|
|
|
|
|
|
|
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';
|
2017-06-02 02:51:52 +02:00
|
|
|
}
|
|
|
|
|
2018-01-15 14:32:01 +01:00
|
|
|
handleChanged(event, fieldType) {
|
2018-01-18 06:09:08 +01:00
|
|
|
const formatter = {
|
|
|
|
email: _ => _,
|
|
|
|
phone: this.formatPhone,
|
|
|
|
country_code: this.formatCountryCode,
|
|
|
|
};
|
2017-06-02 02:51:52 +02:00
|
|
|
this.setState({
|
2018-01-18 06:09:08 +01:00
|
|
|
[fieldType]: formatter[fieldType](event.target.value),
|
2017-06-02 02:51:52 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-22 07:17:06 +01:00
|
|
|
handleSelect(event) {
|
|
|
|
this.setState({ country_code: event.target.value });
|
|
|
|
}
|
|
|
|
|
2017-09-11 03:25:24 +02:00
|
|
|
handleSubmit() {
|
2018-01-18 06:09:08 +01:00
|
|
|
const { email, phone, country_code } = this.state;
|
2018-01-15 14:32:01 +01:00
|
|
|
if (phone) {
|
2018-01-22 07:17:06 +01:00
|
|
|
this.props.addUserPhone(phone.replace(/\D/g, ''), country_code.substring(1));
|
2018-01-15 14:32:01 +01:00
|
|
|
} else {
|
|
|
|
this.props.addUserEmail(email);
|
|
|
|
}
|
2017-06-02 02:51:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-01-15 15:30:10 +01:00
|
|
|
const { cancelButton, emailErrorMessage, phoneErrorMessage, isPending, fieldType } = this.props;
|
2017-06-02 02:51:52 +02:00
|
|
|
|
2018-01-15 14:32:01 +01:00
|
|
|
return fieldType === 'phone' ? (
|
|
|
|
<div>
|
|
|
|
<p>
|
|
|
|
{__(
|
|
|
|
'Enter your phone number and we will send you a verification code. We will not share your phone number with third parties.'
|
|
|
|
)}
|
|
|
|
</p>
|
|
|
|
<Form onSubmit={this.handleSubmit.bind(this)}>
|
2018-01-21 20:29:48 +01:00
|
|
|
<div className="form-row-phone">
|
2018-01-22 07:17:06 +01:00
|
|
|
<FormField type="select" onChange={this.handleSelect.bind(this)}>
|
|
|
|
{countryCodes.map(country => (
|
|
|
|
<option value={country.countryCallingCode}>
|
|
|
|
{country.emoji} {country.countryCallingCode}
|
|
|
|
</option>
|
|
|
|
))}
|
2018-01-21 20:29:48 +01:00
|
|
|
</FormField>
|
|
|
|
<FormRow
|
|
|
|
type="text"
|
|
|
|
placeholder={this.state.country_code === '+1' ? '(555) 555-5555' : '5555555555'}
|
|
|
|
name="phone"
|
|
|
|
value={this.state.phone}
|
|
|
|
errorMessage={phoneErrorMessage}
|
|
|
|
onChange={event => {
|
|
|
|
this.handleChanged(event, 'phone');
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
2018-01-15 14:32:01 +01:00
|
|
|
<div className="form-row-submit">
|
|
|
|
<Submit label="Submit" disabled={isPending} />
|
|
|
|
{cancelButton}
|
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
</div>
|
|
|
|
) : (
|
2017-12-07 19:07:30 +01:00
|
|
|
<div>
|
2017-08-26 05:21:26 +02:00
|
|
|
<p>
|
2017-12-30 00:43:51 +01:00
|
|
|
{__("We'll let you know about LBRY updates, security issues, and great new content.")}
|
2017-08-26 05:21:26 +02:00
|
|
|
</p>
|
2017-12-21 22:08:54 +01:00
|
|
|
<p>{__("We'll never sell your email, and you can unsubscribe at any time.")}</p>
|
2017-12-07 19:07:30 +01:00
|
|
|
<Form onSubmit={this.handleSubmit.bind(this)}>
|
|
|
|
<FormRow
|
|
|
|
type="text"
|
|
|
|
label="Email"
|
|
|
|
placeholder="youremail@example.org"
|
|
|
|
name="email"
|
|
|
|
value={this.state.email}
|
2018-01-15 15:30:10 +01:00
|
|
|
errorMessage={emailErrorMessage}
|
2017-12-07 19:07:30 +01:00
|
|
|
onChange={event => {
|
2018-01-15 14:32:01 +01:00
|
|
|
this.handleChanged(event, 'email');
|
2017-12-07 19:07:30 +01:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<div className="form-row-submit">
|
|
|
|
<Submit label="Submit" disabled={isPending} />
|
|
|
|
{cancelButton}
|
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
</div>
|
2017-06-08 23:15:34 +02:00
|
|
|
);
|
2017-06-02 02:51:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-11 07:16:03 +01:00
|
|
|
export default UserFieldNew;
|