Commit 72336d6a authored by Joseph Samela's avatar Joseph Samela Committed by Harrison Healey

Remove unused status flags in requests/general.js (#952)

parent 43131529
......@@ -9,29 +9,17 @@ export default keyMirror({
REMOVED_APP_CREDENTIALS: null,
RECEIVED_APP_DEVICE_TOKEN: null,
PING_REQUEST: null,
PING_SUCCESS: null,
PING_FAILURE: null,
PING_RESET: null,
RECEIVED_SERVER_VERSION: null,
CLIENT_CONFIG_REQUEST: null,
CLIENT_CONFIG_SUCCESS: null,
CLIENT_CONFIG_FAILURE: null,
CLIENT_CONFIG_RECEIVED: null,
CLIENT_CONFIG_RESET: null,
CLIENT_LICENSE_REQUEST: null,
CLIENT_LICENSE_SUCCESS: null,
CLIENT_LICENSE_FAILURE: null,
CLIENT_LICENSE_RECEIVED: null,
CLIENT_LICENSE_RESET: null,
RECEIVED_DATA_RETENTION_POLICY: null,
DATA_RETENTION_POLICY_REQUEST: null,
DATA_RETENTION_POLICY_SUCCESS: null,
DATA_RETENTION_POLICY_FAILURE: null,
LOG_CLIENT_ERROR_REQUEST: null,
LOG_CLIENT_ERROR_SUCCESS: null,
......@@ -47,7 +35,6 @@ export default keyMirror({
WEBSOCKET_FAILURE: null,
WEBSOCKET_CLOSED: null,
REDIRECT_LOCATION_REQUEST: null,
REDIRECT_LOCATION_SUCCESS: null,
REDIRECT_LOCATION_FAILURE: null,
});
......@@ -4,10 +4,10 @@
import assert from 'assert';
import nock from 'nock';
import {FormattedError} from './helpers.ts';
import {GeneralTypes} from 'action_types';
import * as Actions from 'actions/general';
import {Client4} from 'client';
import {RequestStatus} from '../constants';
import TestHelper from 'test/test_helper';
import configureStore from 'test/test_store';
......@@ -29,25 +29,35 @@ describe('Actions.General', () => {
it('getPing - Invalid URL', async () => {
const serverUrl = Client4.getUrl();
Client4.setUrl('notarealurl');
await Actions.getPing(true)(store.dispatch, store.getState);
const {server} = store.getState().requests.general;
assert.ok(server.status === RequestStatus.FAILURE && server.error);
const pingError = new FormattedError(
'mobile.server_ping_failed',
'Cannot connect to the server. Please check your server URL and internet connection.'
);
nock(Client4.getBaseRoute()).
get('/system/ping').
query(true).
reply(401, {error: 'ping error', code: 401});
const {error} = await Actions.getPing()(store.dispatch, store.getState);
Client4.setUrl(serverUrl);
assert.deepEqual(error, pingError);
});
it('getPing', async () => {
const response = {
status: 'OK',
version: '4.0.0',
};
nock(Client4.getBaseRoute()).
get('/system/ping').
query(true).
reply(200, {status: 'OK', version: '4.0.0'});
reply(200, response);
await Actions.getPing()(store.dispatch, store.getState);
const {server} = store.getState().requests.general;
if (server.status === RequestStatus.FAILURE) {
throw new Error(JSON.stringify(server.error));
}
const {data} = await Actions.getPing()(store.dispatch, store.getState);
assert.deepEqual(data, response);
});
it('getClientConfig', async () => {
......@@ -58,11 +68,6 @@ describe('Actions.General', () => {
await Actions.getClientConfig()(store.dispatch, store.getState);
const configRequest = store.getState().requests.general.config;
if (configRequest.status === RequestStatus.FAILURE) {
throw new Error(JSON.stringify(configRequest.error));
}
const clientConfig = store.getState().entities.general.config;
// Check a few basic fields since they may change over time
......@@ -80,11 +85,6 @@ describe('Actions.General', () => {
await Actions.getLicenseConfig()(store.dispatch, store.getState);
const licenseRequest = store.getState().requests.general.license;
if (licenseRequest.status === RequestStatus.FAILURE) {
throw new Error(JSON.stringify(licenseRequest.error));
}
const licenseConfig = store.getState().entities.general.license;
// Check a few basic fields since they may change over time
......@@ -133,27 +133,6 @@ describe('Actions.General', () => {
});
describe('getRedirectLocation', () => {
it('new server', async () => {
store.dispatch({type: GeneralTypes.RECEIVED_SERVER_VERSION, data: '5.3.0'});
const mock = nock(Client4.getBaseRoute()).
get('/redirect_location').
query({url: 'http://examp.le'}).
reply(200, '{"location": "https://example.com"}');
let requestStatus = store.getState().requests.general.redirectLocation.status;
assert.equal(requestStatus, RequestStatus.NOT_STARTED);
// Should have followed the link
const result = await store.dispatch(Actions.getRedirectLocation('http://examp.le'));
assert.deepEqual(result.data, {location: 'https://example.com'});
requestStatus = store.getState().requests.general.redirectLocation.status;
assert.equal(requestStatus, RequestStatus.SUCCESS);
assert.equal(mock.isDone(), true);
});
it('old server', async () => {
store.dispatch({type: GeneralTypes.RECEIVED_SERVER_VERSION, data: '5.0.0'});
......@@ -161,16 +140,10 @@ describe('Actions.General', () => {
get('/redirect_location').
reply(404);
let requestStatus = store.getState().requests.general.redirectLocation.status;
assert.equal(requestStatus, RequestStatus.NOT_STARTED);
// Should return the original link
const result = await store.dispatch(Actions.getRedirectLocation('http://examp.le'));
assert.deepEqual(result.data, {location: 'http://examp.le'});
requestStatus = store.getState().requests.general.redirectLocation.status;
assert.equal(requestStatus, RequestStatus.SUCCESS);
// Should not call the API on an old server
assert.equal(mock.isDone(), false);
});
......
......@@ -7,7 +7,7 @@ import {GeneralTypes} from 'action_types';
import {getServerVersion} from 'selectors/entities/general';
import {isMinimumServerVersion} from 'utils/helpers';
import {GeneralState} from 'types/general';
import {GenericClientResponse, logLevel} from 'types/client4';
import {logLevel} from 'types/client4';
import {GetStateFunc, DispatchFunc, ActionFunc, batchActions} from 'types/actions';
import {logError} from './errors';
......@@ -16,9 +16,7 @@ import {loadMe} from './users';
import {bindClientFunc, forceLogoutIfNecessary, FormattedError} from './helpers';
export function getPing(): ActionFunc {
return async (dispatch: DispatchFunc, getState: GetStateFunc) => {
dispatch({type: GeneralTypes.PING_REQUEST, data: {}}, getState);
return async () => {
let data;
let pingError = new FormattedError(
'mobile.server_ping_failed',
......@@ -28,7 +26,6 @@ export function getPing(): ActionFunc {
data = await Client4.ping();
if (data.status !== 'OK') {
// successful ping but not the right return {data}
dispatch({type: GeneralTypes.PING_FAILURE, data: {}, error: pingError}, getState);
return {error: pingError};
}
} catch (error) { // Client4Error
......@@ -36,11 +33,9 @@ export function getPing(): ActionFunc {
// When the server requires a client certificate to connect.
pingError = error;
}
dispatch({type: GeneralTypes.PING_FAILURE, data: {}, error: pingError}, getState);
return {error: pingError};
}
dispatch({type: GeneralTypes.PING_SUCCESS, data}, getState);
return {data};
};
}
......@@ -55,20 +50,11 @@ export function resetPing(): ActionFunc {
export function getClientConfig(): ActionFunc {
return async (dispatch: DispatchFunc, getState: GetStateFunc) => {
dispatch({type: GeneralTypes.CLIENT_CONFIG_REQUEST, data: {}}, getState);
let data;
try {
data = await Client4.getClientConfigOld();
} catch (error) {
forceLogoutIfNecessary(error, dispatch, getState);
dispatch(batchActions([
{
type: GeneralTypes.CLIENT_CONFIG_FAILURE,
error,
},
logError(error),
]), getState);
return {error};
}
......@@ -77,7 +63,6 @@ export function getClientConfig(): ActionFunc {
dispatch(batchActions([
{type: GeneralTypes.CLIENT_CONFIG_RECEIVED, data},
{type: GeneralTypes.CLIENT_CONFIG_SUCCESS},
]));
return {data};
......@@ -86,8 +71,6 @@ export function getClientConfig(): ActionFunc {
export function getDataRetentionPolicy(): ActionFunc {
return async (dispatch: DispatchFunc, getState: GetStateFunc) => {
dispatch({type: GeneralTypes.DATA_RETENTION_POLICY_REQUEST, data: {}}, getState);
let data;
try {
data = await Client4.getDataRetentionPolicy();
......@@ -95,7 +78,7 @@ export function getDataRetentionPolicy(): ActionFunc {
forceLogoutIfNecessary(error, dispatch, getState);
dispatch(batchActions([
{
type: GeneralTypes.DATA_RETENTION_POLICY_FAILURE,
type: GeneralTypes.RECEIVED_DATA_RETENTION_POLICY,
error,
},
logError(error),
......@@ -105,7 +88,6 @@ export function getDataRetentionPolicy(): ActionFunc {
dispatch(batchActions([
{type: GeneralTypes.RECEIVED_DATA_RETENTION_POLICY, data},
{type: GeneralTypes.DATA_RETENTION_POLICY_SUCCESS},
]));
return {data};
......@@ -115,9 +97,7 @@ export function getDataRetentionPolicy(): ActionFunc {
export function getLicenseConfig(): ActionFunc {
return bindClientFunc({
clientFunc: Client4.getClientLicenseOld,
onRequest: GeneralTypes.CLIENT_LICENSE_REQUEST,
onSuccess: [GeneralTypes.CLIENT_LICENSE_RECEIVED, GeneralTypes.CLIENT_LICENSE_SUCCESS],
onFailure: GeneralTypes.CLIENT_LICENSE_FAILURE,
onSuccess: [GeneralTypes.CLIENT_LICENSE_RECEIVED],
});
}
......@@ -184,8 +164,6 @@ export function setUrl(url: string) {
export function getRedirectLocation(url: string): ActionFunc {
return async (dispatch: DispatchFunc, getState: GetStateFunc) => {
dispatch({type: GeneralTypes.REDIRECT_LOCATION_REQUEST, data: {}}, getState);
let pendingData: Promise<any>;
if (isMinimumServerVersion(getServerVersion(getState()), 5, 3)) {
pendingData = Client4.getRedirectLocation(url);
......
......@@ -8,50 +8,6 @@ import {GeneralRequestsStatuses, RequestStatusType} from 'types/requests';
import {handleRequest, initialRequestState} from './helpers';
function server(state: RequestStatusType = initialRequestState(), action: GenericAction): RequestStatusType {
if (action.type === GeneralTypes.PING_RESET) {
return initialRequestState();
}
return handleRequest(
GeneralTypes.PING_REQUEST,
GeneralTypes.PING_SUCCESS,
GeneralTypes.PING_FAILURE,
state,
action
);
}
function config(state: RequestStatusType = initialRequestState(), action: GenericAction): RequestStatusType {
return handleRequest(
GeneralTypes.CLIENT_CONFIG_REQUEST,
GeneralTypes.CLIENT_CONFIG_SUCCESS,
GeneralTypes.CLIENT_CONFIG_FAILURE,
state,
action
);
}
function dataRetentionPolicy(state: RequestStatusType = initialRequestState(), action: GenericAction): RequestStatusType {
return handleRequest(
GeneralTypes.DATA_RETENTION_POLICY_REQUEST,
GeneralTypes.DATA_RETENTION_POLICY_SUCCESS,
GeneralTypes.DATA_RETENTION_POLICY_FAILURE,
state,
action
);
}
function license(state: RequestStatusType = initialRequestState(), action: GenericAction): RequestStatusType {
return handleRequest(
GeneralTypes.CLIENT_LICENSE_REQUEST,
GeneralTypes.CLIENT_LICENSE_SUCCESS,
GeneralTypes.CLIENT_LICENSE_FAILURE,
state,
action
);
}
function websocket(state: RequestStatusType = initialRequestState(), action: GenericAction): RequestStatusType {
if (action.type === GeneralTypes.WEBSOCKET_CLOSED) {
return initialRequestState();
......@@ -66,21 +22,6 @@ function websocket(state: RequestStatusType = initialRequestState(), action: Gen
);
}
function redirectLocation(state: RequestStatusType = initialRequestState(), action: GenericAction): RequestStatusType {
return handleRequest(
GeneralTypes.REDIRECT_LOCATION_REQUEST,
GeneralTypes.REDIRECT_LOCATION_SUCCESS,
GeneralTypes.REDIRECT_LOCATION_FAILURE,
state,
action
);
}
export default (combineReducers({
server,
config,
dataRetentionPolicy,
license,
websocket,
redirectLocation,
}) as (b: GeneralRequestsStatuses, a: GenericAction) => GeneralRequestsStatuses);
......@@ -159,30 +159,10 @@ const state: GlobalState = {
},
},
general: {
server: {
status: 'not_started',
error: null,
},
config: {
status: 'not_started',
error: null,
},
dataRetentionPolicy: {
status: 'not_started',
error: null,
},
license: {
status: 'not_started',
error: null,
},
websocket: {
status: 'not_started',
error: null,
},
redirectLocation: {
status: 'not_started',
error: null,
},
},
posts: {
createPost: {
......
......@@ -16,12 +16,7 @@ export type ChannelsRequestsStatuses = {
};
export type GeneralRequestsStatuses = {
server: RequestStatusType;
config: RequestStatusType;
dataRetentionPolicy: RequestStatusType;
license: RequestStatusType;
websocket: RequestStatusType;
redirectLocation: RequestStatusType;
};
export type PostsRequestsStatuses = {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment