Skip to content

Commit

Permalink
[FIX] UIKit submit when connection lost (#1748)
Browse files Browse the repository at this point in the history
  • Loading branch information
djorkaeffalexandre authored Feb 18, 2020
1 parent aaca4ad commit 78aef73
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 27 deletions.
7 changes: 7 additions & 0 deletions app/lib/Navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ function setTopLevelNavigator(navigatorRef) {
_navigator = navigatorRef;
}

function back() {
_navigator.dispatch(
NavigationActions.back()
);
}

function navigate(routeName, params) {
_navigator.dispatch(
NavigationActions.navigate({
Expand All @@ -16,6 +22,7 @@ function navigate(routeName, params) {
}

export default {
back,
navigate,
setTopLevelNavigator
};
25 changes: 10 additions & 15 deletions app/lib/methods/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import random from '../../utils/random';
import EventEmitter from '../../utils/events';
import Navigation from '../Navigation';

const TRIGGER_TIMEOUT = 5000;

const ACTION_TYPES = {
ACTION: 'blockAction',
SUBMIT: 'viewSubmit',
Expand Down Expand Up @@ -34,7 +32,7 @@ const invalidateTriggerId = (id) => {
export const generateTriggerId = (appId) => {
const triggerId = random(17);
triggersId.set(triggerId, appId);
setTimeout(invalidateTriggerId, TRIGGER_TIMEOUT, triggerId);

return triggerId;
};

Expand Down Expand Up @@ -105,12 +103,10 @@ export function triggerAction({

const payload = rest.payload || rest;

setTimeout(reject, TRIGGER_TIMEOUT, triggerId);

const { userId, authToken } = this.sdk.currentLogin;
const { host } = this.sdk.client;

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
const result = await fetch(`${ host }/api/apps/ui.interaction/${ appId }/`, {
method: 'POST',
Expand All @@ -133,11 +129,7 @@ export function triggerAction({

try {
const { type: interactionType, ...data } = await result.json();
handlePayloadUserInteraction(interactionType, data);

if (data.success) {
return resolve();
}
return resolve(handlePayloadUserInteraction(interactionType, data));
} catch (e) {
// modal.close has no body, so result.json will fail
// but it returns ok status
Expand All @@ -156,8 +148,11 @@ export default function triggerBlockAction(options) {
return triggerAction.call(this, { type: ACTION_TYPES.ACTION, ...options });
}

export function triggerSubmitView({ viewId, ...options }) {
return triggerAction.call(this, { type: ACTION_TYPES.SUBMIT, viewId, ...options });
export async function triggerSubmitView({ 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 }) {
Expand Down
29 changes: 17 additions & 12 deletions app/views/ModalBlockView.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { MODAL_ACTIONS, CONTAINER_TYPES } from '../lib/methods/actions';

import sharedStyles from './Styles';
import { textParser } from '../containers/UIKit/utils';
import Navigation from '../lib/Navigation';

const styles = StyleSheet.create({
container: {
Expand Down Expand Up @@ -60,6 +61,7 @@ class ModalBlockView extends React.Component {
const { theme, closeModal } = screenProps;
const data = navigation.getParam('data');
const cancel = navigation.getParam('cancel', () => {});
const submitting = navigation.getParam('submitting', false);
const { view } = data;
const { title, submit, close } = view;
return {
Expand All @@ -70,7 +72,7 @@ class ModalBlockView extends React.Component {
<Item
title={textParser([close.text])}
style={styles.submit}
onPress={() => cancel({ closeModal })}
onPress={!submitting && (() => cancel({ closeModal }))}
testID='close-modal-uikit'
/>
</CustomHeaderButtons>
Expand All @@ -80,7 +82,7 @@ class ModalBlockView extends React.Component {
<Item
title={textParser([submit.text])}
style={styles.submit}
onPress={navigation.getParam('submit', () => {})}
onPress={!submitting && (navigation.getParam('submit', () => {}))}
testID='submit-modal-uikit'
/>
</CustomHeaderButtons>
Expand All @@ -100,6 +102,7 @@ class ModalBlockView extends React.Component {

constructor(props) {
super(props);
this.submitting = false;
const { navigation } = props;
const data = navigation.getParam('data');
this.values = data.view.blocks.filter(filterInputFields).map(mapElementToState).reduce(reduceState, {});
Expand Down Expand Up @@ -155,9 +158,15 @@ class ModalBlockView extends React.Component {

cancel = async({ closeModal }) => {
const { data } = this.state;
const { navigation } = this.props;
const { appId, viewId, view } = data;
this.setState({ loading: true });

// handle tablet case
if (closeModal) {
closeModal();
} else {
Navigation.back();
}

try {
await RocketChat.triggerCancel({
appId,
Expand All @@ -172,18 +181,13 @@ class ModalBlockView extends React.Component {
} catch (e) {
// do nothing
}
// handle tablet case
if (closeModal) {
closeModal();
} else {
navigation.pop();
}
this.setState({ loading: false });
}

submit = async() => {
const { data } = this.state;
const { navigation } = this.props;
navigation.setParams({ submitting: true });

const { appId, viewId } = data;
this.setState({ loading: true });
try {
Expand All @@ -197,10 +201,11 @@ class ModalBlockView extends React.Component {
}
}
});
navigation.pop();
} catch (e) {
// do nothing
}

navigation.setParams({ submitting: false });
this.setState({ loading: false });
};

Expand Down

0 comments on commit 78aef73

Please sign in to comment.