2018-12-21 10:55:35 +00:00
|
|
|
import React, { Component } from 'react';
|
2017-12-02 13:19:58 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2018-05-07 20:41:36 +00:00
|
|
|
import FastImage from 'react-native-fast-image';
|
2018-12-21 10:55:35 +00:00
|
|
|
import equal from 'deep-equal';
|
2019-04-08 12:35:28 +00:00
|
|
|
import Touchable from 'react-native-platform-touchable';
|
2018-08-01 19:35:06 +00:00
|
|
|
|
2017-12-02 13:19:58 +00:00
|
|
|
import PhotoModal from './PhotoModal';
|
2018-04-24 19:34:03 +00:00
|
|
|
import Markdown from './Markdown';
|
2018-08-01 19:35:06 +00:00
|
|
|
import styles from './styles';
|
2017-12-02 13:19:58 +00:00
|
|
|
|
2018-12-21 10:55:35 +00:00
|
|
|
export default class extends Component {
|
2017-12-02 13:19:58 +00:00
|
|
|
static propTypes = {
|
|
|
|
file: PropTypes.object.isRequired,
|
|
|
|
baseUrl: PropTypes.string.isRequired,
|
2018-07-10 13:40:32 +00:00
|
|
|
user: PropTypes.object.isRequired,
|
2018-09-11 16:32:52 +00:00
|
|
|
customEmojis: PropTypes.oneOfType([
|
|
|
|
PropTypes.array,
|
|
|
|
PropTypes.object
|
|
|
|
])
|
2017-12-02 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
2018-12-21 10:55:35 +00:00
|
|
|
state = { modalVisible: false, isPressed: false };
|
|
|
|
|
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
|
|
|
const { modalVisible, isPressed } = this.state;
|
|
|
|
const { file } = this.props;
|
|
|
|
if (nextState.modalVisible !== modalVisible) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextState.isPressed !== isPressed) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!equal(nextProps.file, file)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-12-02 13:19:58 +00:00
|
|
|
|
2019-04-08 12:35:28 +00:00
|
|
|
onPressButton = () => {
|
2018-09-19 14:18:32 +00:00
|
|
|
this.setState({
|
|
|
|
modalVisible: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-02 13:19:58 +00:00
|
|
|
getDescription() {
|
2018-09-11 16:32:52 +00:00
|
|
|
const {
|
|
|
|
file, customEmojis, baseUrl, user
|
|
|
|
} = this.props;
|
2018-04-24 19:34:03 +00:00
|
|
|
if (file.description) {
|
2018-09-11 16:32:52 +00:00
|
|
|
return <Markdown msg={file.description} customEmojis={customEmojis} baseUrl={baseUrl} username={user.username} />;
|
2017-12-02 13:19:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-19 14:18:32 +00:00
|
|
|
isPressed = (state) => {
|
|
|
|
this.setState({ isPressed: state });
|
2017-12-02 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { modalVisible, isPressed } = this.state;
|
2018-03-23 16:49:51 +00:00
|
|
|
const { baseUrl, file, user } = this.props;
|
|
|
|
const img = `${ baseUrl }${ file.image_url }?rc_uid=${ user.id }&rc_token=${ user.token }`;
|
2018-09-14 19:39:52 +00:00
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
if (!img) {
|
2018-09-14 19:39:52 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-12-02 13:19:58 +00:00
|
|
|
return (
|
2018-04-24 19:34:03 +00:00
|
|
|
[
|
2019-04-08 12:35:28 +00:00
|
|
|
<Touchable
|
2018-04-24 19:34:03 +00:00
|
|
|
key='image'
|
2019-04-08 12:35:28 +00:00
|
|
|
onPress={this.onPressButton}
|
2018-08-01 19:35:06 +00:00
|
|
|
style={styles.imageContainer}
|
2019-04-08 12:35:28 +00:00
|
|
|
background={Touchable.Ripple('#fff')}
|
2017-12-02 13:19:58 +00:00
|
|
|
>
|
2019-04-08 12:35:28 +00:00
|
|
|
<React.Fragment>
|
|
|
|
<FastImage
|
|
|
|
style={[styles.image, isPressed && { opacity: 0.5 }]}
|
|
|
|
source={{ uri: encodeURI(img) }}
|
|
|
|
resizeMode={FastImage.resizeMode.cover}
|
|
|
|
/>
|
|
|
|
{this.getDescription()}
|
|
|
|
</React.Fragment>
|
|
|
|
</Touchable>,
|
2017-12-02 13:19:58 +00:00
|
|
|
<PhotoModal
|
2018-04-24 19:34:03 +00:00
|
|
|
key='modal'
|
2018-09-11 16:32:52 +00:00
|
|
|
title={file.title}
|
|
|
|
description={file.description}
|
2018-03-23 16:49:51 +00:00
|
|
|
image={img}
|
2018-09-25 19:28:42 +00:00
|
|
|
isVisible={modalVisible}
|
2017-12-02 13:19:58 +00:00
|
|
|
onClose={() => this.setState({ modalVisible: false })}
|
|
|
|
/>
|
2018-04-24 19:34:03 +00:00
|
|
|
]
|
2017-12-02 13:19:58 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|