refs #14161 Merge branch 'origin/14161-inputs-focus-enter-action'
[arvados.git] / src / services / common-service / common-resource-service.ts
index 0ad6fbce1f3df525f4fd139af33e4a9c91c75fd6..f6810c0453b183a1db0847fe127a8dd607004d41 100644 (file)
@@ -6,7 +6,7 @@ import * as _ from "lodash";
 import { AxiosInstance, AxiosPromise } from "axios";
 import { Resource } from "src/models/resource";
 import * as uuid from "uuid/v4";
-import { ProgressFn } from "~/services/api/api-progress";
+import { ApiActions } from "~/services/api/api-actions";
 
 export interface ListArguments {
     limit?: number;
@@ -41,7 +41,7 @@ export enum CommonResourceServiceError {
 
 export class CommonResourceService<T extends Resource> {
 
-    static mapResponseKeys = (response: { data: any }): Promise<any> =>
+    static mapResponseKeys = (response: { data: any }) =>
         CommonResourceService.mapKeys(_.camelCase)(response.data)
 
     static mapKeys = (mapFn: (key: string) => string) =>
@@ -62,36 +62,38 @@ export class CommonResourceService<T extends Resource> {
             }
         }
 
-    static defaultResponse<R>(promise: AxiosPromise<R>, progressFn: ProgressFn): Promise<R> {
+    static defaultResponse<R>(promise: AxiosPromise<R>, actions: ApiActions): Promise<R> {
         const reqId = uuid();
-        progressFn(reqId, true);
+        actions.progressFn(reqId, true);
         return promise
             .then(data => {
-                progressFn(reqId, false);
+                actions.progressFn(reqId, false);
                 return data;
             })
             .then(CommonResourceService.mapResponseKeys)
             .catch(({ response }) => {
-                progressFn(reqId, false);
-                Promise.reject<Errors>(CommonResourceService.mapResponseKeys(response));
+                actions.progressFn(reqId, false);
+                const errors = CommonResourceService.mapResponseKeys(response) as Errors;
+                actions.errorFn(reqId, errors);
+                throw errors;
             });
     }
 
     protected serverApi: AxiosInstance;
     protected resourceType: string;
-    protected progressFn: ProgressFn;
+    protected actions: ApiActions;
 
-    constructor(serverApi: AxiosInstance, resourceType: string, onProgress: ProgressFn) {
+    constructor(serverApi: AxiosInstance, resourceType: string, actions: ApiActions) {
         this.serverApi = serverApi;
         this.resourceType = '/' + resourceType + '/';
-        this.progressFn = onProgress;
+        this.actions = actions;
     }
 
     create(data?: Partial<T> | any) {
         return CommonResourceService.defaultResponse(
             this.serverApi
                 .post<T>(this.resourceType, data && CommonResourceService.mapKeys(_.snakeCase)(data)),
-            this.progressFn
+            this.actions
         );
     }
 
@@ -99,7 +101,7 @@ export class CommonResourceService<T extends Resource> {
         return CommonResourceService.defaultResponse(
             this.serverApi
                 .delete(this.resourceType + uuid),
-            this.progressFn
+            this.actions
         );
     }
 
@@ -107,7 +109,7 @@ export class CommonResourceService<T extends Resource> {
         return CommonResourceService.defaultResponse(
             this.serverApi
                 .get<T>(this.resourceType + uuid),
-            this.progressFn
+            this.actions
         );
     }
 
@@ -123,7 +125,7 @@ export class CommonResourceService<T extends Resource> {
                 .get(this.resourceType, {
                     params: CommonResourceService.mapKeys(_.snakeCase)(params)
                 }),
-            this.progressFn
+            this.actions
         );
     }
 
@@ -131,7 +133,7 @@ export class CommonResourceService<T extends Resource> {
         return CommonResourceService.defaultResponse(
             this.serverApi
                 .put<T>(this.resourceType + uuid, data && CommonResourceService.mapKeys(_.snakeCase)(data)),
-            this.progressFn
+            this.actions
         );
     }
 }