@talend/react-containers
Version:
Provide connected components aka containers for @talend/react-cmf based on @talend/react-components.
276 lines (273 loc) • 10.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.TCompForm = exports.DEFAULT_STATE = void 0;
exports.resolveNameForTitleMap = resolveNameForTitleMap;
exports.toJS = toJS;
var _react = require("react");
var _propTypes = _interopRequireDefault(require("prop-types"));
var _reactCmf = _interopRequireWildcard(require("@talend/react-cmf"));
var _reactForms = _interopRequireDefault(require("@talend/react-forms"));
var _immutable = require("immutable");
var _memoizeOne = _interopRequireDefault(require("memoize-one"));
var _kit = _interopRequireDefault(require("./kit"));
var _fields = _interopRequireDefault(require("./fields"));
var _lodash = require("lodash");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const TO_OMIT = ['definitionURL', 'uiSpecPath', 'submitURL', 'triggerULR', 'lang', 'customTriggers', 'dispatchOnChange', ..._reactCmf.cmfConnect.INJECTED_PROPS];
const DEFAULT_STATE = exports.DEFAULT_STATE = new _immutable.Map({
dirty: false,
initialState: new _immutable.Map()
});
/**
* Convert immutable object to js object
*/
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
*/
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 = _reactForms.default.UIForm.utils.properties.getValue(properties, {
key: parentKey
});
if (names.some(name => name !== undefined)) {
parentValue[nameKey] = valueIsArray ? names : names[0];
} else {
delete parentValue[nameKey];
}
}
class TCompForm extends _react.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 = (0, _memoizeOne.default)(toJS);
this.getMemoizedUiSchema = (0, _memoizeOne.default)(toJS);
this.getMemoizedInitialState = (0, _memoizeOne.default)(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((0, _lodash.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 = _reactCmf.default.sagas.http.getDefaultConfig() || {};
this.trigger = _kit.default.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__*/(0, _jsxRuntime.jsx)("p", {
className: "danger",
children: response.get('statusText')
});
}
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactForms.default, {
loading: true,
displayMode: this.props.displayMode,
actions: this.props.actions
});
}
const props = {
...(0, _lodash.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: {
..._fields.default,
...this.props.widgets
},
updating: this.state.updating
};
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactForms.default, {
...props
});
}
}
exports.TCompForm = TCompForm;
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 = {
..._reactCmf.cmfConnect.propTypes,
definitionURL: _propTypes.default.string.isRequired,
triggerURL: _propTypes.default.string.isRequired,
submitURL: _propTypes.default.string,
uiSpecPath: _propTypes.default.string,
lang: _propTypes.default.string,
customTriggers: _propTypes.default.object,
dispatchOnChange: _propTypes.default.bool,
CSRFTokenCookieKey: _propTypes.default.string,
CSRFTokenHeaderKey: _propTypes.default.string,
onSubmit: _propTypes.default.func,
onChange: _propTypes.default.func
};
var _default = exports.default = (0, _reactCmf.cmfConnect)({
defaultState: DEFAULT_STATE,
defaultProps: {
saga: 'ComponentForm#default'
},
omitCMFProps: true,
withComponentRegistry: true,
withDispatch: true,
withDispatchActionCreator: true,
withComponentId: true
})(TCompForm);
//# sourceMappingURL=ComponentForm.component.js.map