Skip to content

Commit

Permalink
MM-43904 - Fix: Calls batch actions (#6229) (#6232)
Browse files Browse the repository at this point in the history
* fix batch actions

* tests

(cherry picked from commit 67c6515)

Co-authored-by: Christopher Poile <cpoile@gmail.com>
  • Loading branch information
mattermost-build and cpoile authored May 5, 2022
1 parent a046e70 commit b13ed0f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
4 changes: 2 additions & 2 deletions app/products/calls/store/actions/calls.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,14 @@ describe('Actions.Calls', () => {
});

it('loadCalls', async () => {
await store.dispatch(await store.dispatch(CallsActions.loadCalls()));
await store.dispatch(CallsActions.loadCalls());
expect(Client4.getCalls).toBeCalledWith();
assert.equal(store.getState().entities.calls.calls['channel-1'].channelId, 'channel-1');
assert.equal(store.getState().entities.calls.enabled['channel-1'], true);
});

it('loadConfig', async () => {
await store.dispatch(await store.dispatch(CallsActions.loadConfig()));
await store.dispatch(CallsActions.loadConfig());
expect(Client4.getCallsConfig).toBeCalledWith();
assert.equal(store.getState().entities.calls.config.DefaultEnabled, true);
assert.equal(store.getState().entities.calls.config.AllowEnableCalls, true);
Expand Down
35 changes: 21 additions & 14 deletions app/products/calls/store/actions/calls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@

import {intlShape} from 'react-intl';
import InCallManager from 'react-native-incall-manager';
import {batch} from 'react-redux';

import {Client4} from '@client/rest';
import Calls from '@constants/calls';
import {logError} from '@mm-redux/actions/errors';
import {forceLogoutIfNecessary} from '@mm-redux/actions/helpers';
import {GenericAction, ActionFunc, DispatchFunc, GetStateFunc, batchActions} from '@mm-redux/types/actions';
import {
GenericAction,
ActionFunc,
DispatchFunc,
GetStateFunc,
ActionResult,
} from '@mm-redux/types/actions';
import {Dictionary} from '@mm-redux/types/utilities';
import {newClient} from '@mmproducts/calls/connection';
import CallsTypes from '@mmproducts/calls/store/action_types/calls';
Expand All @@ -19,7 +26,7 @@ import {hasMicrophonePermission} from '@utils/permission';
export let ws: any = null;

export function loadConfig(force = false): ActionFunc {
return async (dispatch: DispatchFunc, getState: GetStateFunc): Promise<GenericAction> => {
return async (dispatch: DispatchFunc, getState: GetStateFunc): Promise<ActionResult> => {
if (!force) {
if ((Date.now() - getConfig(getState()).last_retrieved_at) < Calls.RefreshConfigMillis) {
return {} as GenericAction;
Expand All @@ -34,28 +41,27 @@ export function loadConfig(force = false): ActionFunc {
dispatch(logError(error));

// Reset the config to the default (off) since it looks like Calls is not enabled.
return {
dispatch({
type: CallsTypes.RECEIVED_CONFIG,
data: {...DefaultServerConfig, last_retrieved_at: Date.now()},
};
});
}

return {
type: CallsTypes.RECEIVED_CONFIG,
data: {...data, last_retrieved_at: Date.now()},
};
data = {...data, last_retrieved_at: Date.now()};
dispatch({type: CallsTypes.RECEIVED_CONFIG, data});
return {data};
};
}

export function loadCalls(): ActionFunc {
return async (dispatch: DispatchFunc, getState: GetStateFunc): Promise<GenericAction> => {
return async (dispatch: DispatchFunc, getState: GetStateFunc): Promise<ActionResult> => {
let resp = [];
try {
resp = await Client4.getCalls();
} catch (error) {
forceLogoutIfNecessary(error, dispatch, getState);
dispatch(logError(error));
return {} as GenericAction;
return {};
}

const callsResults: Dictionary<Call> = {};
Expand Down Expand Up @@ -86,7 +92,8 @@ export function loadCalls(): ActionFunc {
enabled: enabledChannels,
};

return {type: CallsTypes.RECEIVED_CALLS, data};
dispatch({type: CallsTypes.RECEIVED_CALLS, data});
return {data};
};
}

Expand All @@ -98,9 +105,9 @@ export function batchLoadCalls(forceConfig = false): ActionFunc {
return {};
}

const promises = [dispatch(loadConfig(forceConfig)), dispatch(loadCalls())];
Promise.all(promises).then((actions: Array<Awaited<GenericAction>>) => {
dispatch(batchActions(actions, 'BATCH_LOAD_CALLS'));
batch(() => {
dispatch(loadConfig(forceConfig));
dispatch(loadCalls());
});

return {};
Expand Down

0 comments on commit b13ed0f

Please sign in to comment.