Commit 608ed8a7 authored by Pepijn's avatar Pepijn Committed by Jesús Espino

Add types to actions/general.js (#572)

* Add types to actions/general.js

* Add types to actions/general.js

* Move all client4 related types to separate file.

Also added some docs to bindClientFunc
parent 8e949c71
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
// @flow
import {Client4} from 'client';
import {bindClientFunc, forceLogoutIfNecessary, FormattedError} from './helpers.js';
......@@ -9,11 +10,15 @@ import {loadRolesIfNeeded} from './roles';
import {logError} from './errors';
import {batchActions} from 'redux-batched-actions';
export function getPing() {
return async (dispatch, getState) => {
dispatch({type: GeneralTypes.PING_REQUEST}, getState);
import type {GeneralState} from '../types/general';
import type {GenericClientResponse, logLevel} from '../types/client4';
import type {GetStateFunc, DispatchFunc, ActionFunc} from '../types/actions';
let data;
export function getPing(): ActionFunc {
return async (dispatch: DispatchFunc, getState: GetStateFunc) => {
dispatch({type: GeneralTypes.PING_REQUEST, data: {}}, getState);
let data: GenericClientResponse;
let pingError = new FormattedError(
'mobile.server_ping_failed',
'Cannot connect to the server. Please check your server URL and internet connection.'
......@@ -22,15 +27,15 @@ export function getPing() {
data = await Client4.ping();
if (data.status !== 'OK') {
// successful ping but not the right return {data}
dispatch({type: GeneralTypes.PING_FAILURE, error: pingError}, getState);
dispatch({type: GeneralTypes.PING_FAILURE, data: {}, error: pingError}, getState);
return {error: pingError};
}
} catch (error) {
} catch (error) { // Client4Error
if (error.status_code === 401) {
// When the server requires a client certificate to connect.
pingError = error;
}
dispatch({type: GeneralTypes.PING_FAILURE, error: pingError}, getState);
dispatch({type: GeneralTypes.PING_FAILURE, data: {}, error: pingError}, getState);
return {error: pingError};
}
......@@ -39,17 +44,17 @@ export function getPing() {
};
}
export function resetPing() {
return async (dispatch, getState) => {
dispatch({type: GeneralTypes.PING_RESET}, getState);
export function resetPing(): ActionFunc {
return async (dispatch: DispatchFunc, getState: GetStateFunc) => {
dispatch({type: GeneralTypes.PING_RESET, data: {}}, getState);
return {data: true};
};
}
export function getClientConfig() {
return async (dispatch, getState) => {
dispatch({type: GeneralTypes.CLIENT_CONFIG_REQUEST}, getState);
export function getClientConfig(): ActionFunc {
return async (dispatch: DispatchFunc, getState: GetStateFunc) => {
dispatch({type: GeneralTypes.CLIENT_CONFIG_REQUEST, data: {}}, getState);
let data;
try {
......@@ -78,9 +83,9 @@ export function getClientConfig() {
};
}
export function getDataRetentionPolicy() {
return async (dispatch, getState) => {
dispatch({type: GeneralTypes.DATA_RETENTION_POLICY_REQUEST}, getState);
export function getDataRetentionPolicy(): ActionFunc {
return async (dispatch: DispatchFunc, getState: GetStateFunc) => {
dispatch({type: GeneralTypes.DATA_RETENTION_POLICY_REQUEST, data: {}}, getState);
let data;
try {
......@@ -106,7 +111,7 @@ export function getDataRetentionPolicy() {
};
}
export function getLicenseConfig() {
export function getLicenseConfig(): ActionFunc {
return bindClientFunc(
Client4.getClientLicenseOld,
GeneralTypes.CLIENT_LICENSE_REQUEST,
......@@ -115,7 +120,7 @@ export function getLicenseConfig() {
);
}
export function logClientError(message, level = 'ERROR') {
export function logClientError(message: string, level: logLevel = 'ERROR') {
return bindClientFunc(
Client4.logClientError,
GeneralTypes.LOG_CLIENT_ERROR_REQUEST,
......@@ -126,24 +131,24 @@ export function logClientError(message, level = 'ERROR') {
);
}
export function setAppState(state) {
return async (dispatch, getState) => {
export function setAppState(state: $PropertyType<GeneralState, 'appState'>): ActionFunc {
return async (dispatch: DispatchFunc, getState: GetStateFunc) => {
dispatch({type: GeneralTypes.RECEIVED_APP_STATE, data: state}, getState);
return {data: true};
};
}
export function setDeviceToken(token) {
return async (dispatch, getState) => {
export function setDeviceToken(token: $PropertyType<GeneralState, 'deviceToken'>): ActionFunc {
return async (dispatch: DispatchFunc, getState: GetStateFunc) => {
dispatch({type: GeneralTypes.RECEIVED_APP_DEVICE_TOKEN, data: token}, getState);
return {data: true};
};
}
export function setServerVersion(serverVersion) {
return async (dispatch, getState) => {
export function setServerVersion(serverVersion: string): ActionFunc {
return async (dispatch, getState: GetStateFunc) => {
dispatch({type: GeneralTypes.RECEIVED_SERVER_VERSION, data: serverVersion}, getState);
dispatch(loadRolesIfNeeded([]));
......@@ -151,8 +156,8 @@ export function setServerVersion(serverVersion) {
};
}
export function setStoreFromLocalData(data) {
return async (dispatch, getState) => {
export function setStoreFromLocalData(data: {token: string, url: string}): ActionFunc {
return async (dispatch: DispatchFunc, getState) => {
Client4.setToken(data.token);
Client4.setUrl(data.url);
......@@ -169,7 +174,7 @@ export function getSupportedTimezones() {
);
}
export function setUrl(url) {
export function setUrl(url: string) {
Client4.setUrl(url);
return true;
}
......
......@@ -7,8 +7,8 @@ import {Client4} from 'client';
import {UserTypes} from 'action_types';
import {logError} from './errors';
import type {Client4Error} from '../types/errors';
import type {GenericAction, ActionResult, DispatchFunc, GetStateFunc} from '../types/actions';
import type {Client4Error} from '../types/client4';
import type {ActionFunc, GenericAction, DispatchFunc, GetStateFunc} from '../types/actions';
type ActionType = string;
const HTTP_UNAUTHORIZED = 401;
......@@ -50,9 +50,21 @@ export function requestFailure(type: ActionType, error: Client4Error) {
};
}
/**
* Returns an ActionFunc which calls a specfied (client) function and
* dispatches the specifed actions on request, success or failure.
*
* @export
* @param {() => Promise<mixed>} clientFunc clientFunc to execute
* @param {ActionType} request ActionType to dispatch on request
* @param {(ActionType | Array<ActionType>)} success ActionType to dispatch on success
* @param {ActionType} failure ActionType to dispatch on failure
* @param {...Array<any>} args
* @returns {ActionFunc} ActionFunc
*/
export function bindClientFunc(clientFunc: () => Promise<mixed>, request: ActionType,
success: ActionType | Array<ActionType>, failure: ActionType, ...args: Array<any>) {
return async (dispatch: DispatchFunc, getState: GetStateFunc): Promise<ActionResult> => {
success: ActionType | Array<ActionType>, failure: ActionType, ...args: Array<any>): ActionFunc {
return async (dispatch, getState) => {
dispatch(requestData(request), getState);
let data = null;
......
......@@ -8,7 +8,7 @@ import {getRoles} from 'selectors/entities/roles';
import {hasNewPermissions} from 'selectors/entities/general';
import {bindClientFunc} from './helpers';
import type {DispatchFunc, GetStateFunc} from '../types/actions';
import type {DispatchFunc, GetStateFunc, ActionFunc} from '../types/actions';
import type {Role} from '../types/roles';
export function getRolesByNames(rolesNames: Array<string>) {
......@@ -59,7 +59,7 @@ export function setPendingRoles(roles: Array<string>) {
};
}
export function loadRolesIfNeeded(roles: Array<string>) {
export function loadRolesIfNeeded(roles: Array<string>): ActionFunc {
return async (dispatch: DispatchFunc, getState: GetStateFunc) => {
const state = getState();
let pendingRoles = new Set();
......
......@@ -4,13 +4,23 @@
import type {GlobalState} from './store';
export type GetStateFunc = () => GlobalState;
export type GenericAction = {|
type: string,
data: any,
error?: any
|};
type Thunk = (DispatchFunc, GetStateFunc) => Promise<ActionResult>; // eslint-disable-line no-use-before-define
type BatchAction = {
type: 'BATCHING_REDUCER.BATCH';
payload: Array<GenericAction>;
}
type Action = GenericAction | Thunk | BatchAction
export type ActionResult = {|data: any|} | {|error: any|};
export type GetStateFunc = () => GlobalState;
export type DispatchFunc = (GenericAction, ?GetStateFunc) => void;
export type DispatchFunc = (Action, ?GetStateFunc) => void;
export type ActionFunc = (DispatchFunc, GetStateFunc) => Promise<ActionResult>;
......@@ -2,6 +2,15 @@
// See LICENSE.txt for license information.
// @flow
// I assume these are the loglevels
export type logLevel = 'ERROR' | 'WARNING' | 'INFO'
export type GenericClientResponse = {
response: Object,
headers: Map<string, string>,
data: Object,
}
type ErrorOffline = {|
message: string,
url: string
......@@ -21,4 +30,4 @@ type ErrorApi = {|
url: string
|}
export type Client4Error = ErrorOffline | ErrorInvalidResponse | ErrorApi;
export type Client4Error = ErrorOffline | ErrorInvalidResponse | ErrorApi;
\ No newline at end of file
......@@ -28,12 +28,14 @@
* @param {object} obj
* @return {object}
*/
export default function keyMirror(obj: {[string]: null}): {[string]: string} {
export default function keyMirror(obj: {[string]: null}) {
if (!(obj instanceof Object && !Array.isArray(obj))) {
throw new Error('keyMirror(...): Argument must be an object.');
}
const ret = {};
type Out = {[string]: $Keys<typeof obj>;}
const ret:Out = {};
for (const key in obj) {
if (!obj.hasOwnProperty(key)) {
continue;
......@@ -41,6 +43,5 @@ export default function keyMirror(obj: {[string]: null}): {[string]: string} {
ret[key] = key;
}
return ret;
}
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