Commit e988d075 authored by Joaquim Rocha's avatar Joaquim Rocha
Browse files

frontend: Allow to use an error callback with useApiList

We need to be aware when an error occurs when getting a list of cluster
objects, so this patch adds an error callback optional arg to the
mentioned method.
parent 5ff99459
Showing with 10 additions and 4 deletions
+10 -4
import { createRouteURL } from '../router'; import { createRouteURL } from '../router';
import { timeAgo } from '../util'; import { timeAgo } from '../util';
import { useConnectApi } from '.'; import { useConnectApi } from '.';
import { apiFactory, apiFactoryWithNamespace, post } from './apiProxy'; import { ApiError, apiFactory, apiFactoryWithNamespace, post } from './apiProxy';
import CronJob from './cronJob'; import CronJob from './cronJob';
import DaemonSet from './daemonSet'; import DaemonSet from './daemonSet';
import Deployment from './deployment'; import Deployment from './deployment';
...@@ -95,7 +95,8 @@ makeKubeObject<T extends (KubeObjectInterface | KubeEvent)>(objectName: string) ...@@ -95,7 +95,8 @@ makeKubeObject<T extends (KubeObjectInterface | KubeEvent)>(objectName: string)
return this.jsonData!.kind; return this.jsonData!.kind;
} }
static apiList<U extends KubeObject>(onList: (arg: U[]) => void) { static apiList<U extends KubeObject>(onList: (arg: U[]) => void,
onError?: (err: ApiError) => void) {
const createInstance = (item: T) => this.create(item) as U; const createInstance = (item: T) => this.create(item) as U;
const args: any[] = [(list: T[]) => onList(list.map((item: T) => createInstance(item) as U))]; const args: any[] = [(list: T[]) => onList(list.map((item: T) => createInstance(item) as U))];
...@@ -104,12 +105,17 @@ makeKubeObject<T extends (KubeObjectInterface | KubeEvent)>(objectName: string) ...@@ -104,12 +105,17 @@ makeKubeObject<T extends (KubeObjectInterface | KubeEvent)>(objectName: string)
args.unshift(null); args.unshift(null);
} }
if (onError) {
args.push(onError);
}
return this.apiEndpoint.list.bind(null, ...args); return this.apiEndpoint.list.bind(null, ...args);
} }
static useApiList<U extends KubeObject>(onList: (...arg: any[]) => any) { static useApiList<U extends KubeObject>(onList: (...arg: any[]) => any,
onError?: (err: ApiError) => void) {
const listCallback = onList as (arg: U[]) => void; const listCallback = onList as (arg: U[]) => void;
useConnectApi(this.apiList(listCallback)); useConnectApi(this.apiList(listCallback, onError));
} }
static create<U extends KubeObject>(this: new (arg: T) => U, item: T): U { static create<U extends KubeObject>(this: new (arg: T) => U, item: T): U {
......
Supports Markdown
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