2021-02-04 19:15:23 +01:00
|
|
|
import React, { PureComponent } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2022-02-15 19:39:57 +01:00
|
|
|
import { debounce } from 'lodash';
|
2021-02-04 19:15:23 +01:00
|
|
|
import classnames from 'classnames';
|
2019-11-04 13:40:46 +01:00
|
|
|
|
|
|
|
export default class SignatureRequestMessage extends PureComponent {
|
|
|
|
static propTypes = {
|
|
|
|
data: PropTypes.object.isRequired,
|
2022-02-15 19:39:57 +01:00
|
|
|
onMessageScrolled: PropTypes.func,
|
|
|
|
setMessageRootRef: PropTypes.func,
|
2022-02-16 00:59:59 +01:00
|
|
|
messageRootRef: PropTypes.object,
|
|
|
|
messageIsScrollable: PropTypes.bool,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-11-04 13:40:46 +01:00
|
|
|
|
|
|
|
static contextTypes = {
|
|
|
|
t: PropTypes.func,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-11-04 13:40:46 +01:00
|
|
|
|
2022-02-15 19:39:57 +01:00
|
|
|
state = {
|
|
|
|
messageIsScrolled: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
setMessageIsScrolled = () => {
|
2022-02-16 00:59:59 +01:00
|
|
|
if (!this.props.messageRootRef || this.state.messageIsScrolled) {
|
2022-02-15 19:39:57 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-16 00:59:59 +01:00
|
|
|
const { scrollTop, offsetHeight, scrollHeight } = this.props.messageRootRef;
|
2022-05-24 17:28:40 +02:00
|
|
|
const isAtBottom = Math.round(scrollTop) + offsetHeight >= scrollHeight;
|
2022-02-15 19:39:57 +01:00
|
|
|
|
|
|
|
if (isAtBottom) {
|
|
|
|
this.setState({ messageIsScrolled: true });
|
|
|
|
this.props.onMessageScrolled();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
onScroll = debounce(this.setMessageIsScrolled, 25);
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
renderNode(data) {
|
2019-11-04 13:40:46 +01:00
|
|
|
return (
|
|
|
|
<div className="signature-request-message--node">
|
2020-08-19 18:27:05 +02:00
|
|
|
{Object.entries(data).map(([label, value], i) => (
|
2019-11-04 13:40:46 +01:00
|
|
|
<div
|
|
|
|
className={classnames('signature-request-message--node', {
|
2020-11-03 00:41:28 +01:00
|
|
|
'signature-request-message--node-leaf':
|
|
|
|
typeof value !== 'object' || value === null,
|
2019-11-04 13:40:46 +01:00
|
|
|
})}
|
|
|
|
key={i}
|
|
|
|
>
|
2020-11-03 00:41:28 +01:00
|
|
|
<span className="signature-request-message--node-label">
|
|
|
|
{label}:{' '}
|
|
|
|
</span>
|
|
|
|
{typeof value === 'object' && value !== null ? (
|
|
|
|
this.renderNode(value)
|
|
|
|
) : (
|
|
|
|
<span className="signature-request-message--node-value">
|
2020-12-11 18:40:34 +01:00
|
|
|
{`${value}`}
|
2020-11-03 00:41:28 +01:00
|
|
|
</span>
|
|
|
|
)}
|
2019-11-04 13:40:46 +01:00
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</div>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-11-04 13:40:46 +01:00
|
|
|
}
|
|
|
|
|
2022-02-16 00:59:59 +01:00
|
|
|
renderScrollButton() {
|
2019-11-04 13:40:46 +01:00
|
|
|
return (
|
2022-02-15 19:39:57 +01:00
|
|
|
<div
|
2022-02-16 00:59:59 +01:00
|
|
|
onClick={() => {
|
|
|
|
this.setState({ messageIsScrolled: true });
|
|
|
|
this.props.onMessageScrolled();
|
|
|
|
this.props.messageRootRef.scrollTo(
|
|
|
|
0,
|
|
|
|
this.props.messageRootRef.scrollHeight,
|
|
|
|
);
|
2022-02-15 19:39:57 +01:00
|
|
|
}}
|
2022-02-16 00:59:59 +01:00
|
|
|
className="signature-request-message__scroll-button"
|
|
|
|
data-testid="signature-request-scroll-button"
|
2022-02-15 19:39:57 +01:00
|
|
|
>
|
2022-03-23 18:03:02 +01:00
|
|
|
<i className="fa fa-arrow-down" title={this.context.t('scrollDown')} />
|
2022-02-16 00:59:59 +01:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { data, messageIsScrollable } = this.props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div onScroll={this.onScroll} className="signature-request-message">
|
|
|
|
{messageIsScrollable ? this.renderScrollButton() : null}
|
2020-11-03 00:41:28 +01:00
|
|
|
<div className="signature-request-message__title">
|
|
|
|
{this.context.t('signatureRequest1')}
|
|
|
|
</div>
|
2022-02-15 19:39:57 +01:00
|
|
|
<div
|
|
|
|
className="signature-request-message--root"
|
|
|
|
ref={this.props.setMessageRootRef}
|
|
|
|
>
|
2019-11-04 13:40:46 +01:00
|
|
|
{this.renderNode(data)}
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-11-04 13:40:46 +01:00
|
|
|
}
|
|
|
|
}
|