import React, { Component } from 'react';
import {
View, Text, StyleSheet, Image, ScrollView, Platform, TouchableHighlight
} from 'react-native';
import PropTypes from 'prop-types';
import Modal from 'react-native-modal';
import { responsive } from 'react-native-responsive-ui';
import equal from 'deep-equal';
import TextInput from '../TextInput';
import Button from '../Button';
import I18n from '../../i18n';
import sharedStyles from '../../views/Styles';
const cancelButtonColor = '#f7f8fa';
const styles = StyleSheet.create({
modal: {
alignItems: 'center'
},
titleContainer: {
flexDirection: 'row',
paddingHorizontal: 16,
paddingTop: 16
},
title: {
...sharedStyles.textBold
},
container: {
height: 430,
backgroundColor: '#ffffff',
flexDirection: 'column'
},
scrollView: {
flex: 1,
padding: 16
},
image: {
height: 150,
flex: 1,
marginBottom: 16,
resizeMode: 'contain'
},
buttonContainer: {
flexDirection: 'row',
justifyContent: 'space-between',
padding: 16,
backgroundColor: '#f7f8fa'
},
button: {
marginBottom: 0
},
androidButton: {
paddingHorizontal: 15,
justifyContent: 'center',
height: 48,
borderRadius: 2
},
androidButtonText: {
fontSize: 18,
textAlign: 'center'
}
});
@responsive
export default class UploadModal extends Component {
static propTypes = {
isVisible: PropTypes.bool,
file: PropTypes.object,
close: PropTypes.func,
submit: PropTypes.func,
window: PropTypes.object
}
state = {
name: '',
description: '',
file: {}
};
static getDerivedStateFromProps(props, state) {
if (!equal(props.file, state.file) && props.file && props.file.path) {
return {
file: props.file,
name: props.file.filename || 'Filename',
description: ''
};
}
return null;
}
shouldComponentUpdate(nextProps, nextState) {
const { name, description, file } = this.state;
const { window, isVisible } = this.props;
if (nextState.name !== name) {
return true;
}
if (nextState.description !== description) {
return true;
}
if (nextProps.isVisible !== isVisible) {
return true;
}
if (nextProps.window.width !== window.width) {
return true;
}
if (!equal(nextState.file, file)) {
return true;
}
return false;
}
submit = () => {
const { file, submit } = this.props;
const { name, description } = this.state;
submit({ ...file, name, description });
}
renderButtons = () => {
const { close } = this.props;
if (Platform.OS === 'ios') {
return (
);
}
// FIXME: RNGH don't work well on Android modals: https://github.com/kmagiera/react-native-gesture-handler/issues/139
return (
{I18n.t('Cancel')}
{I18n.t('Send')}
);
}
render() {
const { window: { width }, isVisible, close } = this.props;
const { name, description, file } = this.state;
return (
{I18n.t('Upload_file_question_mark')}
this.setState({ name: value })}
/>
this.setState({ description: value })}
/>
{this.renderButtons()}
);
}
}