2020-02-11 14:01:35 +00:00
|
|
|
import React from 'react';
|
2020-02-12 17:21:11 +00:00
|
|
|
import { StyleSheet, View } from 'react-native';
|
2020-02-11 14:01:35 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import isEqual from 'lodash/isEqual';
|
|
|
|
import { connect } from 'react-redux';
|
2020-07-20 16:35:17 +00:00
|
|
|
import { KeyboardAwareScrollView } from '@codler/react-native-keyboard-aware-scroll-view';
|
2020-02-11 14:01:35 +00:00
|
|
|
|
|
|
|
import { withTheme } from '../theme';
|
|
|
|
import EventEmitter from '../utils/events';
|
|
|
|
import { themes } from '../constants/colors';
|
|
|
|
import { CustomHeaderButtons, Item } from '../containers/HeaderButton';
|
|
|
|
import { modalBlockWithContext } from '../containers/UIKit/MessageBlock';
|
|
|
|
import RocketChat from '../lib/rocketchat';
|
|
|
|
import ActivityIndicator from '../containers/ActivityIndicator';
|
|
|
|
import { MODAL_ACTIONS, CONTAINER_TYPES } from '../lib/methods/actions';
|
|
|
|
|
|
|
|
import sharedStyles from './Styles';
|
|
|
|
import { textParser } from '../containers/UIKit/utils';
|
2020-02-18 15:56:02 +00:00
|
|
|
import Navigation from '../lib/Navigation';
|
2020-02-11 14:01:35 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
flex: 1,
|
|
|
|
paddingHorizontal: 16
|
|
|
|
},
|
|
|
|
content: {
|
|
|
|
paddingVertical: 16
|
|
|
|
},
|
|
|
|
submit: {
|
|
|
|
...sharedStyles.textSemibold,
|
|
|
|
fontSize: 16
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Object.fromEntries = Object.fromEntries || (arr => arr.reduce((acc, [k, v]) => ((acc[k] = v, acc)), {}));
|
|
|
|
const groupStateByBlockIdMap = (obj, [key, { blockId, value }]) => {
|
|
|
|
obj[blockId] = obj[blockId] || {};
|
|
|
|
obj[blockId][key] = value;
|
|
|
|
return obj;
|
|
|
|
};
|
|
|
|
const groupStateByBlockId = obj => Object.entries(obj).reduce(groupStateByBlockIdMap, {});
|
|
|
|
const filterInputFields = ({ element, elements = [] }) => {
|
|
|
|
if (element && element.initialValue) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (elements.length && elements.map(e => ({ element: e })).filter(filterInputFields).length) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const mapElementToState = ({ element, blockId, elements = [] }) => {
|
|
|
|
if (elements.length) {
|
|
|
|
return elements.map(e => ({ element: e, blockId })).filter(filterInputFields).map(mapElementToState);
|
|
|
|
}
|
|
|
|
return [element.actionId, { value: element.initialValue, blockId }];
|
|
|
|
};
|
|
|
|
const reduceState = (obj, el) => (Array.isArray(el[0]) ? { ...obj, ...Object.fromEntries(el) } : { ...obj, [el[0]]: el[1] });
|
|
|
|
|
|
|
|
class ModalBlockView extends React.Component {
|
2020-06-15 14:00:46 +00:00
|
|
|
static navigationOptions = ({ route }) => {
|
|
|
|
const data = route.params?.data;
|
2020-02-11 14:01:35 +00:00
|
|
|
const { view } = data;
|
2020-06-15 14:00:46 +00:00
|
|
|
const { title } = view;
|
2020-02-11 14:01:35 +00:00
|
|
|
return {
|
2020-06-15 14:00:46 +00:00
|
|
|
title: textParser([title])
|
2020-02-11 14:01:35 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
navigation: PropTypes.object,
|
2020-06-15 14:00:46 +00:00
|
|
|
route: PropTypes.object,
|
2020-02-11 14:01:35 +00:00
|
|
|
theme: PropTypes.string,
|
|
|
|
language: PropTypes.string,
|
|
|
|
user: PropTypes.shape({
|
|
|
|
id: PropTypes.string,
|
|
|
|
token: PropTypes.string
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2020-02-18 15:56:02 +00:00
|
|
|
this.submitting = false;
|
2020-06-15 14:00:46 +00:00
|
|
|
const data = props.route.params?.data;
|
2020-02-11 14:01:35 +00:00
|
|
|
this.values = data.view.blocks.filter(filterInputFields).map(mapElementToState).reduce(reduceState, {});
|
|
|
|
this.state = {
|
|
|
|
data,
|
|
|
|
loading: false
|
|
|
|
};
|
2020-06-15 14:00:46 +00:00
|
|
|
this.setHeader();
|
2020-02-11 14:01:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
const { data } = this.state;
|
|
|
|
const { viewId } = data;
|
|
|
|
EventEmitter.addEventListener(viewId, this.handleUpdate);
|
|
|
|
}
|
|
|
|
|
2020-02-17 16:42:34 +00:00
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
|
|
|
if (!isEqual(nextProps, this.props)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!isEqual(nextState, this.state)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-11 14:01:35 +00:00
|
|
|
componentDidUpdate(prevProps) {
|
2020-06-15 14:00:46 +00:00
|
|
|
const { navigation, route } = this.props;
|
|
|
|
const oldData = prevProps.route.params?.data ?? {};
|
|
|
|
const newData = route.params?.data ?? {};
|
2020-04-13 13:56:57 +00:00
|
|
|
if (oldData.viewId !== newData.viewId) {
|
2020-02-11 14:01:35 +00:00
|
|
|
navigation.push('ModalBlockView', { data: newData });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
const { data } = this.state;
|
|
|
|
const { viewId } = data;
|
|
|
|
EventEmitter.removeListener(viewId, this.handleUpdate);
|
|
|
|
}
|
|
|
|
|
2020-06-15 14:00:46 +00:00
|
|
|
setHeader = () => {
|
|
|
|
const { data } = this.state;
|
2020-04-13 13:56:57 +00:00
|
|
|
const { navigation } = this.props;
|
2020-06-15 14:00:46 +00:00
|
|
|
const { view } = data;
|
|
|
|
const { title, close, submit } = view;
|
|
|
|
navigation.setOptions({
|
|
|
|
title: textParser([title]),
|
|
|
|
headerLeft: close ? () => (
|
|
|
|
<CustomHeaderButtons>
|
|
|
|
<Item
|
|
|
|
title={textParser([close.text])}
|
|
|
|
style={styles.submit}
|
|
|
|
onPress={this.cancel}
|
|
|
|
testID='close-modal-uikit'
|
|
|
|
/>
|
|
|
|
</CustomHeaderButtons>
|
|
|
|
) : null,
|
|
|
|
headerRight: submit ? () => (
|
|
|
|
<CustomHeaderButtons>
|
|
|
|
<Item
|
|
|
|
title={textParser([submit.text])}
|
|
|
|
style={styles.submit}
|
|
|
|
onPress={this.submit}
|
|
|
|
testID='submit-modal-uikit'
|
|
|
|
/>
|
|
|
|
</CustomHeaderButtons>
|
|
|
|
) : null
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleUpdate = ({ type, ...data }) => {
|
2020-02-11 14:01:35 +00:00
|
|
|
if ([MODAL_ACTIONS.ERRORS].includes(type)) {
|
|
|
|
const { errors } = data;
|
|
|
|
this.setState({ errors });
|
|
|
|
} else {
|
|
|
|
this.setState({ data });
|
2020-06-15 14:00:46 +00:00
|
|
|
this.setHeader();
|
2020-02-11 14:01:35 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
cancel = async({ closeModal }) => {
|
|
|
|
const { data } = this.state;
|
|
|
|
const { appId, viewId, view } = data;
|
2020-02-18 15:56:02 +00:00
|
|
|
|
|
|
|
// handle tablet case
|
|
|
|
if (closeModal) {
|
|
|
|
closeModal();
|
|
|
|
} else {
|
|
|
|
Navigation.back();
|
|
|
|
}
|
|
|
|
|
2020-02-11 14:01:35 +00:00
|
|
|
try {
|
|
|
|
await RocketChat.triggerCancel({
|
|
|
|
appId,
|
|
|
|
viewId,
|
|
|
|
view: {
|
|
|
|
...view,
|
|
|
|
id: viewId,
|
|
|
|
state: groupStateByBlockId(this.values)
|
|
|
|
},
|
|
|
|
isCleared: true
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
submit = async() => {
|
|
|
|
const { data } = this.state;
|
2020-06-15 14:00:46 +00:00
|
|
|
if (this.submitting) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.submitting = true;
|
2020-02-18 15:56:02 +00:00
|
|
|
|
2020-02-11 14:01:35 +00:00
|
|
|
const { appId, viewId } = data;
|
|
|
|
this.setState({ loading: true });
|
|
|
|
try {
|
|
|
|
await RocketChat.triggerSubmitView({
|
|
|
|
viewId,
|
|
|
|
appId,
|
|
|
|
payload: {
|
|
|
|
view: {
|
|
|
|
id: viewId,
|
|
|
|
state: groupStateByBlockId(this.values)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
// do nothing
|
|
|
|
}
|
2020-02-18 15:56:02 +00:00
|
|
|
|
2020-06-15 14:00:46 +00:00
|
|
|
this.submitting = false;
|
2020-02-11 14:01:35 +00:00
|
|
|
this.setState({ loading: false });
|
|
|
|
};
|
|
|
|
|
|
|
|
action = async({ actionId, value, blockId }) => {
|
|
|
|
const { data } = this.state;
|
|
|
|
const { mid, appId, viewId } = data;
|
|
|
|
await RocketChat.triggerBlockAction({
|
|
|
|
container: {
|
|
|
|
type: CONTAINER_TYPES.VIEW,
|
|
|
|
id: viewId
|
|
|
|
},
|
|
|
|
actionId,
|
|
|
|
appId,
|
|
|
|
value,
|
|
|
|
blockId,
|
|
|
|
mid
|
|
|
|
});
|
|
|
|
this.changeState({ actionId, value, blockId });
|
|
|
|
}
|
|
|
|
|
|
|
|
changeState = ({ actionId, value, blockId = 'default' }) => {
|
|
|
|
this.values[actionId] = {
|
|
|
|
blockId,
|
|
|
|
value
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { data, loading, errors } = this.state;
|
|
|
|
const { theme, language } = this.props;
|
|
|
|
const { values } = this;
|
|
|
|
const { view } = data;
|
|
|
|
const { blocks } = view;
|
|
|
|
|
|
|
|
return (
|
2020-02-12 17:21:11 +00:00
|
|
|
<KeyboardAwareScrollView
|
2020-02-11 14:01:35 +00:00
|
|
|
style={[
|
|
|
|
styles.container,
|
|
|
|
{ backgroundColor: themes[theme].auxiliaryBackground }
|
|
|
|
]}
|
|
|
|
keyboardShouldPersistTaps='always'
|
|
|
|
>
|
|
|
|
<View style={styles.content}>
|
|
|
|
{
|
|
|
|
React.createElement(
|
|
|
|
modalBlockWithContext({
|
|
|
|
action: this.action,
|
|
|
|
state: this.changeState,
|
|
|
|
...data
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
blocks,
|
|
|
|
errors,
|
|
|
|
language,
|
|
|
|
values
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
</View>
|
|
|
|
{loading ? <ActivityIndicator absolute size='large' theme={theme} /> : null}
|
2020-02-12 17:21:11 +00:00
|
|
|
</KeyboardAwareScrollView>
|
2020-02-11 14:01:35 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
|
|
|
language: state.login.user && state.login.user.language
|
|
|
|
});
|
|
|
|
|
|
|
|
export default connect(mapStateToProps)(withTheme(ModalBlockView));
|