huridocs/uwazi

View on GitHub
app/react/Library/components/Doc.js

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
Definition for rule 'node/no-restricted-import' was not found.
import React, { Component } from 'react';
import { is, Map } from 'immutable';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { NeedAuthorization } from 'app/Auth';
import ShowIf from 'app/App/ShowIf';
import { t } from 'app/I18N';
import UploadEntityStatus from 'app/Library/components/UploadEntityStatus';
import ViewDocButton from 'app/Library/components/ViewDocButton';
import { Icon } from 'UI';
 
import { Item } from 'app/Layout';
import helpers from 'app/Documents/helpers';
 
Export statements should appear at the end of the file
export class Doc extends Component {
shouldComponentUpdate(nextProps) {
return (
!is(this.props.doc, nextProps.doc) ||
!is(this.props.targetReference, nextProps.targetReference) ||
this.props.additionalText !== nextProps.additionalText ||
this.props.active !== nextProps.active ||
(this.props.searchParams &&
nextProps.searchParams &&
this.props.searchParams.sort !== nextProps.searchParams.sort)
);
}
 
onClick(e) {
if (this.props.onClick) {
this.props.onClick(e, this.props.doc, this.props.active);
}
}
 
getConnections(connections) {
return (
<div>
{connections.map((connection, index) => (
Do not use Array index in keys
<div key={index} className="item-connection">
<div>
<Icon icon="exchange-alt" />
<span>
{t(connection.context, connection.label)}
{connection.type === 'metadata' ? ` ${t('System', 'in')}...` : ''}
</span>
</div>
<NeedAuthorization roles={['admin', 'editor']}>
<ShowIf if={connection.sourceType !== 'metadata'}>
<button
className="btn btn-default btn-hover-danger btn-xs"
onClick={e => this.deleteConnection(e, connection)}
type="button"
>
<Icon icon="trash-alt" />
</button>
</ShowIf>
</NeedAuthorization>
</div>
))}
</div>
);
}
 
deleteConnection(e, connection) {
e.stopPropagation();
const { _id, sourceType } = connection;
this.props.deleteConnection({ _id, sourceType });
}
 
render() {
const { className, additionalText, targetReference } = this.props;
const doc = helpers.performantDocToJSWithoutRelations(this.props.doc);
const { sharedId, file, processed } = doc;
 
let itemConnections = null;
if (doc.connections && doc.connections.length) {
itemConnections = this.getConnections(doc.connections);
}
 
const buttons = (
<div>
<ViewDocButton
file={file}
sharedId={sharedId}
processed={processed}
storeKey={this.props.storeKey}
targetReference={targetReference}
/>
</div>
);
 
return (
<Item
JSX props should not use .bind()
onClick={this.onClick.bind(this)}
onSnippetClick={this.props.onSnippetClick}
active={this.props.active}
doc={this.props.doc}
additionalText={additionalText}
searchParams={this.props.searchParams}
deleteConnection={this.props.deleteConnection}
itemHeader={itemConnections}
buttons={buttons}
labels={<UploadEntityStatus doc={this.props.doc} />}
className={className}
/>
);
}
}
 
Doc.defaultProps = {
targetReference: null,
};
 
Doc.propTypes = {
propType "doc" is not required, but has no corresponding defaultProps declaration.
Prop type "object" is forbidden
doc: PropTypes.object,
Prop type "object" is forbidden
propType "searchParams" is not required, but has no corresponding defaultProps declaration.
searchParams: PropTypes.object,
propType "active" is not required, but has no corresponding defaultProps declaration.
active: PropTypes.bool,
propType "authorized" is not required, but has no corresponding defaultProps declaration.
'authorized' PropType is defined but prop is never used
authorized: PropTypes.bool,
propType "deleteConnection" is not required, but has no corresponding defaultProps declaration.
deleteConnection: PropTypes.func,
propType "onSnippetClick" is not required, but has no corresponding defaultProps declaration.
onSnippetClick: PropTypes.func,
propType "onClick" is not required, but has no corresponding defaultProps declaration.
onClick: PropTypes.func,
propType "className" is not required, but has no corresponding defaultProps declaration.
className: PropTypes.string,
propType "additionalText" is not required, but has no corresponding defaultProps declaration.
additionalText: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
propType "storeKey" is not required, but has no corresponding defaultProps declaration.
storeKey: PropTypes.string,
targetReference: PropTypes.instanceOf(Map),
};
 
export function mapStateToProps(state, ownProps) {
const active = ownProps.storeKey
? !!state[ownProps.storeKey].ui
.get('selectedDocuments')
.find(doc => doc.get('_id') === ownProps.doc.get('_id'))
: false;
return {
active,
};
}
 
Prefer named exports.
export default connect(mapStateToProps)(Doc);