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';
|
2021-12-13 16:28:29 +00:00
|
|
|
import { StackNavigationOptions, StackNavigationProp } from '@react-navigation/stack';
|
|
|
|
import { RouteProp } from '@react-navigation/native';
|
2020-02-11 14:01:35 +00:00
|
|
|
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';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { themes } from '../lib/constants';
|
2020-10-30 16:15:58 +00:00
|
|
|
import * as HeaderButton from '../containers/HeaderButton';
|
2020-02-11 14:01:35 +00:00
|
|
|
import { modalBlockWithContext } from '../containers/UIKit/MessageBlock';
|
|
|
|
import RocketChat from '../lib/rocketchat';
|
|
|
|
import ActivityIndicator from '../containers/ActivityIndicator';
|
|
|
|
import { textParser } from '../containers/UIKit/utils';
|
2022-04-07 13:22:19 +00:00
|
|
|
import Navigation from '../lib/navigation/appNavigation';
|
2021-12-13 16:28:29 +00:00
|
|
|
import { MasterDetailInsideStackParamList } from '../stacks/MasterDetailStack/types';
|
2022-03-16 19:07:49 +00:00
|
|
|
import { ContainerTypes, ModalActions } from '../containers/UIKit/interfaces';
|
2020-02-11 14:01:35 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
flex: 1,
|
|
|
|
paddingHorizontal: 16
|
|
|
|
},
|
|
|
|
content: {
|
|
|
|
paddingVertical: 16
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-12-13 16:28:29 +00:00
|
|
|
interface IValueBlockId {
|
|
|
|
value: string;
|
|
|
|
blockId: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
type TElementToState = [string, IValueBlockId];
|
|
|
|
interface IActions {
|
|
|
|
actionId: string;
|
|
|
|
value: any;
|
|
|
|
blockId?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IValues {
|
|
|
|
[key: string]: {
|
|
|
|
[key: string]: string;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
interface IModalBlockViewState {
|
|
|
|
data: any;
|
|
|
|
loading: boolean;
|
|
|
|
errors?: any;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IModalBlockViewProps {
|
|
|
|
navigation: StackNavigationProp<MasterDetailInsideStackParamList, 'ModalBlockView'>;
|
|
|
|
route: RouteProp<MasterDetailInsideStackParamList, 'ModalBlockView'>;
|
|
|
|
theme: string;
|
|
|
|
language: string;
|
|
|
|
user: {
|
|
|
|
id: string;
|
|
|
|
token: string;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-sequences
|
|
|
|
Object.fromEntries = Object.fromEntries || ((arr: any[]) => arr.reduce((acc, [k, v]) => ((acc[k] = v), acc), {}));
|
|
|
|
const groupStateByBlockIdMap = (obj: any, [key, { blockId, value }]: TElementToState) => {
|
2020-02-11 14:01:35 +00:00
|
|
|
obj[blockId] = obj[blockId] || {};
|
|
|
|
obj[blockId][key] = value;
|
|
|
|
return obj;
|
|
|
|
};
|
2021-12-13 16:28:29 +00:00
|
|
|
const groupStateByBlockId = (obj: { [key: string]: any }) => Object.entries(obj).reduce(groupStateByBlockIdMap, {});
|
|
|
|
const filterInputFields = ({ element, elements = [] }: { element: any; elements?: any[] }) => {
|
2020-02-11 14:01:35 +00:00
|
|
|
if (element && element.initialValue) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (elements.length && elements.map(e => ({ element: e })).filter(filterInputFields).length) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
2021-12-13 16:28:29 +00:00
|
|
|
|
|
|
|
const mapElementToState = ({ element, blockId, elements = [] }: { element: any; blockId: string; elements?: any[] }): any => {
|
2020-02-11 14:01:35 +00:00
|
|
|
if (elements.length) {
|
2021-09-13 20:41:05 +00:00
|
|
|
return elements
|
|
|
|
.map(e => ({ element: e, blockId }))
|
|
|
|
.filter(filterInputFields)
|
|
|
|
.map(mapElementToState);
|
2020-02-11 14:01:35 +00:00
|
|
|
}
|
|
|
|
return [element.actionId, { value: element.initialValue, blockId }];
|
|
|
|
};
|
2021-12-13 16:28:29 +00:00
|
|
|
const reduceState = (obj: any, el: any) =>
|
|
|
|
Array.isArray(el[0]) ? { ...obj, ...Object.fromEntries(el) } : { ...obj, [el[0]]: el[1] };
|
2020-02-11 14:01:35 +00:00
|
|
|
|
2021-12-13 16:28:29 +00:00
|
|
|
class ModalBlockView extends React.Component<IModalBlockViewProps, IModalBlockViewState> {
|
|
|
|
private submitting: boolean;
|
|
|
|
|
|
|
|
private values: IValues;
|
|
|
|
|
|
|
|
static navigationOptions = ({ route }: Pick<IModalBlockViewProps, 'route'>): StackNavigationOptions => {
|
2020-06-15 14:00:46 +00:00
|
|
|
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
|
|
|
};
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-02-11 14:01:35 +00:00
|
|
|
|
2021-12-13 16:28:29 +00:00
|
|
|
constructor(props: IModalBlockViewProps) {
|
2020-02-11 14:01:35 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-12-13 16:28:29 +00:00
|
|
|
componentDidUpdate(prevProps: IModalBlockViewProps) {
|
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]),
|
2021-09-13 20:41:05 +00:00
|
|
|
headerLeft: close
|
|
|
|
? () => (
|
|
|
|
<HeaderButton.Container>
|
2022-01-17 16:10:39 +00:00
|
|
|
<HeaderButton.Item title={textParser([close.text])} onPress={this.cancel} testID='close-modal-uikit' />
|
2021-09-13 20:41:05 +00:00
|
|
|
</HeaderButton.Container>
|
|
|
|
)
|
2021-12-13 16:28:29 +00:00
|
|
|
: undefined,
|
2021-09-13 20:41:05 +00:00
|
|
|
headerRight: submit
|
|
|
|
? () => (
|
|
|
|
<HeaderButton.Container>
|
2022-01-17 16:10:39 +00:00
|
|
|
<HeaderButton.Item title={textParser([submit.text])} onPress={this.submit} testID='submit-modal-uikit' />
|
2021-09-13 20:41:05 +00:00
|
|
|
</HeaderButton.Container>
|
|
|
|
)
|
2021-12-13 16:28:29 +00:00
|
|
|
: undefined
|
2020-06-15 14:00:46 +00:00
|
|
|
});
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-06-15 14:00:46 +00:00
|
|
|
|
2022-03-16 19:07:49 +00:00
|
|
|
handleUpdate = ({ type, ...data }: { type: ModalActions }) => {
|
|
|
|
if ([ModalActions.ERRORS].includes(type)) {
|
2021-12-13 16:28:29 +00:00
|
|
|
const { errors }: any = data;
|
2020-02-11 14:01:35 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-12-13 16:28:29 +00:00
|
|
|
cancel = async ({ closeModal }: { closeModal?: () => void }) => {
|
2020-02-11 14:01:35 +00:00
|
|
|
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
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-02-11 14:01:35 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
submit = async () => {
|
2020-02-11 14:01:35 +00:00
|
|
|
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 });
|
|
|
|
};
|
|
|
|
|
2021-12-13 16:28:29 +00:00
|
|
|
action = async ({ actionId, value, blockId }: IActions) => {
|
2020-02-11 14:01:35 +00:00
|
|
|
const { data } = this.state;
|
|
|
|
const { mid, appId, viewId } = data;
|
|
|
|
await RocketChat.triggerBlockAction({
|
|
|
|
container: {
|
2022-03-16 19:07:49 +00:00
|
|
|
type: ContainerTypes.VIEW,
|
2020-02-11 14:01:35 +00:00
|
|
|
id: viewId
|
|
|
|
},
|
|
|
|
actionId,
|
|
|
|
appId,
|
|
|
|
value,
|
|
|
|
blockId,
|
|
|
|
mid
|
|
|
|
});
|
|
|
|
this.changeState({ actionId, value, blockId });
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-02-11 14:01:35 +00:00
|
|
|
|
2021-12-13 16:28:29 +00:00
|
|
|
changeState = ({ actionId, value, blockId = 'default' }: IActions) => {
|
2020-02-11 14:01:35 +00:00
|
|
|
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
|
2021-09-13 20:41:05 +00:00
|
|
|
style={[styles.container, { backgroundColor: themes[theme].auxiliaryBackground }]}
|
|
|
|
keyboardShouldPersistTaps='always'>
|
2020-02-11 14:01:35 +00:00
|
|
|
<View style={styles.content}>
|
2021-09-13 20:41:05 +00:00
|
|
|
{React.createElement(
|
|
|
|
modalBlockWithContext({
|
|
|
|
action: this.action,
|
|
|
|
state: this.changeState,
|
|
|
|
...data
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
blocks,
|
|
|
|
errors,
|
|
|
|
language,
|
|
|
|
values
|
|
|
|
}
|
|
|
|
)}
|
2020-02-11 14:01:35 +00:00
|
|
|
</View>
|
2022-03-18 02:37:10 +00:00
|
|
|
{loading ? <ActivityIndicator absolute size='large' /> : null}
|
2020-02-12 17:21:11 +00:00
|
|
|
</KeyboardAwareScrollView>
|
2020-02-11 14:01:35 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-13 16:28:29 +00:00
|
|
|
const mapStateToProps = (state: any) => ({
|
2020-02-11 14:01:35 +00:00
|
|
|
language: state.login.user && state.login.user.language
|
|
|
|
});
|
|
|
|
|
|
|
|
export default connect(mapStateToProps)(withTheme(ModalBlockView));
|