Skip to content

Commit

Permalink
More types in actions and reducers (#1166)
Browse files Browse the repository at this point in the history
  • Loading branch information
bsekachev authored Feb 20, 2020
1 parent a376ee7 commit f208cfe
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 186 deletions.
1 change: 1 addition & 0 deletions cvat-ui/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module.exports = {
'jsx-quotes': ['error', 'prefer-single'],
'arrow-parens': ['error', 'always'],
'@typescript-eslint/no-explicit-any': [0],
'@typescript-eslint/explicit-function-return-type': ['warn', { allowExpressions: true }],
'no-restricted-syntax': [0, {'selector': 'ForOfStatement'}],
'no-plusplus': [0],
'lines-between-class-members': 0,
Expand Down
4 changes: 2 additions & 2 deletions cvat-ui/src/actions/about-actions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ActionUnion, createAction, ThunkAction } from 'utils/redux';
import getCore from 'cvat-core';
import { ActionUnion, createAction, ThunkAction } from '../utils/redux';

const core = getCore();

Expand All @@ -17,7 +17,7 @@ const aboutActions = {

export type AboutActions = ActionUnion<typeof aboutActions>;

export const getAboutAsync = (): ThunkAction => async (dispatch) => {
export const getAboutAsync = (): ThunkAction => async (dispatch): Promise<void> => {
dispatch(aboutActions.getAbout());

try {
Expand Down
2 changes: 1 addition & 1 deletion cvat-ui/src/actions/auth-actions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ActionUnion, createAction, ThunkAction } from 'utils/redux';
import getCore from 'cvat-core';
import { ActionUnion, createAction, ThunkAction } from '../utils/redux';

const cvat = getCore();

Expand Down
60 changes: 23 additions & 37 deletions cvat-ui/src/actions/formats-actions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { AnyAction, Dispatch, ActionCreator } from 'redux';
import { ThunkAction } from 'redux-thunk';

import { ActionUnion, createAction, ThunkAction } from 'utils/redux';
import getCore from 'cvat-core';

const cvat = getCore();
Expand All @@ -11,48 +9,36 @@ export enum FormatsActionTypes {
GET_FORMATS_FAILED = 'GET_FORMATS_FAILED',
}

function getFormats(): AnyAction {
return {
type: FormatsActionTypes.GET_FORMATS,
payload: {},
};
}

function getFormatsSuccess(
annotationFormats: any[],
datasetFormats: any[],
): AnyAction {
return {
type: FormatsActionTypes.GET_FORMATS_SUCCESS,
payload: {
const formatsActions = {
getFormats: () => createAction(FormatsActionTypes.GET_FORMATS),
getFormatsSuccess: (annotationFormats: any[], datasetFormats: any[]) => (
createAction(FormatsActionTypes.GET_FORMATS_SUCCESS, {
annotationFormats,
datasetFormats,
},
};
}

function getFormatsFailed(error: any): AnyAction {
return {
type: FormatsActionTypes.GET_FORMATS_FAILED,
payload: {
error,
},
};
}

export function getFormatsAsync(): ThunkAction<Promise<void>, {}, {}, AnyAction> {
return async (dispatch: ActionCreator<Dispatch>): Promise<void> => {
dispatch(getFormats());
})
),
getFormatsFailed: (error: any) => (
createAction(FormatsActionTypes.GET_FORMATS_FAILED, { error })
),
};

export type FormatsActions = ActionUnion<typeof formatsActions>;

export function getFormatsAsync(): ThunkAction {
return async (dispatch): Promise<void> => {
dispatch(formatsActions.getFormats());
let annotationFormats = null;
let datasetFormats = null;

try {
annotationFormats = await cvat.server.formats();
datasetFormats = await cvat.server.datasetFormats();

dispatch(
formatsActions.getFormatsSuccess(annotationFormats, datasetFormats),
);
} catch (error) {
dispatch(getFormatsFailed(error));
return;
dispatch(formatsActions.getFormatsFailed(error));
}

dispatch(getFormatsSuccess(annotationFormats, datasetFormats));
};
}
77 changes: 36 additions & 41 deletions cvat-ui/src/actions/plugins-actions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { AnyAction, Dispatch, ActionCreator } from 'redux';
import { ThunkAction } from 'redux-thunk';
import { ActionUnion, createAction, ThunkAction } from 'utils/redux';
import { SupportedPlugins } from 'reducers/interfaces';
import PluginChecker from 'utils/plugin-checker';

Expand All @@ -8,49 +7,45 @@ export enum PluginsActionTypes {
CHECKED_ALL_PLUGINS = 'CHECKED_ALL_PLUGINS'
}

interface PluginObjects {
[plugin: string]: boolean;
}

function checkPlugins(): AnyAction {
const action = {
type: PluginsActionTypes.CHECK_PLUGINS,
payload: {},
};

return action;
}
type PluginObjects = Record<SupportedPlugins, boolean>;

function checkedAllPlugins(list: PluginObjects): AnyAction {
const action = {
type: PluginsActionTypes.CHECKED_ALL_PLUGINS,
payload: {
const pluginActions = {
checkPlugins: () => createAction(PluginsActionTypes.CHECK_PLUGINS),
checkedAllPlugins: (list: PluginObjects) => (
createAction(PluginsActionTypes.CHECKED_ALL_PLUGINS, {
list,
},
};

return action;
}

export function checkPluginsAsync():
ThunkAction<Promise<void>, {}, {}, AnyAction> {
return async (dispatch: ActionCreator<Dispatch>): Promise<void> => {
dispatch(checkPlugins());
const plugins: PluginObjects = {};

const promises: Promise<boolean>[] = [];
const keys = Object.keys(SupportedPlugins);
for (const key of keys) {
const plugin = SupportedPlugins[key as any];
promises.push(PluginChecker.check(plugin as SupportedPlugins));
}
})
),
};

export type PluginActions = ActionUnion<typeof pluginActions>;

export function checkPluginsAsync(): ThunkAction {
return async (dispatch): Promise<void> => {
dispatch(pluginActions.checkPlugins());
const plugins: PluginObjects = {
ANALYTICS: false,
AUTO_ANNOTATION: false,
GIT_INTEGRATION: false,
TF_ANNOTATION: false,
TF_SEGMENTATION: false,
};

const promises: Promise<boolean>[] = [
PluginChecker.check(SupportedPlugins.ANALYTICS),
PluginChecker.check(SupportedPlugins.AUTO_ANNOTATION),
PluginChecker.check(SupportedPlugins.GIT_INTEGRATION),
PluginChecker.check(SupportedPlugins.TF_ANNOTATION),
PluginChecker.check(SupportedPlugins.TF_SEGMENTATION),
];

const values = await Promise.all(promises);
let i = 0;
for (const key of keys) {
plugins[key] = values[i++];
}
[plugins.ANALYTICS] = values;
[, plugins.AUTO_ANNOTATION] = values;
[,, plugins.GIT_INTEGRATION] = values;
[,,, plugins.TF_ANNOTATION] = values;
[,,,, plugins.TF_SEGMENTATION] = values;

dispatch(checkedAllPlugins(plugins));
dispatch(pluginActions.checkedAllPlugins(plugins));
};
}
63 changes: 24 additions & 39 deletions cvat-ui/src/actions/share-actions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { AnyAction, Dispatch, ActionCreator } from 'redux';
import { ThunkAction } from 'redux-thunk';
import { ActionUnion, createAction, ThunkAction } from 'utils/redux';
import getCore from 'cvat-core';

import { ShareFileInfo } from 'reducers/interfaces';
import getCore from 'cvat-core';

const core = getCore();

Expand All @@ -12,49 +11,35 @@ export enum ShareActionTypes {
LOAD_SHARE_DATA_FAILED = 'LOAD_SHARE_DATA_FAILED',
}

function loadShareData(): AnyAction {
const action = {
type: ShareActionTypes.LOAD_SHARE_DATA,
payload: {},
};

return action;
}

function loadShareDataSuccess(values: ShareFileInfo[], directory: string): AnyAction {
const action = {
type: ShareActionTypes.LOAD_SHARE_DATA_SUCCESS,
payload: {
const shareActions = {
loadShareData: () => createAction(ShareActionTypes.LOAD_SHARE_DATA),
loadShareDataSuccess: (values: ShareFileInfo[], directory: string) => (
createAction(ShareActionTypes.LOAD_SHARE_DATA_SUCCESS, {
values,
directory,
},
};

return action;
}

function loadShareDataFailed(error: any): AnyAction {
const action = {
type: ShareActionTypes.LOAD_SHARE_DATA_FAILED,
payload: {
error,
},
};

return action;
}

export function loadShareDataAsync(directory: string, success: () => void, failure: () => void):
ThunkAction<Promise<void>, {}, {}, AnyAction> {
return async (dispatch: ActionCreator<Dispatch>): Promise<void> => {
})
),
loadShareDataFailed: (error: any) => (
createAction(ShareActionTypes.LOAD_SHARE_DATA_FAILED, { error })
),
};

export type ShareActions = ActionUnion<typeof shareActions>;

export function loadShareDataAsync(
directory: string,
success: () => void,
failure: () => void,
): ThunkAction {
return async (dispatch): Promise<void> => {
try {
dispatch(loadShareData());
dispatch(shareActions.loadShareData());
const values = await core.server.share(directory);
success();
dispatch(loadShareDataSuccess(values as ShareFileInfo[], directory));
dispatch(shareActions.loadShareDataSuccess(values as ShareFileInfo[], directory));
} catch (error) {
dispatch(loadShareDataFailed(error));
failure();
dispatch(shareActions.loadShareDataFailed(error));
}
};
}
52 changes: 14 additions & 38 deletions cvat-ui/src/actions/users-actions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { AnyAction, Dispatch, ActionCreator } from 'redux';
import { ThunkAction } from 'redux-thunk';

import { ActionUnion, createAction, ThunkAction } from 'utils/redux';
import getCore from 'cvat-core';

const core = getCore();
Expand All @@ -11,47 +9,25 @@ export enum UsersActionTypes {
GET_USERS_FAILED = 'GET_USERS_FAILED',
}

function getUsers(): AnyAction {
const action = {
type: UsersActionTypes.GET_USERS,
payload: {},
};

return action;
}

function getUsersSuccess(users: any[]): AnyAction {
const action = {
type: UsersActionTypes.GET_USERS_SUCCESS,
payload: { users },
};
const usersActions = {
getUsers: () => createAction(UsersActionTypes.GET_USERS),
getUsersSuccess: (users: any[]) => createAction(UsersActionTypes.GET_USERS_SUCCESS, { users }),
getUsersFailed: (error: any) => createAction(UsersActionTypes.GET_USERS_FAILED, { error }),
};

return action;
}

function getUsersFailed(error: any): AnyAction {
const action = {
type: UsersActionTypes.GET_USERS_FAILED,
payload: { error },
};

return action;
}
export type UsersActions = ActionUnion<typeof usersActions>;

export function getUsersAsync():
ThunkAction<Promise<void>, {}, {}, AnyAction> {
return async (dispatch: ActionCreator<Dispatch>): Promise<void> => {
dispatch(getUsers());
export function getUsersAsync(): ThunkAction {
return async (dispatch): Promise<void> => {
dispatch(usersActions.getUsers());

try {
const users = await core.users.get();
dispatch(
getUsersSuccess(
users.map((userData: any): any => new core.classes.User(userData)),
),
);
const wrappedUsers = users
.map((userData: any): any => new core.classes.User(userData));
dispatch(usersActions.getUsersSuccess(wrappedUsers));
} catch (error) {
dispatch(getUsersFailed(error));
dispatch(usersActions.getUsersFailed(error));
}
};
}
4 changes: 2 additions & 2 deletions cvat-ui/src/reducers/auth-reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const defaultState: AuthState = {
user: null,
};

export default (state = defaultState, action: AuthActions): AuthState => {
export default function (state = defaultState, action: AuthActions): AuthState {
switch (action.type) {
case AuthActionTypes.AUTHORIZED_SUCCESS:
return {
Expand Down Expand Up @@ -67,4 +67,4 @@ export default (state = defaultState, action: AuthActions): AuthState => {
default:
return state;
}
};
}
10 changes: 6 additions & 4 deletions cvat-ui/src/reducers/formats-reducer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { AnyAction } from 'redux';
import { FormatsActionTypes } from 'actions/formats-actions';
import { AuthActionTypes } from 'actions/auth-actions';
import { FormatsActionTypes, FormatsActions } from 'actions/formats-actions';
import { AuthActionTypes, AuthActions } from 'actions/auth-actions';

import { FormatsState } from './interfaces';

Expand All @@ -11,7 +10,10 @@ const defaultState: FormatsState = {
fetching: false,
};

export default (state = defaultState, action: AnyAction): FormatsState => {
export default (
state: FormatsState = defaultState,
action: FormatsActions | AuthActions,
): FormatsState => {
switch (action.type) {
case FormatsActionTypes.GET_FORMATS: {
return {
Expand Down
Loading

0 comments on commit f208cfe

Please sign in to comment.