2017-11-08 12:22:03 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* PopUpForm
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from 'reactstrap';
|
|
|
|
import { FormattedMessage } from 'react-intl';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-01-24 13:02:16 +01:00
|
|
|
import {
|
|
|
|
capitalize,
|
|
|
|
get,
|
|
|
|
findIndex,
|
|
|
|
isArray,
|
|
|
|
isEmpty,
|
|
|
|
isObject,
|
|
|
|
includes,
|
|
|
|
map,
|
|
|
|
startsWith,
|
|
|
|
tail,
|
|
|
|
take,
|
|
|
|
takeRight,
|
|
|
|
} from 'lodash';
|
2018-01-19 13:34:55 +01:00
|
|
|
|
|
|
|
// Translations
|
|
|
|
import en from 'translations/en.json';
|
2017-11-08 12:22:03 +01:00
|
|
|
|
|
|
|
import Input from 'components/Input';
|
|
|
|
|
|
|
|
import styles from './styles.scss';
|
|
|
|
|
|
|
|
class PopUpForm extends React.Component { // eslint-disable-line react/prefer-stateless-function
|
2018-01-24 13:02:16 +01:00
|
|
|
state = { enabled: false, isEditing: false };
|
|
|
|
|
2018-01-24 19:12:13 +01:00
|
|
|
getRedirectURIProviderConf = () => {
|
|
|
|
switch (this.props.dataToEdit) {
|
|
|
|
case 'facebook':
|
|
|
|
return `${strapi.backendURL}/connect/facebook/callback`;
|
|
|
|
case 'github':
|
|
|
|
return get(this.props.values, 'redirect_uri', '');
|
|
|
|
default: {
|
|
|
|
const value = get(this.props.values, 'callback', '');
|
|
|
|
|
|
|
|
return startsWith(value, 'http') ? value : `${strapi.backendURL}${value}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-24 13:02:16 +01:00
|
|
|
generateRedirectURL = (url) => {
|
|
|
|
return startsWith(url, 'https://') || startsWith(url, 'http://') || this.state.isEditing ? url : `${strapi.backendURL}${startsWith(url, '/') ? '' : '/'}${url}`;
|
|
|
|
}
|
2018-01-19 17:16:14 +01:00
|
|
|
|
|
|
|
handleChange = (e) => {
|
|
|
|
this.setState({ enabled: e.target.value });
|
|
|
|
this.props.onChange(e);
|
|
|
|
}
|
|
|
|
|
2018-01-24 13:02:16 +01:00
|
|
|
handleBlur = (e) => {
|
|
|
|
this.setState({ isEditing: false });
|
|
|
|
|
|
|
|
if (isEmpty(e.target.value)) {
|
|
|
|
const { name, type } = e.target;
|
|
|
|
const target = Object.assign({ name, type }, { value: `/auth/${this.props.dataToEdit}/callback` });
|
|
|
|
this.props.onChange({ target });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleFocus = () => this.setState({ isEditing: true });
|
|
|
|
|
2017-11-08 12:22:03 +01:00
|
|
|
renderForm = () => {
|
2018-01-19 17:16:14 +01:00
|
|
|
const { dataToEdit, settingType, values } = this.props;
|
|
|
|
const form = Object.keys(values.options || values || {}).reduce((acc, current) => {
|
|
|
|
const path = settingType === 'email-templates' ? ['options', current] : [ current ];
|
|
|
|
const name = settingType === 'email-templates' ? 'options.' : '';
|
2018-01-19 13:34:55 +01:00
|
|
|
|
2018-01-19 17:16:14 +01:00
|
|
|
if (isObject(get(values, path)) && !isArray(get(values, path))) {
|
|
|
|
return Object.keys(get(values, path, {}))
|
|
|
|
.reduce((acc, curr) => {
|
|
|
|
acc.push(`${name}${current}.${curr}`);
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, []).concat(acc);
|
|
|
|
} else if (current !== 'icon' && current !== 'scope'){
|
|
|
|
acc.push(`${name}${current}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
if (settingType === 'providers') {
|
2017-11-08 12:22:03 +01:00
|
|
|
return (
|
|
|
|
<div className="row">
|
|
|
|
<Input
|
2018-01-19 17:16:14 +01:00
|
|
|
inputDescription="users-permissions.PopUpForm.Providers.enabled.description"
|
|
|
|
label="users-permissions.PopUpForm.Providers.enabled.label"
|
|
|
|
name={`${dataToEdit}.enabled`}
|
|
|
|
onChange={this.handleChange}
|
2017-11-08 12:22:03 +01:00
|
|
|
type="toggle"
|
|
|
|
validations={{}}
|
2018-01-19 17:16:14 +01:00
|
|
|
value={get(this.props.values, 'enabled', this.state.enabled)}
|
2017-11-08 12:22:03 +01:00
|
|
|
/>
|
2018-01-22 13:10:58 +01:00
|
|
|
{form.length > 1 ? (
|
|
|
|
<div className={styles.separator} />
|
|
|
|
|
|
|
|
) : ''}
|
2018-01-19 17:16:14 +01:00
|
|
|
{map(tail(form), (value, key) => (
|
|
|
|
<Input
|
|
|
|
autoFocus={key === 0}
|
2018-01-22 12:05:22 +01:00
|
|
|
customBootstrapClass="col-md-12"
|
|
|
|
didCheckErrors={this.props.didCheckErrors}
|
|
|
|
errors={get(this.props.formErrors, [findIndex(this.props.formErrors, ['name', value]), 'errors'], [])}
|
2018-01-19 17:16:14 +01:00
|
|
|
key={value}
|
2018-01-24 19:12:13 +01:00
|
|
|
label={`users-permissions.PopUpForm.Providers.${ includes(value, 'callback') || includes(value, 'redirect_uri') ? 'redirectURL.front-end' : value}.label`}
|
2018-01-19 17:16:14 +01:00
|
|
|
name={`${dataToEdit}.${value}`}
|
2018-01-24 13:02:16 +01:00
|
|
|
onFocus={includes(value, 'callback') || includes(value, 'redirect_uri') ? this.handleFocus : () => {}}
|
|
|
|
onBlur={includes(value, 'callback') || includes(value, 'redirect_uri') ? this.handleBlur : false}
|
2018-01-19 17:16:14 +01:00
|
|
|
onChange={this.props.onChange}
|
|
|
|
type="text"
|
2018-01-24 13:02:16 +01:00
|
|
|
value={includes(value, 'callback') || includes(value, 'redirect_uri') ? this.generateRedirectURL(get(values, value)) : get(values, value)}
|
2018-01-22 12:05:22 +01:00
|
|
|
validations={{ required: true }}
|
2018-01-19 17:16:14 +01:00
|
|
|
/>
|
|
|
|
))}
|
2018-01-24 19:12:13 +01:00
|
|
|
{ dataToEdit !== 'email' ? (
|
|
|
|
<Input
|
|
|
|
customBootstrapClass="col-md-12"
|
|
|
|
disabled
|
|
|
|
label={`users-permissions.PopUpForm.Providers.${dataToEdit}.providerConfig.redirectURL`}
|
|
|
|
name="noName"
|
|
|
|
type="text"
|
|
|
|
onChange={() => {}}
|
|
|
|
value={this.getRedirectURIProviderConf()}
|
|
|
|
validations={{}}
|
|
|
|
/>
|
|
|
|
) : ''}
|
2017-11-08 12:22:03 +01:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="row">
|
2018-01-19 13:34:55 +01:00
|
|
|
{map(take(form, 3), (value, key) => (
|
|
|
|
<Input
|
|
|
|
autoFocus={key === 0}
|
|
|
|
key={value}
|
2018-01-22 12:25:50 +01:00
|
|
|
didCheckErrors={this.props.didCheckErrors}
|
|
|
|
errors={get(this.props.formErrors, [findIndex(this.props.formErrors, ['name', value]), 'errors'], [])}
|
2018-01-19 13:34:55 +01:00
|
|
|
label={`users-permissions.PopUpForm.Email.${value}.label`}
|
|
|
|
name={`${dataToEdit}.${value}`}
|
|
|
|
onChange={this.props.onChange}
|
|
|
|
placeholder={`users-permissions.PopUpForm.Email.${value}.placeholder`}
|
|
|
|
type={includes(value, 'email') ? 'email' : 'text'}
|
|
|
|
value={get(values, value)}
|
2018-01-22 12:25:50 +01:00
|
|
|
validations={value !== 'options.response_email' ? { required: true } : {}}
|
2018-01-19 13:34:55 +01:00
|
|
|
/>
|
|
|
|
))}
|
2017-11-08 12:22:03 +01:00
|
|
|
<div className="col-md-6" />
|
2018-01-19 13:34:55 +01:00
|
|
|
{map(takeRight(form, 2), (value) => (
|
|
|
|
<Input
|
|
|
|
key={value}
|
|
|
|
customBootstrapClass="col-md-12"
|
2018-01-22 12:25:50 +01:00
|
|
|
didCheckErrors={this.props.didCheckErrors}
|
|
|
|
errors={get(this.props.formErrors, [findIndex(this.props.formErrors, ['name', value]), 'errors'], [])}
|
2018-01-19 13:34:55 +01:00
|
|
|
label={`users-permissions.PopUpForm.Email.${value}.label`}
|
|
|
|
name={`${dataToEdit}.${value}`}
|
2018-01-19 17:43:00 +01:00
|
|
|
inputDescription={includes(value, 'object') ? 'users-permissions.PopUpForm.Email.email_templates.inputDescription' : ''}
|
|
|
|
linkContent={includes(value, 'object') ? { link: 'https://strapi.io/documentation/plugin-development/ui-components.html', description: 'users-permissions.PopUpForm.Email.link.documentation' } : {}}
|
2018-01-19 13:34:55 +01:00
|
|
|
onChange={this.props.onChange}
|
|
|
|
placeholder={`users-permissions.PopUpForm.Email.${this.props.dataToEdit}.${value}.placeholder`}
|
|
|
|
type={includes(value, 'object') ? 'text' : 'textarea'}
|
2018-01-22 12:25:50 +01:00
|
|
|
validations={{ required: true }}
|
2018-01-19 13:34:55 +01:00
|
|
|
value={get(values, value)}
|
|
|
|
/>
|
|
|
|
))}
|
2017-11-08 12:22:03 +01:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-01-19 17:16:14 +01:00
|
|
|
const { display } = this.props.values;
|
|
|
|
const { actionType, dataToEdit, settingType } = this.props;
|
2018-01-19 13:34:55 +01:00
|
|
|
|
|
|
|
let header = <span>{dataToEdit}</span>;
|
|
|
|
|
|
|
|
if (actionType) {
|
2018-01-19 17:16:14 +01:00
|
|
|
header = <FormattedMessage id={`users-permissions.PopUpForm.header.${actionType}.${settingType}`} values={{ provider: <i>{capitalize(dataToEdit)}</i> }} />;
|
2018-01-19 13:34:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (display && en[display]) {
|
|
|
|
header = <FormattedMessage id={`users-permissions.${display}`} />;
|
|
|
|
}
|
|
|
|
|
2017-11-08 12:22:03 +01:00
|
|
|
return (
|
|
|
|
<div className={styles.popUpForm}>
|
2018-01-19 13:34:55 +01:00
|
|
|
<Modal isOpen={this.props.isOpen} toggle={this.context.unsetDataToEdit} className={`${styles.modalPosition}`}>
|
|
|
|
<ModalHeader toggle={this.context.unsetDataToEdit} className={styles.modalHeader} />
|
2017-11-08 12:22:03 +01:00
|
|
|
<div className={styles.headerContainer}>
|
|
|
|
<div>
|
2018-01-19 13:34:55 +01:00
|
|
|
{header}
|
2017-11-08 12:22:03 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
2017-11-08 16:06:21 +01:00
|
|
|
<form onSubmit={this.props.onSubmit}>
|
|
|
|
<ModalBody className={styles.modalBody}>
|
|
|
|
<div className="container-fluid">
|
|
|
|
{this.renderForm()}
|
|
|
|
</div>
|
|
|
|
</ModalBody>
|
|
|
|
<ModalFooter className={styles.modalFooter}>
|
2018-01-19 13:34:55 +01:00
|
|
|
<Button onClick={() => this.context.unsetDataToEdit()} className={styles.secondary}>
|
2017-11-23 18:08:14 +01:00
|
|
|
<FormattedMessage id="users-permissions.PopUpForm.button.cancel" />
|
2017-11-08 16:06:21 +01:00
|
|
|
</Button>
|
2018-01-22 13:10:58 +01:00
|
|
|
<Button type="submit" onClick={this.props.onSubmit} className={styles.primary}>
|
|
|
|
<FormattedMessage id="users-permissions.PopUpForm.button.save" />
|
|
|
|
</Button>
|
2017-11-08 16:06:21 +01:00
|
|
|
</ModalFooter>
|
|
|
|
</form>
|
2017-11-08 12:22:03 +01:00
|
|
|
</Modal>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-19 13:34:55 +01:00
|
|
|
PopUpForm.contextTypes = {
|
|
|
|
unsetDataToEdit: PropTypes.func.isRequired,
|
|
|
|
};
|
|
|
|
|
2017-11-08 16:06:21 +01:00
|
|
|
PopUpForm.defaultProps = {
|
|
|
|
settingType: 'providers',
|
2018-01-22 12:05:22 +01:00
|
|
|
// showLoader: false,
|
2017-11-08 16:06:21 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
PopUpForm.propTypes = {
|
2017-11-08 12:22:03 +01:00
|
|
|
actionType: PropTypes.string.isRequired,
|
2018-01-19 13:34:55 +01:00
|
|
|
dataToEdit: PropTypes.string.isRequired,
|
2018-01-22 12:05:22 +01:00
|
|
|
didCheckErrors: PropTypes.bool.isRequired,
|
|
|
|
formErrors: PropTypes.array.isRequired,
|
2017-11-08 12:22:03 +01:00
|
|
|
isOpen: PropTypes.bool.isRequired,
|
2017-11-08 16:06:21 +01:00
|
|
|
onChange: PropTypes.func.isRequired,
|
|
|
|
onSubmit: PropTypes.func.isRequired,
|
|
|
|
settingType: PropTypes.string,
|
2018-01-22 12:05:22 +01:00
|
|
|
// showLoader: PropTypes.bool,
|
2017-11-08 16:06:21 +01:00
|
|
|
values: PropTypes.object.isRequired,
|
2017-11-08 12:22:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export default PopUpForm;
|