/**
*
* PopUpForm
*
*/
import React from 'react';
import { FormattedMessage } from 'react-intl';
import PropTypes from 'prop-types';
import {
capitalize,
get,
findIndex,
isArray,
isEmpty,
isObject,
includes,
map,
startsWith,
tail,
take,
takeRight,
} from 'lodash';
import {
ButtonModal,
HeaderModal,
HeaderModalTitle,
Modal,
ModalBody,
ModalFooter,
ModalForm,
InputsIndex as Input,
} from 'strapi-helper-plugin';
import { HomePageContext } from '../../contexts/HomePage';
// Translations
import en from '../../translations/en.json';
/* eslint-disable react/sort-comp */
/* eslint-disable no-shadow */
class PopUpForm extends React.Component {
// eslint-disable-line react/prefer-stateless-function
state = { enabled: false, isEditing: false };
static contextType = HomePageContext;
UNSAFE_componentWillReceiveProps(nextProps) {
const { values } = nextProps;
if (get(values, 'enabled') && get(values, 'enabled') !== get(this.props.values, 'enabled')) {
this.setState({ enabled: get(values, 'enabled') });
}
}
getRedirectURIProviderConf = () => {
// NOTE: Still testings providers so the switch statement is likely to change
switch (this.props.dataToEdit) {
case 'discord':
return `${strapi.backendURL}/connect/discord/callback`;
case 'facebook':
return `${strapi.backendURL}/connect/facebook/callback`;
case 'google':
return `${strapi.backendURL}/connect/google/callback`;
case 'github':
return `${strapi.backendURL}/connect/github/callback`;
case 'microsoft':
return `${strapi.backendURL}/connect/microsoft/callback`;
case 'twitter':
return `${strapi.backendURL}/connect/twitter/callback`;
case 'instagram':
return `${strapi.backendURL}/connect/instagram/callback`;
case 'vk':
return `${strapi.backendURL}/connect/vk/callback`;
case 'twitch':
return `${strapi.backendURL}/connect/twitch/callback`;
default: {
const value = get(this.props.values, 'callback', '');
return startsWith(value, 'http') ? value : `${strapi.backendURL}${value}`;
}
}
};
generateRedirectURL = url => {
return startsWith(url, 'https://') || startsWith(url, 'http://') || this.state.isEditing
? url
: `${strapi.backendURL}${startsWith(url, '/') ? '' : '/'}${url}`;
};
handleChange = e => {
this.setState({ enabled: e.target.value });
this.props.onChange(e);
};
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 });
renderForm = () => {
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);
}
if (current !== 'icon' && current !== 'scope') {
acc.push(`${name}${current}`);
}
return acc;
}, []);
if (settingType === 'providers') {
return (
<>
{map(tail(form), (value, key) => (
{}
}
onBlur={
includes(value, 'callback') || includes(value, 'redirect_uri')
? this.handleBlur
: false
}
onChange={this.props.onChange}
type="text"
value={
includes(value, 'callback') || includes(value, 'redirect_uri')
? this.generateRedirectURL(get(values, value))
: get(values, value)
}
validations={{ required: true }}
/>
))}
{dataToEdit !== 'email' && (
{}}
value={this.getRedirectURIProviderConf()}
validations={{}}
/>
)}
>
);
}
const params = {
link: (