2017-11-08 12:22:03 +01:00
|
|
|
/**
|
2019-04-02 17:44:25 +02:00
|
|
|
*
|
|
|
|
* PopUpForm
|
|
|
|
*
|
|
|
|
*/
|
2017-11-08 12:22:03 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
2019-04-16 16:55:53 +02:00
|
|
|
import { InputsIndex as Input } from 'strapi-helper-plugin';
|
2017-11-08 12:22:03 +01:00
|
|
|
|
2019-04-02 17:44:25 +02:00
|
|
|
// Translations
|
|
|
|
import en from '../../translations/en.json';
|
|
|
|
|
2017-11-08 12:22:03 +01:00
|
|
|
import styles from './styles.scss';
|
|
|
|
|
2019-09-13 15:17:24 +02:00
|
|
|
import { Wrapper } from './Components';
|
|
|
|
|
2019-04-02 17:44:25 +02:00
|
|
|
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 };
|
|
|
|
|
2019-08-13 11:31:10 +02:00
|
|
|
UNSAFE_componentWillReceiveProps(nextProps) {
|
2018-01-26 10:12:16 +01:00
|
|
|
const { values } = nextProps;
|
|
|
|
|
2019-04-08 19:54:30 +02:00
|
|
|
if (
|
|
|
|
get(values, 'enabled') &&
|
|
|
|
get(values, 'enabled') !== get(this.props.values, 'enabled')
|
|
|
|
) {
|
2018-01-26 10:12:16 +01:00
|
|
|
this.setState({ enabled: get(values, 'enabled') });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-02 17:44:25 +02:00
|
|
|
getRedirectURIProviderConf = () => {
|
|
|
|
// NOTE: Still testings providers so the switch statement is likely to change
|
2018-01-24 19:12:13 +01:00
|
|
|
switch (this.props.dataToEdit) {
|
2018-07-29 17:43:45 -07:00
|
|
|
case 'discord':
|
|
|
|
return `${strapi.backendURL}/connect/discord/callback`;
|
2018-01-24 19:12:13 +01:00
|
|
|
case 'facebook':
|
|
|
|
return `${strapi.backendURL}/connect/facebook/callback`;
|
2018-01-25 16:28:44 +01:00
|
|
|
case 'google':
|
|
|
|
return `${strapi.backendURL}/connect/google/callback`;
|
2018-01-24 19:12:13 +01:00
|
|
|
case 'github':
|
|
|
|
return get(this.props.values, 'redirect_uri', '');
|
2018-07-22 22:03:29 -04:00
|
|
|
case 'microsoft':
|
|
|
|
return `${strapi.backendURL}/connect/microsoft/callback`;
|
2018-11-20 16:55:20 +01:00
|
|
|
case 'twitter':
|
|
|
|
return `${strapi.backendURL}/connect/twitter/callback`;
|
2018-01-24 19:12:13 +01:00
|
|
|
default: {
|
|
|
|
const value = get(this.props.values, 'callback', '');
|
|
|
|
|
2019-04-08 19:54:30 +02:00
|
|
|
return startsWith(value, 'http')
|
|
|
|
? value
|
|
|
|
: `${strapi.backendURL}${value}`;
|
2018-01-24 19:12:13 +01:00
|
|
|
}
|
|
|
|
}
|
2019-04-02 17:44:25 +02:00
|
|
|
};
|
2018-01-24 19:12:13 +01:00
|
|
|
|
2019-04-02 17:44:25 +02:00
|
|
|
generateRedirectURL = url => {
|
2019-04-08 19:54:30 +02:00
|
|
|
return startsWith(url, 'https://') ||
|
|
|
|
startsWith(url, 'http://') ||
|
|
|
|
this.state.isEditing
|
2019-04-02 17:44:25 +02:00
|
|
|
? url
|
|
|
|
: `${strapi.backendURL}${startsWith(url, '/') ? '' : '/'}${url}`;
|
|
|
|
};
|
2018-01-19 17:16:14 +01:00
|
|
|
|
2019-04-02 17:44:25 +02:00
|
|
|
handleChange = e => {
|
2018-01-19 17:16:14 +01:00
|
|
|
this.setState({ enabled: e.target.value });
|
|
|
|
this.props.onChange(e);
|
2019-04-02 17:44:25 +02:00
|
|
|
};
|
2018-01-19 17:16:14 +01:00
|
|
|
|
2019-04-02 17:44:25 +02:00
|
|
|
handleBlur = e => {
|
2018-01-24 13:02:16 +01:00
|
|
|
this.setState({ isEditing: false });
|
|
|
|
|
|
|
|
if (isEmpty(e.target.value)) {
|
|
|
|
const { name, type } = e.target;
|
2019-04-02 17:44:25 +02:00
|
|
|
const target = Object.assign(
|
|
|
|
{ name, type },
|
2019-07-02 08:37:41 +02:00
|
|
|
{ value: `/auth/${this.props.dataToEdit}/callback` }
|
2019-04-02 17:44:25 +02:00
|
|
|
);
|
2018-01-24 13:02:16 +01:00
|
|
|
this.props.onChange({ target });
|
|
|
|
}
|
2019-04-02 17:44:25 +02:00
|
|
|
};
|
2018-01-24 13:02:16 +01:00
|
|
|
|
|
|
|
handleFocus = () => this.setState({ isEditing: true });
|
|
|
|
|
2017-11-08 12:22:03 +01:00
|
|
|
renderForm = () => {
|
2019-04-08 19:54:30 +02:00
|
|
|
const {
|
|
|
|
dataToEdit,
|
|
|
|
didCheckErrors,
|
|
|
|
formErrors,
|
|
|
|
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.' : '';
|
|
|
|
|
|
|
|
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;
|
|
|
|
},
|
2019-07-02 08:37:41 +02:00
|
|
|
[]
|
2019-04-08 19:54:30 +02:00
|
|
|
);
|
2018-01-19 17:16:14 +01:00
|
|
|
|
|
|
|
if (settingType === 'providers') {
|
2017-11-08 12:22:03 +01:00
|
|
|
return (
|
2018-01-25 17:16:41 +01:00
|
|
|
<div className={`row ${styles.providerDisabled}`}>
|
2017-11-08 12:22:03 +01:00
|
|
|
<Input
|
2019-04-08 19:54:30 +02:00
|
|
|
inputDescription={{
|
|
|
|
id: 'users-permissions.PopUpForm.Providers.enabled.description',
|
|
|
|
}}
|
|
|
|
label={{
|
|
|
|
id: 'users-permissions.PopUpForm.Providers.enabled.label',
|
|
|
|
}}
|
2018-06-14 11:35:40 +02:00
|
|
|
name={`${settingType}.${dataToEdit}.enabled`}
|
2018-01-19 17:16:14 +01:00
|
|
|
onChange={this.handleChange}
|
2019-04-08 19:54:30 +02:00
|
|
|
type="toggle"
|
2017-11-08 12:22:03 +01:00
|
|
|
validations={{}}
|
2019-04-08 19:54:30 +02:00
|
|
|
value={get(values, 'enabled', this.state.enabled)}
|
2017-11-08 12:22:03 +01:00
|
|
|
/>
|
2018-01-26 18:04:39 +01:00
|
|
|
|
2019-04-02 17:44:25 +02:00
|
|
|
{form.length > 1 && <div className={styles.separator} />}
|
2018-01-22 13:10:58 +01:00
|
|
|
|
2018-01-19 17:16:14 +01:00
|
|
|
{map(tail(form), (value, key) => (
|
|
|
|
<Input
|
|
|
|
autoFocus={key === 0}
|
2019-04-08 19:54:30 +02:00
|
|
|
customBootstrapClass="col-md-12"
|
|
|
|
didCheckErrors={didCheckErrors}
|
2019-04-02 17:44:25 +02:00
|
|
|
errors={get(
|
2019-04-08 19:54:30 +02:00
|
|
|
formErrors,
|
|
|
|
[findIndex(formErrors, ['name', value]), 'errors'],
|
2019-07-02 08:37:41 +02:00
|
|
|
[]
|
2019-04-02 17:44:25 +02:00
|
|
|
)}
|
2018-01-19 17:16:14 +01:00
|
|
|
key={value}
|
2019-04-02 17:44:25 +02:00
|
|
|
label={{
|
|
|
|
id: `users-permissions.PopUpForm.Providers.${
|
|
|
|
includes(value, 'callback') || includes(value, 'redirect_uri')
|
|
|
|
? 'redirectURL.front-end'
|
|
|
|
: value
|
|
|
|
}.label`,
|
|
|
|
}}
|
2018-06-14 11:35:40 +02:00
|
|
|
name={`${settingType}.${dataToEdit}.${value}`}
|
2019-04-02 17:44:25 +02: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}
|
2019-04-08 19:54:30 +02:00
|
|
|
type="text"
|
2019-04-02 17:44:25 +02: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
|
|
|
/>
|
|
|
|
))}
|
2019-04-02 17:44:25 +02:00
|
|
|
{dataToEdit !== 'email' && (
|
2018-01-24 19:12:13 +01:00
|
|
|
<Input
|
2019-04-08 19:54:30 +02:00
|
|
|
customBootstrapClass="col-md-12"
|
2018-01-24 19:12:13 +01:00
|
|
|
disabled
|
2019-04-02 17:44:25 +02:00
|
|
|
label={{
|
|
|
|
id: `users-permissions.PopUpForm.Providers.${dataToEdit}.providerConfig.redirectURL`,
|
|
|
|
}}
|
2019-04-08 19:54:30 +02:00
|
|
|
name="noName"
|
|
|
|
type="text"
|
2018-01-24 19:12:13 +01:00
|
|
|
onChange={() => {}}
|
|
|
|
value={this.getRedirectURIProviderConf()}
|
|
|
|
validations={{}}
|
|
|
|
/>
|
2018-01-25 17:16:41 +01:00
|
|
|
)}
|
2017-11-08 12:22:03 +01:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-02-05 17:41:49 +01:00
|
|
|
const params = {
|
|
|
|
link: (
|
2019-04-02 17:44:25 +02:00
|
|
|
<a
|
2019-04-08 19:54:30 +02:00
|
|
|
href="https://github.com/strapi/strapi/blob/master/packages/strapi-plugin-users-permissions/docs/email-templates.md"
|
|
|
|
target="_blank"
|
2019-07-02 08:37:41 +02:00
|
|
|
rel="noopener noreferrer"
|
2019-04-02 17:44:25 +02:00
|
|
|
>
|
2019-04-08 19:54:30 +02:00
|
|
|
<FormattedMessage id="users-permissions.PopUpForm.Email.link.documentation" />
|
2018-02-05 17:41:49 +01:00
|
|
|
</a>
|
|
|
|
),
|
|
|
|
};
|
|
|
|
|
2017-11-08 12:22:03 +01:00
|
|
|
return (
|
2019-04-08 19:54:30 +02:00
|
|
|
<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}
|
2019-04-02 17:44:25 +02:00
|
|
|
errors={get(
|
|
|
|
this.props.formErrors,
|
|
|
|
[findIndex(this.props.formErrors, ['name', value]), 'errors'],
|
2019-07-02 08:37:41 +02:00
|
|
|
[]
|
2019-04-02 17:44:25 +02:00
|
|
|
)}
|
2018-02-05 17:41:49 +01:00
|
|
|
label={{ id: `users-permissions.PopUpForm.Email.${value}.label` }}
|
2018-06-14 11:35:40 +02:00
|
|
|
name={`${settingType}.${dataToEdit}.${value}`}
|
2018-01-19 13:34:55 +01:00
|
|
|
onChange={this.props.onChange}
|
|
|
|
placeholder={`users-permissions.PopUpForm.Email.${value}.placeholder`}
|
|
|
|
type={includes(value, 'email') ? 'email' : 'text'}
|
|
|
|
value={get(values, value)}
|
2019-04-08 19:54:30 +02:00
|
|
|
validations={
|
|
|
|
value !== 'options.response_email' ? { required: true } : {}
|
|
|
|
}
|
2018-01-19 13:34:55 +01:00
|
|
|
/>
|
|
|
|
))}
|
2019-04-08 19:54:30 +02:00
|
|
|
<div className="col-md-6" />
|
2019-04-02 17:44:25 +02:00
|
|
|
{map(takeRight(form, 2), value => (
|
2018-01-19 13:34:55 +01:00
|
|
|
<Input
|
|
|
|
key={value}
|
2019-04-08 19:54:30 +02:00
|
|
|
customBootstrapClass="col-md-12"
|
2018-01-22 12:25:50 +01:00
|
|
|
didCheckErrors={this.props.didCheckErrors}
|
2019-04-02 17:44:25 +02:00
|
|
|
errors={get(
|
|
|
|
this.props.formErrors,
|
|
|
|
[findIndex(this.props.formErrors, ['name', value]), 'errors'],
|
2019-07-02 08:37:41 +02:00
|
|
|
[]
|
2019-04-02 17:44:25 +02:00
|
|
|
)}
|
2018-02-05 17:41:49 +01:00
|
|
|
label={{ id: `users-permissions.PopUpForm.Email.${value}.label` }}
|
2018-06-14 11:35:40 +02:00
|
|
|
name={`${settingType}.${dataToEdit}.${value}`}
|
2018-02-05 17:41:49 +01:00
|
|
|
inputDescription={{
|
2019-04-02 17:44:25 +02:00
|
|
|
id: includes(value, 'object')
|
|
|
|
? 'users-permissions.PopUpForm.Email.email_templates.inputDescription'
|
|
|
|
: '',
|
2018-02-05 17:41:49 +01:00
|
|
|
params,
|
|
|
|
}}
|
2018-01-19 13:34:55 +01:00
|
|
|
onChange={this.props.onChange}
|
2019-07-02 15:09:59 +02:00
|
|
|
placeholder={`users-permissions.PopUpForm.Email.${this.props.dataToEdit}.${value}.placeholder`}
|
2018-01-19 13:34:55 +01:00
|
|
|
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)}
|
2018-02-05 17:41:49 +01:00
|
|
|
inputStyle={!includes(value, 'object') ? { height: '16rem' } : {}}
|
2018-01-19 13:34:55 +01:00
|
|
|
/>
|
|
|
|
))}
|
2017-11-08 12:22:03 +01:00
|
|
|
</div>
|
|
|
|
);
|
2019-04-02 17:44:25 +02:00
|
|
|
};
|
2017-11-08 12:22:03 +01:00
|
|
|
|
|
|
|
render() {
|
2018-01-19 17:16:14 +01:00
|
|
|
const { display } = this.props.values;
|
2019-09-13 15:17:24 +02:00
|
|
|
const {
|
|
|
|
actionType,
|
|
|
|
dataToEdit,
|
|
|
|
isOpen,
|
|
|
|
onSubmit,
|
|
|
|
settingType,
|
|
|
|
} = this.props;
|
2018-01-19 13:34:55 +01:00
|
|
|
|
|
|
|
let header = <span>{dataToEdit}</span>;
|
|
|
|
|
|
|
|
if (actionType) {
|
2019-04-02 17:44:25 +02: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}`} />;
|
|
|
|
}
|
|
|
|
|
2019-09-13 15:17:24 +02:00
|
|
|
console.log(actionType);
|
|
|
|
|
2017-11-08 12:22:03 +01:00
|
|
|
return (
|
2019-09-13 15:17:24 +02:00
|
|
|
<Wrapper>
|
2019-04-02 17:44:25 +02:00
|
|
|
<Modal
|
2019-09-13 15:17:24 +02:00
|
|
|
isOpen={isOpen}
|
2019-04-02 17:44:25 +02:00
|
|
|
toggle={this.context.unsetDataToEdit}
|
|
|
|
className={`${styles.modalPosition}`}
|
|
|
|
>
|
2019-04-08 19:54:30 +02:00
|
|
|
<ModalHeader
|
|
|
|
toggle={this.context.unsetDataToEdit}
|
|
|
|
className={styles.modalHeader}
|
|
|
|
/>
|
2017-11-08 12:22:03 +01:00
|
|
|
<div className={styles.headerContainer}>
|
2019-04-02 17:44:25 +02:00
|
|
|
<div>{header}</div>
|
2017-11-08 12:22:03 +01:00
|
|
|
</div>
|
2019-09-13 15:17:24 +02:00
|
|
|
<form onSubmit={onSubmit}>
|
2017-11-08 16:06:21 +01:00
|
|
|
<ModalBody className={styles.modalBody}>
|
2019-04-08 19:54:30 +02:00
|
|
|
<div className="container-fluid">{this.renderForm()}</div>
|
2017-11-08 16:06:21 +01:00
|
|
|
</ModalBody>
|
|
|
|
<ModalFooter className={styles.modalFooter}>
|
2019-04-08 19:54:30 +02:00
|
|
|
<Button
|
|
|
|
onClick={() => this.context.unsetDataToEdit()}
|
|
|
|
className={styles.secondary}
|
|
|
|
>
|
|
|
|
<FormattedMessage id="users-permissions.PopUpForm.button.cancel" />
|
2017-11-08 16:06:21 +01:00
|
|
|
</Button>
|
2019-04-08 19:54:30 +02:00
|
|
|
<Button
|
|
|
|
type="submit"
|
2019-09-13 15:17:24 +02:00
|
|
|
onClick={onSubmit}
|
2019-04-08 19:54:30 +02:00
|
|
|
className={styles.primary}
|
|
|
|
>
|
|
|
|
<FormattedMessage id="users-permissions.PopUpForm.button.save" />
|
2018-01-22 13:10:58 +01:00
|
|
|
</Button>
|
2017-11-08 16:06:21 +01:00
|
|
|
</ModalFooter>
|
|
|
|
</form>
|
2017-11-08 12:22:03 +01:00
|
|
|
</Modal>
|
2019-09-13 15:17:24 +02:00
|
|
|
</Wrapper>
|
2017-11-08 12:22:03 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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',
|
|
|
|
};
|
|
|
|
|
|
|
|
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,
|
|
|
|
values: PropTypes.object.isRequired,
|
2017-11-08 12:22:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export default PopUpForm;
|