Skip to content

Commit

Permalink
State refactoring #1
Browse files Browse the repository at this point in the history
  • Loading branch information
bsekachev committed Oct 18, 2019
1 parent c0a9e99 commit 233e25d
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 120 deletions.
42 changes: 20 additions & 22 deletions cvat-ui/src/actions/tasks-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,39 +28,39 @@ function getTasks(): AnyAction {
}

function getTasksSuccess(array: any[], previews: string[],
count: number, query: TasksQuery): AnyAction {
count: number, gettingQuery: TasksQuery): AnyAction {
const action = {
type: TasksActionTypes.GET_TASKS_SUCCESS,
payload: {
previews,
array,
count,
query,
gettingQuery,
},
};

return action;
}

function getTasksFailed(error: any, query: TasksQuery): AnyAction {
function getTasksFailed(gettingTasksError: any, gettingQuery: TasksQuery): AnyAction {
const action = {
type: TasksActionTypes.GET_TASKS_FAILED,
payload: {
error,
query,
gettingTasksError,
gettingQuery,
},
};

return action;
}

export function getTasksAsync(query: TasksQuery):
export function getTasksAsync(gettingQuery: TasksQuery):
ThunkAction<Promise<void>, {}, {}, AnyAction> {
return async (dispatch: ActionCreator<Dispatch>): Promise<void> => {
dispatch(getTasks());

// We need remove all keys with null values from query
const filteredQuery = { ...query };
const filteredQuery = { ...gettingQuery };
for (const key in filteredQuery) {
if (filteredQuery[key] === null) {
delete filteredQuery[key];
Expand All @@ -70,8 +70,8 @@ ThunkAction<Promise<void>, {}, {}, AnyAction> {
let result = null;
try {
result = await cvat.tasks.get(filteredQuery);
} catch (error) {
dispatch(getTasksFailed(error, query));
} catch (gettingTasksError) {
dispatch(getTasksFailed(gettingTasksError, gettingQuery));
return;
}

Expand All @@ -93,7 +93,7 @@ ThunkAction<Promise<void>, {}, {}, AnyAction> {
}
}

dispatch(getTasksSuccess(array, previews, result.count, query));
dispatch(getTasksSuccess(array, previews, result.count, gettingQuery));
};
}

Expand Down Expand Up @@ -121,13 +121,13 @@ function dumpAnnotationSuccess(task: any, dumper: any): AnyAction {
return action;
}

function dumpAnnotationFailed(task: any, dumper: any, error: any): AnyAction {
function dumpAnnotationFailed(task: any, dumper: any, dumpingError: any): AnyAction {
const action = {
type: TasksActionTypes.DUMP_ANNOTATIONS_FAILED,
payload: {
task,
dumper,
error,
dumpingError,
},
};

Expand All @@ -137,13 +137,12 @@ function dumpAnnotationFailed(task: any, dumper: any, error: any): AnyAction {
export function dumpAnnotationsAsync(task: any, dumper: any):
ThunkAction<Promise<void>, {}, {}, AnyAction> {
return async (dispatch: ActionCreator<Dispatch>): Promise<void> => {
dispatch(dumpAnnotation(task, dumper));

try {
dispatch(dumpAnnotation(task, dumper));
const url = await task.annotations.dump(task.name, dumper);
window.location.assign(url);
} catch (error) {
dispatch(dumpAnnotationFailed(task, dumper, error));
} catch (dumpingError) {
dispatch(dumpAnnotationFailed(task, dumper, dumpingError));
return;
}

Expand Down Expand Up @@ -174,12 +173,12 @@ function loadAnnotationsSuccess(task: any): AnyAction {
return action;
}

function loadAnnotationsFailed(task: any, error: any): AnyAction {
function loadAnnotationsFailed(task: any, loadingError: any): AnyAction {
const action = {
type: TasksActionTypes.LOAD_ANNOTATIONS_FAILED,
payload: {
task,
error,
loadingError,
},
};

Expand All @@ -189,12 +188,11 @@ function loadAnnotationsFailed(task: any, error: any): AnyAction {
export function loadAnnotationsAsync(task: any, loader: any, file: File):
ThunkAction<Promise<void>, {}, {}, AnyAction> {
return async (dispatch: ActionCreator<Dispatch>): Promise<void> => {
dispatch(loadAnnotations(task, loader));

try {
dispatch(loadAnnotations(task, loader));
await task.annotations.upload(file, loader);
} catch (error) {
dispatch(loadAnnotationsFailed(task, error));
} catch (loadingError) {
dispatch(loadAnnotationsFailed(task, loadingError));
return;
}

Expand Down
40 changes: 19 additions & 21 deletions cvat-ui/src/reducers/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,34 @@ export interface TasksQuery {
[key: string]: string | number | null;
}

export interface DumpState {
dumperName: string;
}

export interface LoadState {
loaderName: string;
}

export interface Task {
instance: any;
instance: any; // cvat-core instance
preview: string;
}

export interface ActiveTask {
dump: DumpState[];
load: LoadState | null;
}

export interface TasksState {
initialized: boolean;
gettingTasksError: any;
gettingQuery: TasksQuery;
count: number;
current: Task[];
active: {
[index: number]: ActiveTask;
activities: {
dumps: {
dumpingError: any;
byTask: {
// dumps in different formats at the same time
[tid: number]: string[]; // dumper names
};
};
loads: {
loadingError: any;
loadingDoneMessage: string;
byTask: {
// only one loading simultaneously
[tid: number]: string; // loader name
};
};
};
error: any;
dumpError: any;
loadError: any;
loadDone: string;
query: TasksQuery;
}

export interface FormatsState {
Expand Down
Loading

0 comments on commit 233e25d

Please sign in to comment.