@talend/react-containers
Version:
Provide connected components aka containers for @talend/react-cmf based on @talend/react-components.
161 lines (156 loc) • 5.48 kB
JavaScript
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import { Component } from 'react';
import PropTypes from 'prop-types';
import { cmfConnect } from '@talend/react-cmf';
import { Trans, withTranslation } from 'react-i18next';
import getDefaultT from '../translate';
import I18N_DOMAIN_CONTAINERS from '../constant';
import CONSTANTS from './constants';
/**
* getLabel: return label to display on delete confirmation dialog
* @param resource
* @returns {*}
*/
import { ConfirmDialog } from "@talend/react-components";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
function getLabel(resource) {
if (resource) {
return resource.get('label') || resource.get('name') || '';
}
return '';
}
/**
* DeleteResource is used to delete a specific resource.
* When the component is mounted, it opens a confirm dialog.
* It uses the saga matching pattern to launch a race between the cancel and validate action.
*/
export class DeleteResource extends Component {
constructor(props) {
super(props);
this.getLabelInfo = this.getLabelInfo.bind(this);
this.getResourceInfo = this.getResourceInfo.bind(this);
this.onHide = this.onHide.bind(this);
}
/**
* onHide handler, called when click outside delete modal
* @param event
*/
onHide(event) {
this.props.dispatchActionCreator('DeleteResource#cancel', event, {
model: this.getResourceInfo()
});
}
/**
* Get the label from the collections.
* Return the label and a boolean to confirm that the item has been found.
*/
getLabelInfo() {
return {
label: getLabel(this.props.resource),
found: !!this.props.resource
};
}
/**
* Build an object with all the data's resource we need.
*/
getResourceInfo() {
return {
resourceType: this.props.resourceType,
collectionId: this.props.collectionId,
resourceTypeLabel: this.props.resourceTypeLabel ? this.props.resourceTypeLabel : this.props.resourceType,
uri: this.props.uri,
...this.getLabelInfo(),
id: this.props.resourceId,
redirectUrl: this.props.redirectUrl,
onCancelRedirectUrl: this.props.onCancelRedirectUrl,
resource: this.props.resource
};
}
render() {
const resourceInfo = this.getResourceInfo();
const validateAction = {
componentId: this.props[CONSTANTS.VALIDATE_ACTION],
model: resourceInfo,
label: this.props.t('DELETE_RESOURCE_YES', {
defaultValue: 'REMOVE'
}),
bsStyle: 'danger',
onClickActionCreator: 'DeleteResource#validate',
...this.props.validateActionProps
};
const cancelAction = {
componentId: this.props[CONSTANTS.CANCEL_ACTION],
model: resourceInfo,
label: this.props.t('DELETE_RESOURCE_NO', {
defaultValue: 'CANCEL'
}),
className: 'btn-inverse',
onClickActionCreator: 'DeleteResource#cancel',
...this.props.cancelAction
};
// Sorry for this duplication, but we need it because of the i18n scanner to create 2 keys
// DELETE_RESOURCE_MESSAGE and DELETE_RESOURCE_MESSAGE_female
let question;
const {
resourceTypeLabel,
label
} = resourceInfo;
if (this.props.female) {
question = /*#__PURE__*/_jsxs(Trans, {
i18nKey: "tui-containers:DELETE_RESOURCE_MESSAGE",
tOptions: {
context: 'female'
},
children: ["Are you sure you want to remove the ", {
resourceTypeLabel
}, " ", /*#__PURE__*/_jsx("strong", {
children: {
label
}
}), "?"]
});
} else {
question = /*#__PURE__*/_jsxs(Trans, {
i18nKey: "tui-containers:DELETE_RESOURCE_MESSAGE",
children: ["Are you sure you want to remove the ", {
resourceTypeLabel
}, " ", /*#__PURE__*/_jsx("strong", {
children: {
label
}
}), "?"]
});
}
return /*#__PURE__*/_jsx(ConfirmDialog, {
show: true,
header: this.props.header,
cancelAction: cancelAction,
validateAction: validateAction,
getComponent: this.props.getComponent,
onHide: this.onHide,
children: question
});
}
}
_defineProperty(DeleteResource, "displayName", 'Container(DeleteResource)');
_defineProperty(DeleteResource, "propTypes", {
...cmfConnect.propTypes,
header: PropTypes.string,
uri: PropTypes.string.isRequired,
resourceType: PropTypes.string.isRequired,
resourceTypeLabel: PropTypes.string,
resourceId: PropTypes.string,
collectionId: PropTypes.string,
female: PropTypes.bool,
onCancelRedirectUrl: PropTypes.string,
validateActionProps: PropTypes.object,
t: PropTypes.func
});
_defineProperty(DeleteResource, "defaultProps", {
validateActionProps: {},
t: getDefaultT()
});
export default withTranslation(I18N_DOMAIN_CONTAINERS)(DeleteResource);
//# sourceMappingURL=DeleteResource.container.js.map