[FIX] Load local URL image (#871)

This commit is contained in:
Diego Mello 2019-05-03 10:29:56 -03:00 committed by GitHub
parent 661e9eac01
commit d6ed1055ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 13 deletions

View File

@ -320,13 +320,13 @@ export default class Message extends PureComponent {
} }
renderUrl = () => { renderUrl = () => {
const { urls } = this.props; const { urls, user, baseUrl } = this.props;
if (urls.length === 0) { if (urls.length === 0) {
return null; return null;
} }
return urls.map((url, index) => ( return urls.map((url, index) => (
<Url url={url} key={url.url} index={index} /> <Url url={url} key={url.url} index={index} user={user} baseUrl={baseUrl} />
)); ));
} }

View File

@ -3,6 +3,7 @@ import { View, Text, StyleSheet } from 'react-native';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import FastImage from 'react-native-fast-image'; import FastImage from 'react-native-fast-image';
import Touchable from 'react-native-platform-touchable'; import Touchable from 'react-native-platform-touchable';
import isEqual from 'lodash/isEqual';
import openLink from '../../utils/openLink'; import openLink from '../../utils/openLink';
import sharedStyles from '../../views/Styles'; import sharedStyles from '../../views/Styles';
@ -50,33 +51,60 @@ const styles = StyleSheet.create({
} }
}); });
const onPress = (url) => { const UrlImage = React.memo(({ image, user, baseUrl }) => {
openLink(url); if (!image) {
}; return null;
const Url = ({ url, index }) => { }
image = image.includes('http') ? image : `${ baseUrl }/${ image }?rc_uid=${ user.id }&rc_token=${ user.token }`;
return <FastImage source={{ uri: image }} style={styles.image} resizeMode={FastImage.resizeMode.cover} />;
});
const UrlContent = React.memo(({ title, description }) => (
<View style={styles.textContainer}>
{title ? <Text style={styles.title} numberOfLines={2}>{title}</Text> : null}
{description ? <Text style={styles.description} numberOfLines={2}>{description}</Text> : null}
</View>
));
const Url = React.memo(({
url, index, user, baseUrl
}) => {
if (!url) { if (!url) {
return null; return null;
} }
const onPress = () => openLink(url.url);
return ( return (
<Touchable <Touchable
onPress={() => onPress(url.url)} onPress={onPress}
style={[styles.button, index > 0 && styles.marginTop, styles.container]} style={[styles.button, index > 0 && styles.marginTop, styles.container]}
background={Touchable.Ripple('#fff')} background={Touchable.Ripple('#fff')}
> >
<React.Fragment> <React.Fragment>
{url.image ? <FastImage source={{ uri: url.image }} style={styles.image} resizeMode={FastImage.resizeMode.cover} /> : null} <UrlImage image={url.image} user={user} baseUrl={baseUrl} />
<View style={styles.textContainer}> <UrlContent title={url.title} description={url.description} />
<Text style={styles.title} numberOfLines={2}>{url.title}</Text>
<Text style={styles.description} numberOfLines={2}>{url.description}</Text>
</View>
</React.Fragment> </React.Fragment>
</Touchable> </Touchable>
); );
}, (oldProps, newProps) => isEqual(oldProps.url, newProps.url));
UrlImage.propTypes = {
image: PropTypes.string,
user: PropTypes.object,
baseUrl: PropTypes.string
};
UrlContent.propTypes = {
title: PropTypes.string,
description: PropTypes.string
}; };
Url.propTypes = { Url.propTypes = {
url: PropTypes.object.isRequired, url: PropTypes.object.isRequired,
index: PropTypes.number index: PropTypes.number,
user: PropTypes.object,
baseUrl: PropTypes.string
}; };
export default Url; export default Url;