[FIX] UIKit submit when connection lost (#1748)

This commit is contained in:
Djorkaeff Alexandre 2020-02-18 12:56:02 -03:00 committed by GitHub
parent aaca4ad268
commit 78aef7320e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 27 deletions

View File

@ -6,6 +6,12 @@ function setTopLevelNavigator(navigatorRef) {
_navigator = navigatorRef; _navigator = navigatorRef;
} }
function back() {
_navigator.dispatch(
NavigationActions.back()
);
}
function navigate(routeName, params) { function navigate(routeName, params) {
_navigator.dispatch( _navigator.dispatch(
NavigationActions.navigate({ NavigationActions.navigate({
@ -16,6 +22,7 @@ function navigate(routeName, params) {
} }
export default { export default {
back,
navigate, navigate,
setTopLevelNavigator setTopLevelNavigator
}; };

View File

@ -2,8 +2,6 @@ import random from '../../utils/random';
import EventEmitter from '../../utils/events'; import EventEmitter from '../../utils/events';
import Navigation from '../Navigation'; import Navigation from '../Navigation';
const TRIGGER_TIMEOUT = 5000;
const ACTION_TYPES = { const ACTION_TYPES = {
ACTION: 'blockAction', ACTION: 'blockAction',
SUBMIT: 'viewSubmit', SUBMIT: 'viewSubmit',
@ -34,7 +32,7 @@ const invalidateTriggerId = (id) => {
export const generateTriggerId = (appId) => { export const generateTriggerId = (appId) => {
const triggerId = random(17); const triggerId = random(17);
triggersId.set(triggerId, appId); triggersId.set(triggerId, appId);
setTimeout(invalidateTriggerId, TRIGGER_TIMEOUT, triggerId);
return triggerId; return triggerId;
}; };
@ -105,12 +103,10 @@ export function triggerAction({
const payload = rest.payload || rest; const payload = rest.payload || rest;
setTimeout(reject, TRIGGER_TIMEOUT, triggerId);
const { userId, authToken } = this.sdk.currentLogin;
const { host } = this.sdk.client;
try { try {
const { userId, authToken } = this.sdk.currentLogin;
const { host } = this.sdk.client;
// we need to use fetch because this.sdk.post add /v1 to url // we need to use fetch because this.sdk.post add /v1 to url
const result = await fetch(`${ host }/api/apps/ui.interaction/${ appId }/`, { const result = await fetch(`${ host }/api/apps/ui.interaction/${ appId }/`, {
method: 'POST', method: 'POST',
@ -133,11 +129,7 @@ export function triggerAction({
try { try {
const { type: interactionType, ...data } = await result.json(); const { type: interactionType, ...data } = await result.json();
handlePayloadUserInteraction(interactionType, data); return resolve(handlePayloadUserInteraction(interactionType, data));
if (data.success) {
return resolve();
}
} catch (e) { } catch (e) {
// modal.close has no body, so result.json will fail // modal.close has no body, so result.json will fail
// but it returns ok status // but it returns ok status
@ -156,8 +148,11 @@ export default function triggerBlockAction(options) {
return triggerAction.call(this, { type: ACTION_TYPES.ACTION, ...options }); return triggerAction.call(this, { type: ACTION_TYPES.ACTION, ...options });
} }
export function triggerSubmitView({ viewId, ...options }) { export async function triggerSubmitView({ viewId, ...options }) {
return triggerAction.call(this, { type: ACTION_TYPES.SUBMIT, viewId, ...options }); const result = await triggerAction.call(this, { type: ACTION_TYPES.SUBMIT, viewId, ...options });
if (!result || MODAL_ACTIONS.CLOSE === result) {
Navigation.back();
}
} }
export function triggerCancel({ view, ...options }) { export function triggerCancel({ view, ...options }) {

View File

@ -17,6 +17,7 @@ import { MODAL_ACTIONS, CONTAINER_TYPES } from '../lib/methods/actions';
import sharedStyles from './Styles'; import sharedStyles from './Styles';
import { textParser } from '../containers/UIKit/utils'; import { textParser } from '../containers/UIKit/utils';
import Navigation from '../lib/Navigation';
const styles = StyleSheet.create({ const styles = StyleSheet.create({
container: { container: {
@ -60,6 +61,7 @@ class ModalBlockView extends React.Component {
const { theme, closeModal } = screenProps; const { theme, closeModal } = screenProps;
const data = navigation.getParam('data'); const data = navigation.getParam('data');
const cancel = navigation.getParam('cancel', () => {}); const cancel = navigation.getParam('cancel', () => {});
const submitting = navigation.getParam('submitting', false);
const { view } = data; const { view } = data;
const { title, submit, close } = view; const { title, submit, close } = view;
return { return {
@ -70,7 +72,7 @@ class ModalBlockView extends React.Component {
<Item <Item
title={textParser([close.text])} title={textParser([close.text])}
style={styles.submit} style={styles.submit}
onPress={() => cancel({ closeModal })} onPress={!submitting && (() => cancel({ closeModal }))}
testID='close-modal-uikit' testID='close-modal-uikit'
/> />
</CustomHeaderButtons> </CustomHeaderButtons>
@ -80,7 +82,7 @@ class ModalBlockView extends React.Component {
<Item <Item
title={textParser([submit.text])} title={textParser([submit.text])}
style={styles.submit} style={styles.submit}
onPress={navigation.getParam('submit', () => {})} onPress={!submitting && (navigation.getParam('submit', () => {}))}
testID='submit-modal-uikit' testID='submit-modal-uikit'
/> />
</CustomHeaderButtons> </CustomHeaderButtons>
@ -100,6 +102,7 @@ class ModalBlockView extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.submitting = false;
const { navigation } = props; const { navigation } = props;
const data = navigation.getParam('data'); const data = navigation.getParam('data');
this.values = data.view.blocks.filter(filterInputFields).map(mapElementToState).reduce(reduceState, {}); this.values = data.view.blocks.filter(filterInputFields).map(mapElementToState).reduce(reduceState, {});
@ -155,9 +158,15 @@ class ModalBlockView extends React.Component {
cancel = async({ closeModal }) => { cancel = async({ closeModal }) => {
const { data } = this.state; const { data } = this.state;
const { navigation } = this.props;
const { appId, viewId, view } = data; const { appId, viewId, view } = data;
this.setState({ loading: true });
// handle tablet case
if (closeModal) {
closeModal();
} else {
Navigation.back();
}
try { try {
await RocketChat.triggerCancel({ await RocketChat.triggerCancel({
appId, appId,
@ -172,18 +181,13 @@ class ModalBlockView extends React.Component {
} catch (e) { } catch (e) {
// do nothing // do nothing
} }
// handle tablet case
if (closeModal) {
closeModal();
} else {
navigation.pop();
}
this.setState({ loading: false });
} }
submit = async() => { submit = async() => {
const { data } = this.state; const { data } = this.state;
const { navigation } = this.props; const { navigation } = this.props;
navigation.setParams({ submitting: true });
const { appId, viewId } = data; const { appId, viewId } = data;
this.setState({ loading: true }); this.setState({ loading: true });
try { try {
@ -197,10 +201,11 @@ class ModalBlockView extends React.Component {
} }
} }
}); });
navigation.pop();
} catch (e) { } catch (e) {
// do nothing // do nothing
} }
navigation.setParams({ submitting: false });
this.setState({ loading: false }); this.setState({ loading: false });
}; };