Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NEW] Custom fields on signup #1013

Merged
merged 6 commits into from
Jun 28, 2019
Merged
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
118 changes: 106 additions & 12 deletions app/views/RegisterView.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
} from 'react-native';
import { connect } from 'react-redux';
import { SafeAreaView } from 'react-navigation';
import RNPickerSelect from 'react-native-picker-select';
import equal from 'deep-equal';

import TextInput from '../containers/TextInput';
import Button from '../containers/Button';
Expand All @@ -17,10 +19,13 @@ import { loginRequest as loginRequestAction } from '../actions/login';
import isValidEmail from '../utils/isValidEmail';
import { LegalButton } from '../containers/HeaderButton';
import StatusBar from '../containers/StatusBar';
import log from '../utils/log';

const shouldUpdateState = ['name', 'email', 'password', 'username', 'saving'];

@connect(null, dispatch => ({
@connect(state => ({
Accounts_CustomFields: state.settings.Accounts_CustomFields
}), dispatch => ({
loginRequest: params => dispatch(loginRequestAction(params))
}))
export default class RegisterView extends React.Component {
Expand All @@ -35,15 +40,34 @@ export default class RegisterView extends React.Component {
static propTypes = {
navigation: PropTypes.object,
loginRequest: PropTypes.func,
Site_Name: PropTypes.string
Site_Name: PropTypes.string,
Accounts_CustomFields: PropTypes.string
}

state = {
name: '',
email: '',
password: '',
username: '',
saving: false
constructor(props) {
super(props);
const customFields = {};
this.parsedCustomFields = {};
if (props.Accounts_CustomFields) {
try {
this.parsedCustomFields = JSON.parse(props.Accounts_CustomFields);
} catch (e) {
log('err_parsing_account_custom_fields', e);
}
}
Object.keys(this.parsedCustomFields).forEach((key) => {
if (this.parsedCustomFields[key].defaultValue) {
customFields[key] = this.parsedCustomFields[key].defaultValue;
}
});
this.state = {
name: '',
email: '',
password: '',
username: '',
saving: false,
customFields
};
}

componentDidMount() {
Expand All @@ -53,6 +77,10 @@ export default class RegisterView extends React.Component {
}

shouldComponentUpdate(nextProps, nextState) {
const { customFields } = this.state;
if (!equal(nextState.customFields, customFields)) {
return true;
}
// eslint-disable-next-line react/destructuring-assignment
return shouldUpdateState.some(key => nextState[key] !== this.state[key]);
}
Expand All @@ -77,9 +105,15 @@ export default class RegisterView extends React.Component {

valid = () => {
const {
name, email, password, username
name, email, password, username, customFields
} = this.state;
return name.trim() && email.trim() && password.trim() && username.trim() && isValidEmail(email);
let requiredCheck = true;
Object.keys(this.parsedCustomFields).forEach((key) => {
if (this.parsedCustomFields[key].required) {
requiredCheck = requiredCheck && customFields[key] && Boolean(customFields[key].trim());
}
});
return name.trim() && email.trim() && password.trim() && username.trim() && isValidEmail(email) && requiredCheck;
}

submit = async() => {
Expand All @@ -90,13 +124,13 @@ export default class RegisterView extends React.Component {
Keyboard.dismiss();

const {
name, email, password, username
name, email, password, username, customFields
} = this.state;
const { loginRequest } = this.props;

try {
await RocketChat.register({
name, email, pass: password, username
name, email, pass: password, username, ...customFields
});
await loginRequest({ user: email, password });
} catch (e) {
Expand All @@ -105,6 +139,64 @@ export default class RegisterView extends React.Component {
this.setState({ saving: false });
}

renderCustomFields = () => {
const { customFields } = this.state;
const { Accounts_CustomFields } = this.props;
if (!Accounts_CustomFields) {
return null;
}
try {
return Object.keys(this.parsedCustomFields).map((key, index, array) => {
if (this.parsedCustomFields[key].type === 'select') {
const options = this.parsedCustomFields[key].options.map(option => ({ label: option, value: option }));
return (
<RNPickerSelect
key={key}
items={options}
onValueChange={(value) => {
const newValue = {};
newValue[key] = value;
this.setState({ customFields: { ...customFields, ...newValue } });
}}
value={customFields[key]}
>
<TextInput
inputRef={(e) => { this[key] = e; }}
placeholder={key}
value={customFields[key]}
iconLeft='flag'
testID='register-view-custom-picker'
/>
</RNPickerSelect>
);
}

return (
<TextInput
inputRef={(e) => { this[key] = e; }}
key={key}
placeholder={key}
value={customFields[key]}
iconLeft='flag'
onChangeText={(value) => {
const newValue = {};
newValue[key] = value;
this.setState({ customFields: { ...customFields, ...newValue } });
}}
onSubmitEditing={() => {
if (array.length - 1 > index) {
return this[array[index + 1]].focus();
}
this.avatarUrl.focus();
}}
/>
);
});
} catch (error) {
return null;
}
}

render() {
const { saving } = this.state;
return (
Expand Down Expand Up @@ -153,6 +245,8 @@ export default class RegisterView extends React.Component {
containerStyle={sharedStyles.inputLastChild}
/>

{this.renderCustomFields()}

<Button
title={I18n.t('Register')}
type='primary'
Expand Down