From bdbb3a15ae7dec0e67f51599fa6240857767083d Mon Sep 17 00:00:00 2001 From: Pierre Gonzalez Date: Thu, 9 Apr 2020 17:32:15 +0200 Subject: [PATCH] add isUniqueValuesLoading to dataset header column --- src/lib/dataset/index.ts | 1 + src/store/backend-plugin.ts | 44 +++++++++++++++++++++++++++---------- src/store/getters.ts | 6 +++++ src/store/mutations.ts | 12 ++++++++++ 4 files changed, 52 insertions(+), 11 deletions(-) diff --git a/src/lib/dataset/index.ts b/src/lib/dataset/index.ts index 93cb39d589..f50aa53eb2 100644 --- a/src/lib/dataset/index.ts +++ b/src/lib/dataset/index.ts @@ -12,6 +12,7 @@ export type ColumnTypeMapping = { export type DataSetColumn = { name: string; type?: DataSetColumnType; + isUniqueValueLoading?: boolean; //It will be updated by backendify function }; /** diff --git a/src/store/backend-plugin.ts b/src/store/backend-plugin.ts index 78c4de0773..d122a1ffb0 100644 --- a/src/store/backend-plugin.ts +++ b/src/store/backend-plugin.ts @@ -9,7 +9,7 @@ */ import { Store } from 'vuex'; -import { BackendResponse } from '@/lib/backend-response'; +import { BackendError, BackendResponse } from '@/lib/backend-response'; import { DataSet } from '@/lib/dataset'; import { pageOffset } from '@/lib/dataset/pagination'; import { Pipeline } from '@/lib/steps'; @@ -114,32 +114,54 @@ function _preparePipeline(pipeline: Pipeline, store: Store) { * - logs the error in the store if any, * - sets the `loading: false` property on the store at the end. */ -export function backendify(target: Function) { +export function backendify( + target: Function, + callFront?: Function, + callBack?: Function, + reject?: Function, +) { return async function(this: BackendService | void, store: Store, ...args: any[]) { try { - store.commit(VQBnamespace('setLoading'), { isLoading: true }); + if (callFront) { + callFront(); + } const response = await target.bind(this)(store, ...args); if (response.error) { - store.commit(VQBnamespace('logBackendError'), { - backendError: response.error, - }); + if (reject) { + reject(response.error); + } } return response; } catch (error) { const response = { error: { type: 'error', message: error.toString() } }; - store.commit(VQBnamespace('logBackendError'), { - backendError: response.error, - }); + if (reject) { + reject(response.error); + } return response; } finally { - store.commit(VQBnamespace('setLoading'), { isLoading: false }); + if (callBack) { + callBack(); + } } }; } async function _updateDataset(store: Store, service: BackendService, pipeline: Pipeline) { pipeline = _preparePipeline(pipeline, store); - const response = await backendify(service.executePipeline).bind(service)( + const response = await backendify( + service.executePipeline, + () => { + store.commit(VQBnamespace('setLoading'), { isLoading: true }); + }, + () => { + store.commit(VQBnamespace('setLoading'), { isLoading: false }); + }, + (error: BackendError) => { + store.commit(VQBnamespace('logBackendError'), { + backendError: error, + }); + }, + ).bind(service)( store, pipeline, store.state[VQB_MODULE_NAME].pagesize, diff --git a/src/store/getters.ts b/src/store/getters.ts index 9c4fc2b5bc..3085a496ba 100644 --- a/src/store/getters.ts +++ b/src/store/getters.ts @@ -82,6 +82,12 @@ const getters: GetterTree = { */ pageno: (state: VQBState) => state.dataset.paginationContext ? state.dataset.paginationContext.pageno : 1, + /** + * helper that is True if unique values are loading + */ + isUniqueValuesLoading: (state: VQBState) => (column: string) => + state.dataset.headers[state.dataset.headers.map(col => col.name).indexOf(column)] + .isUniqueValueLoading, /** * Return current edited pipeline */ diff --git a/src/store/mutations.ts b/src/store/mutations.ts index f1b320cbc2..a64e2d1bde 100644 --- a/src/store/mutations.ts +++ b/src/store/mutations.ts @@ -284,6 +284,18 @@ class Mutations { state.isLoading = isLoading; } + /** + * Set unique values loading + */ + setUniqueValuesLoading( + state: VQBState, + { isLoading, column }: { isLoading: boolean; column: string }, + ) { + state.dataset.headers[ + state.dataset.headers.map(col => col.name).indexOf(column) + ].isUniqueValueLoading = isLoading; + } + /** * Update translator. */