lbry-desktop/src/ui/component/userPhoneNew/view.jsx

137 lines
3.9 KiB
React
Raw Normal View History

2019-01-08 19:48:09 +01:00
// @flow
import * as React from 'react';
2019-02-13 17:27:20 +01:00
import { Form, FormField, Submit } from 'component/common/form';
2018-01-22 23:46:14 +01:00
const os = require('os').type();
const countryCodes = require('country-data')
.callingCountries.all.filter(_ => _.emoji)
.reduce(
(acc, cur) => acc.concat(cur.countryCallingCodes.map(_ => ({ ...cur, countryCallingCode: _ }))),
[]
)
.sort((a, b) => {
2018-01-22 07:30:20 +01:00
if (a.countryCallingCode < b.countryCallingCode) {
return -1;
}
2018-01-22 07:30:20 +01:00
if (a.countryCallingCode > b.countryCallingCode) {
return 1;
}
return 0;
});
2019-01-08 19:48:09 +01:00
type Props = {
addUserPhone: (string, string) => void,
cancelButton: React.Node,
phoneErrorMessage: ?string,
isPending: boolean,
};
type State = {
phone: string,
countryCode: string,
};
class UserPhoneNew extends React.PureComponent<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
2018-01-15 14:32:01 +01:00
phone: '',
2019-01-08 19:48:09 +01:00
countryCode: '+1',
};
2018-01-18 06:09:08 +01:00
2019-01-08 19:48:09 +01:00
(this: any).formatPhone = this.formatPhone.bind(this);
(this: any).handleSubmit = this.handleSubmit.bind(this);
(this: any).handleSelect = this.handleSelect.bind(this);
2018-01-18 06:09:08 +01:00
}
2019-01-08 19:48:09 +01:00
formatPhone(value: string) {
const { countryCode } = this.state;
const formattedNumber = value.replace(/\D/g, '');
if (countryCode === '+1') {
if (!formattedNumber) {
2018-01-18 06:09:08 +01:00
return '';
2019-01-08 19:48:09 +01:00
} else if (formattedNumber.length < 4) {
return formattedNumber;
} else if (formattedNumber.length < 7) {
return `(${formattedNumber.substring(0, 3)}) ${formattedNumber.substring(3)}`;
2018-01-18 06:09:08 +01:00
}
2019-01-08 19:48:09 +01:00
const fullNumber = `(${formattedNumber.substring(0, 3)}) ${formattedNumber.substring(
3,
2018-01-18 06:09:08 +01:00
6
2019-01-08 19:48:09 +01:00
)}-${formattedNumber.substring(6)}`;
2018-01-18 06:09:08 +01:00
return fullNumber.length <= 14 ? fullNumber : fullNumber.substring(0, 14);
}
2019-01-08 19:48:09 +01:00
return formattedNumber;
2018-01-18 06:09:08 +01:00
}
2019-01-08 19:48:09 +01:00
handleChanged(event: SyntheticInputEvent<*>) {
this.setState({
phone: this.formatPhone(event.target.value),
});
}
2019-01-08 19:48:09 +01:00
handleSelect(event: SyntheticInputEvent<*>) {
this.setState({ countryCode: event.target.value });
}
handleSubmit() {
2019-01-08 19:48:09 +01:00
const { phone, countryCode } = this.state;
this.props.addUserPhone(phone.replace(/\D/g, ''), countryCode.substring(1));
}
render() {
const { cancelButton, phoneErrorMessage, isPending } = this.props;
return (
2019-01-08 19:48:09 +01:00
<React.Fragment>
2019-03-25 07:18:22 +01:00
<section className="card__content">
<p className="card__subtitle">
2019-01-08 19:48:09 +01:00
{__(
'Enter your phone number and we will send you a verification code. We will not share your phone number with third parties.'
)}
</p>
</section>
<Form onSubmit={this.handleSubmit}>
2019-03-25 07:18:22 +01:00
<div className="card__content">
<fieldset-group class="fieldset-group--smushed">
<FormField
label={__('Country')}
2019-03-25 07:18:22 +01:00
type="select"
name="country-codes"
onChange={this.handleSelect}
>
2019-02-13 17:27:20 +01:00
{countryCodes.map((country, index) => (
<option key={index} value={country.countryCallingCode}>
{os === 'Darwin' ? country.emoji : `(${country.alpha2})`}{' '}
{country.countryCallingCode}
</option>
))}
</FormField>
<FormField
2019-03-25 07:18:22 +01:00
type="text"
label={__('Number')}
2019-02-13 17:27:20 +01:00
placeholder={this.state.countryCode === '+1' ? '(555) 555-5555' : '5555555555'}
2019-03-25 07:18:22 +01:00
name="phone"
2019-02-13 17:27:20 +01:00
value={this.state.phone}
error={phoneErrorMessage}
onChange={event => {
this.handleChanged(event);
}}
/>
</fieldset-group>
</div>
2019-03-25 07:18:22 +01:00
<div className="card__actions">
<Submit label="Submit" disabled={isPending} />
{cancelButton}
2018-01-21 20:29:48 +01:00
</div>
2018-01-15 14:32:01 +01:00
</Form>
2019-01-08 19:48:09 +01:00
</React.Fragment>
2017-06-08 23:15:34 +02:00
);
}
}
export default UserPhoneNew;