MM-43904 - Fix: Calls batch actions (#6229) (#6233)

* fix batch actions

* tests

(cherry picked from commit 67c65156a7)

Co-authored-by: Christopher Poile <cpoile@gmail.com>
This commit is contained in:
Mattermost Build 2022-05-05 20:13:30 +02:00 committed by GitHub
parent 696f8d69a3
commit 491209fdae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 16 deletions

View file

@ -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);

View file

@ -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';
@ -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;
@ -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> = {};
@ -86,7 +92,8 @@ export function loadCalls(): ActionFunc {
enabled: enabledChannels,
};
return {type: CallsTypes.RECEIVED_CALLS, data};
dispatch({type: CallsTypes.RECEIVED_CALLS, data});
return {data};
};
}
@ -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 {};