@talend/react-containers
Version:
Provide connected components aka containers for @talend/react-cmf based on @talend/react-components.
265 lines (263 loc) • 8.65 kB
JavaScript
import { Component } from 'react';
import PropTypes from 'prop-types';
import cmf, { cmfConnect } from '@talend/react-cmf';
import Form from '@talend/react-forms';
import { Map } from 'immutable';
import memoizeOne from 'memoize-one';
import kit from './kit';
import tcompFieldsWidgets from './fields';
import { omit, get } from "lodash";
import { jsx as _jsx } from "react/jsx-runtime";
const TO_OMIT = ['definitionURL', 'uiSpecPath', 'submitURL', 'triggerULR', 'lang', 'customTriggers', 'dispatchOnChange', ...cmfConnect.INJECTED_PROPS];
export const DEFAULT_STATE = new Map({
dirty: false,
initialState: new Map()
});
/**
* Convert immutable object to js object
*/
export function toJS(immutableObject) {
if (!immutableObject) {
return null;
}
return immutableObject.toJS();
}
/**
* Insert titleMap name for corresponding value
* Its key is prefixed by '$', this means that it's an internal property
* @param schema The schema of the trigger input
* @param properties All the form properties
* @param value The input value
*/
export function resolveNameForTitleMap({
schema,
properties,
value
}) {
var _titleMap$;
if (!schema.titleMap) {
return;
}
// Here we add a field side by side with the value
// to keep the title associated to the value
const valueIsArray = Array.isArray(value);
const uniformValue = valueIsArray ? value : [value];
const {
titleMap
} = schema;
const isMultiSection = !!((_titleMap$ = titleMap[0]) !== null && _titleMap$ !== void 0 && _titleMap$.suggestions);
const names = uniformValue.map(nextValue => {
if (isMultiSection) {
// if we are here, it means we are facing a multi section list
// the titleMap contains sections which have their own list of values
// eslint-disable-next-line no-plusplus
for (let index = 0; index < titleMap.length; index++) {
const section = titleMap[index];
const result = section.suggestions.find(subItem => nextValue === subItem.value);
if (result) return result;
}
}
return titleMap.find(item => item.value === nextValue);
}).map(entry => entry && entry.name);
const parentKey = schema.key.slice();
const key = parentKey.pop();
const nameKey = `$${key}_name`;
const parentValue = Form.UIForm.utils.properties.getValue(properties, {
key: parentKey
});
if (names.some(name => name !== undefined)) {
parentValue[nameKey] = valueIsArray ? names : names[0];
} else {
delete parentValue[nameKey];
}
}
export class TCompForm extends Component {
constructor(props) {
super(props);
this.state = {};
this.onTrigger = this.onTrigger.bind(this);
this.onChange = this.onChange.bind(this);
this.onSubmit = this.onSubmit.bind(this);
this.onReset = this.onReset.bind(this);
this.getUISpec = this.getUISpec.bind(this);
this.setupTrigger = this.setupTrigger.bind(this);
this.setupTrigger(props);
this.getMemoizedJsonSchema = memoizeOne(toJS);
this.getMemoizedUiSchema = memoizeOne(toJS);
this.getMemoizedInitialState = memoizeOne(toJS);
}
componentDidUpdate(prevProps) {
const nextProperties = this.props.state.get('properties');
if (prevProps.state.get('properties') !== nextProperties) {
this.setState({
properties: (nextProperties === null || nextProperties === void 0 ? void 0 : nextProperties.toJS()) || {}
});
}
if (prevProps.triggerURL !== this.props.triggerURL || prevProps.customTriggers !== this.props.customTriggers) {
this.setupTrigger(this.props);
}
if (this.props.definitionURL !== prevProps.definitionURL) {
this.props.dispatch({
type: TCompForm.ON_DEFINITION_URL_CHANGED,
...this.props,
properties: this.state.properties
});
}
}
onChange(_, payload) {
if (!this.props.state.get('dirty')) {
this.props.setState({
dirty: true
});
}
resolveNameForTitleMap(payload);
this.setState({
properties: payload.properties
});
if (this.props.dispatchOnChange) {
this.props.dispatch({
type: TCompForm.ON_CHANGE,
component: TCompForm.displayName,
componentId: this.props.componentId,
...payload
});
}
if (this.props.onChange) {
this.props.onChange(_, payload);
}
}
onTrigger(event, payload) {
this.props.dispatch({
type: TCompForm.ON_TRIGGER_BEGIN,
...payload
});
// Trigger definitions from tacokit can precise the fields that are impacted by the trigger.
// Those fields are the jsonSchema path.
// trigger = { options: [{ path: 'user.firstname' }, { path: 'user.lastname' }] }
if (Array.isArray(get(payload, 'trigger.options'))) {
const updating = payload.trigger.options.map(op => op.path);
this.setState({
updating
});
}
return this.trigger(event, payload).then(data => {
this.props.dispatch({
type: TCompForm.ON_TRIGGER_END,
...payload
});
if (data.jsonSchema || data.uiSchema) {
this.props.setState(data);
}
return data;
}).finally(() => {
this.setState({
updating: []
});
});
}
onSubmit(_, properties) {
this.props.dispatch({
type: TCompForm.ON_SUBMIT,
component: TCompForm.displayName,
componentId: this.props.componentId,
properties
});
if (this.props.onSubmit) {
this.props.onSubmit(_, properties);
}
}
onReset() {
this.props.setState(prev => prev.state.set('jsonSchema', this.props.state.getIn(['initialState', 'jsonSchema'])).set('uiSchema', this.props.state.getIn(['initialState', 'uiSchema'])).set('properties', this.props.state.getIn(['initialState', 'properties'])).set('dirty', false));
this.setState({
properties: this.props.state.getIn(['initialState', 'properties']).toJS()
});
}
setupTrigger(props) {
const config = cmf.sagas.http.getDefaultConfig() || {};
this.trigger = kit.createTriggers({
url: props.triggerURL,
customRegistry: props.customTriggers,
headers: config.headers,
lang: props.lang,
security: {
CSRFTokenCookieKey: props.CSRFTokenCookieKey,
CSRFTokenHeaderKey: props.CSRFTokenHeaderKey
}
});
}
getUISpec() {
return {
properties: this.state.properties,
jsonSchema: this.getMemoizedJsonSchema(this.props.state.get('jsonSchema')),
uiSchema: this.getMemoizedUiSchema(this.props.state.get('uiSchema'))
};
}
render() {
const uiSpecs = this.getUISpec();
if (!uiSpecs.jsonSchema) {
const response = this.props.state.get('response');
if (response) {
return /*#__PURE__*/_jsx("p", {
className: "danger",
children: response.get('statusText')
});
}
return /*#__PURE__*/_jsx(Form, {
loading: true,
displayMode: this.props.displayMode,
actions: this.props.actions
});
}
const props = {
...omit(this.props, TO_OMIT),
data: uiSpecs,
initialData: this.getMemoizedInitialState(this.props.state.get('initialState')),
onTrigger: this.onTrigger,
onChange: this.onChange,
onSubmit: this.onSubmit,
onReset: this.onReset,
widgets: {
...tcompFieldsWidgets,
...this.props.widgets
},
updating: this.state.updating
};
return /*#__PURE__*/_jsx(Form, {
...props
});
}
}
TCompForm.ON_CHANGE = 'TCOMP_FORM_CHANGE';
TCompForm.ON_SUBMIT = 'TCOMP_FORM_SUBMIT';
TCompForm.ON_SUBMIT_SUCCEED = 'TCOMP_FORM_SUBMIT_SUCCEED';
TCompForm.ON_SUBMIT_FAILED = 'TCOMP_FORM_SUBMIT_FAILED';
TCompForm.ON_TRIGGER_BEGIN = 'TCOMP_FORM_TRIGGER_BEGIN';
TCompForm.ON_TRIGGER_END = 'TCOMP_FORM_TRIGGER_END';
TCompForm.ON_DEFINITION_URL_CHANGED = 'TCOMP_FORM_DEFINITION_URL_CHANGE';
TCompForm.displayName = 'ComponentForm';
TCompForm.propTypes = {
...cmfConnect.propTypes,
definitionURL: PropTypes.string.isRequired,
triggerURL: PropTypes.string.isRequired,
submitURL: PropTypes.string,
uiSpecPath: PropTypes.string,
lang: PropTypes.string,
customTriggers: PropTypes.object,
dispatchOnChange: PropTypes.bool,
CSRFTokenCookieKey: PropTypes.string,
CSRFTokenHeaderKey: PropTypes.string,
onSubmit: PropTypes.func,
onChange: PropTypes.func
};
export default cmfConnect({
defaultState: DEFAULT_STATE,
defaultProps: {
saga: 'ComponentForm#default'
},
omitCMFProps: true,
withComponentRegistry: true,
withDispatch: true,
withDispatchActionCreator: true,
withComponentId: true
})(TCompForm);
//# sourceMappingURL=ComponentForm.component.js.map