mirror of
https://github.com/Unleash/unleash.git
synced 2025-09-19 17:52:45 +02:00
* feat: change color scheme * feat: add navigation menu * fix: add bg image * fix: add archive and strategies to navigation * fix: round corners * feat: mobile view project details * feat: mobile view navigation * fix: only show menu if user is admin * fix: rename navigation * fix: only render relevant routes for oss context * feat: add project actions * feat: add icons * feat: add breadcrumbs * fix: place breadcrumbs absolutely * fix: adjust breadcrumbs * fix: toast * fix: cleanup * fix login * fix: breadcrumbs * fix: add billing link * fix: links * fix: feature view * fix: path to go back * fix: remove default value * fix: remove unused imports * refactor: delete outdated test * fix: add item to filter in breadcrumb * fix: remove console log
195 lines
6.2 KiB
JavaScript
195 lines
6.2 KiB
JavaScript
import React, { useState, useEffect } from 'react';
|
|
import PropTypes from 'prop-types';
|
|
import { TextField, FormControlLabel, Switch } from '@material-ui/core';
|
|
|
|
import { FormButtons, styles as commonStyles } from '../common';
|
|
import { trim } from '../common/util';
|
|
import AddonParameters from './form-addon-parameters';
|
|
import AddonEvents from './form-addon-events';
|
|
import cloneDeep from 'lodash.clonedeep';
|
|
|
|
import styles from './form-addon-component.module.scss';
|
|
import PageContent from '../common/PageContent/PageContent';
|
|
|
|
const AddonFormComponent = ({
|
|
editMode,
|
|
provider,
|
|
addon,
|
|
fetch,
|
|
cancel,
|
|
submit,
|
|
}) => {
|
|
const [config, setConfig] = useState(addon);
|
|
const [errors, setErrors] = useState({
|
|
parameters: {},
|
|
});
|
|
const submitText = editMode ? 'Update' : 'Create';
|
|
|
|
useEffect(() => {
|
|
if (!provider) {
|
|
fetch();
|
|
}
|
|
}, [fetch, provider]); // empty array => fetch only first time
|
|
|
|
useEffect(() => {
|
|
setConfig({ ...addon });
|
|
/* eslint-disable-next-line */
|
|
}, [addon.description, addon.provider]);
|
|
|
|
useEffect(() => {
|
|
if (provider && !config.provider) {
|
|
setConfig({ ...addon, provider: provider.name });
|
|
}
|
|
}, [provider, addon, config.provider]);
|
|
|
|
const setFieldValue = field => evt => {
|
|
evt.preventDefault();
|
|
const newConfig = { ...config };
|
|
newConfig[field] = evt.target.value;
|
|
setConfig(newConfig);
|
|
};
|
|
|
|
const onEnabled = evt => {
|
|
evt.preventDefault();
|
|
const enabled = !config.enabled;
|
|
setConfig({ ...config, enabled });
|
|
};
|
|
|
|
const setParameterValue = param => evt => {
|
|
evt.preventDefault();
|
|
const newConfig = { ...config };
|
|
newConfig.parameters[param] = evt.target.value;
|
|
setConfig(newConfig);
|
|
};
|
|
|
|
const setEventValue = name => evt => {
|
|
const newConfig = { ...config };
|
|
if (evt.target.checked) {
|
|
newConfig.events.push(name);
|
|
} else {
|
|
newConfig.events = newConfig.events.filter(e => e !== name);
|
|
}
|
|
setConfig(newConfig);
|
|
setErrors({ ...errors, events: undefined });
|
|
};
|
|
|
|
const onSubmit = async evt => {
|
|
evt.preventDefault();
|
|
if (!provider) return;
|
|
|
|
const updatedErrors = cloneDeep(errors);
|
|
updatedErrors.containsErrors = false;
|
|
|
|
// Validations
|
|
if (config.events.length === 0) {
|
|
updatedErrors.events = 'You must listen to at least one event';
|
|
updatedErrors.containsErrors = true;
|
|
}
|
|
|
|
provider.parameters.forEach(p => {
|
|
const value = trim(config.parameters[p.name]);
|
|
if (p.required && !value) {
|
|
updatedErrors.parameters[p.name] = 'This field is required';
|
|
updatedErrors.containsErrors = true;
|
|
}
|
|
});
|
|
|
|
if (updatedErrors.containsErrors) {
|
|
setErrors(updatedErrors);
|
|
return;
|
|
}
|
|
|
|
try {
|
|
await submit(config);
|
|
} catch (e) {
|
|
setErrors({ parameters: {}, general: e.message });
|
|
}
|
|
};
|
|
|
|
const {
|
|
name,
|
|
description,
|
|
documentationUrl = 'https://unleash.github.io/docs/addons',
|
|
} = provider ? provider : {};
|
|
|
|
return (
|
|
<PageContent headerContent={`Configure ${name} addon`}>
|
|
<section className={styles.formSection}>
|
|
{description}
|
|
<a href={documentationUrl} target="_blank" rel="noreferrer">
|
|
Read more
|
|
</a>
|
|
<p className={commonStyles.error}>{errors.general}</p>
|
|
</section>
|
|
<form onSubmit={onSubmit}>
|
|
<section className={styles.formSection}>
|
|
<TextField
|
|
size="small"
|
|
label="Provider"
|
|
name="provider"
|
|
value={config.provider}
|
|
disabled
|
|
variant="outlined"
|
|
className={styles.nameInput}
|
|
/>
|
|
<FormControlLabel
|
|
control={
|
|
<Switch
|
|
checked={config.enabled}
|
|
onChange={onEnabled}
|
|
/>
|
|
}
|
|
label={config.enabled ? 'Enabled' : 'Disabled'}
|
|
/>
|
|
</section>
|
|
<section className={styles.formSection}>
|
|
<TextField
|
|
size="small"
|
|
style={{ width: '80%' }}
|
|
rows={4}
|
|
multiline
|
|
label="Description"
|
|
name="description"
|
|
placeholder=""
|
|
value={config.description}
|
|
error={errors.description}
|
|
onChange={setFieldValue('description')}
|
|
variant="outlined"
|
|
/>
|
|
</section>
|
|
<section className={styles.formSection}>
|
|
<AddonEvents
|
|
provider={provider}
|
|
checkedEvents={config.events}
|
|
setEventValue={setEventValue}
|
|
error={errors.events}
|
|
/>
|
|
</section>
|
|
<section className={styles.formSection}>
|
|
<AddonParameters
|
|
provider={provider}
|
|
config={config}
|
|
errors={errors}
|
|
editMode={editMode}
|
|
setParameterValue={setParameterValue}
|
|
/>
|
|
</section>
|
|
<section className={styles.formSection}>
|
|
<FormButtons submitText={submitText} onCancel={cancel} />
|
|
</section>
|
|
</form>
|
|
</PageContent>
|
|
);
|
|
};
|
|
|
|
AddonFormComponent.propTypes = {
|
|
provider: PropTypes.object,
|
|
addon: PropTypes.object.isRequired,
|
|
fetch: PropTypes.func.isRequired,
|
|
submit: PropTypes.func.isRequired,
|
|
cancel: PropTypes.func.isRequired,
|
|
editMode: PropTypes.bool.isRequired,
|
|
};
|
|
|
|
export default AddonFormComponent;
|