import React from 'react'; import PropTypes from 'prop-types'; // eslint-disable-next-line no-unused-vars import { withTranslation } from 'react-i18next'; import ProgressBar from 'react-bootstrap/es/ProgressBar'; import GrowiArchiveImportOption from '@commons/models/admin/growi-archive-import-option'; const MODE_ATTR_MAP = { insert: { color: 'info', icon: 'icon-plus', label: 'Insert' }, upsert: { color: 'success', icon: 'icon-plus', label: 'Upsert' }, flushAndInsert: { color: 'danger', icon: 'icon-refresh', label: 'Flush and Insert' }, }; export const DEFAULT_MODE = 'insert'; export const MODE_RESTRICTED_COLLECTION = { configs: ['flushAndInsert'], users: ['insert', 'upsert'], }; export default class GrowiZipImportItem extends React.Component { constructor(props) { super(props); this.changeHandler = this.changeHandler.bind(this); this.modeSelectedHandler = this.modeSelectedHandler.bind(this); this.configButtonClickedHandler = this.configButtonClickedHandler.bind(this); this.errorLinkClickedHandler = this.errorLinkClickedHandler.bind(this); } changeHandler(e) { const { collectionName, onChange } = this.props; if (onChange != null) { onChange(collectionName, e.target.checked); } } modeSelectedHandler(mode) { const { collectionName, onOptionChange } = this.props; if (onOptionChange == null) { return; } onOptionChange(collectionName, { mode }); } configButtonClickedHandler() { const { collectionName, onConfigButtonClicked } = this.props; if (onConfigButtonClicked == null) { return; } onConfigButtonClicked(collectionName); } errorLinkClickedHandler() { const { collectionName, onErrorLinkClicked } = this.props; if (onErrorLinkClicked == null) { return; } onErrorLinkClicked(collectionName); } renderModeLabel(mode, isColorized = false) { const attrMap = MODE_ATTR_MAP[mode]; const className = isColorized ? `text-${attrMap.color}` : ''; return {attrMap.label}; } renderCheckbox() { const { collectionName, isSelected, isImporting, } = this.props; return (
); } renderModeSelector() { const { collectionName, option, isImporting, } = this.props; const attrMap = MODE_ATTR_MAP[option.mode]; const btnColor = `btn-${attrMap.color}`; const modes = MODE_RESTRICTED_COLLECTION[collectionName] || Object.keys(MODE_ATTR_MAP); return ( Mode: 
); } renderConfigButton() { const { isImporting, isConfigButtonAvailable } = this.props; return ( ); } renderProgressBar() { const { isImporting, insertedCount, modifiedCount, errorsCount, } = this.props; const total = insertedCount + modifiedCount + errorsCount; return ( ); } renderBody() { const { isImporting, isImported } = this.props; if (!isImporting && !isImported) { return 'Ready'; } const { insertedCount, modifiedCount, errorsCount } = this.props; return (
{insertedCount} Inserted{modifiedCount} Modified,  { errorsCount > 0 ? {errorsCount} Failed : 0 Failed }
); } render() { const { isSelected, } = this.props; return (
{/* left */} {this.renderCheckbox()} {/* right */} {this.renderModeSelector()} {this.renderConfigButton()}
{ isSelected && ( <> {this.renderProgressBar()}
{this.renderBody()}
) }
); } } GrowiZipImportItem.propTypes = { collectionName: PropTypes.string.isRequired, isSelected: PropTypes.bool.isRequired, option: PropTypes.instanceOf(GrowiArchiveImportOption).isRequired, isImporting: PropTypes.bool.isRequired, isImported: PropTypes.bool.isRequired, insertedCount: PropTypes.number, modifiedCount: PropTypes.number, errorsCount: PropTypes.number, isConfigButtonAvailable: PropTypes.bool, onChange: PropTypes.func, onOptionChange: PropTypes.func, onConfigButtonClicked: PropTypes.func, onErrorLinkClicked: PropTypes.func, }; GrowiZipImportItem.defaultProps = { insertedCount: 0, modifiedCount: 0, errorsCount: 0, };