From b38ca1a720e64b438ee4d674b003649d8ade3050 Mon Sep 17 00:00:00 2001 From: Shekar Siri Date: Mon, 2 Jan 2023 18:04:12 +0100 Subject: [PATCH] feat(ui) - cards - metric store save method updates --- frontend/app/mstore/metricStore.ts | 46 +++++++++++++----------------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/frontend/app/mstore/metricStore.ts b/frontend/app/mstore/metricStore.ts index f37b79732..abb6a30f9 100644 --- a/frontend/app/mstore/metricStore.ts +++ b/frontend/app/mstore/metricStore.ts @@ -111,33 +111,27 @@ export default class MetricStore { } // API Communication - save(metric: Widget): Promise { - const wasCreating = !metric.exists(); + async save(metric: Widget): Promise { this.isSaving = true; - return new Promise((resolve, reject) => { - metricService - .saveMetric(metric) - .then((metric: any) => { - const _metric = new Widget().fromJson(metric); - if (wasCreating) { - toast.success('Metric created successfully'); - this.addToList(_metric); - this.instance = _metric; - } else { - toast.success('Metric updated successfully'); - this.updateInList(_metric); - } - resolve(_metric); - }) - .catch(() => { - toast.error('Error saving metric'); - reject(); - }) - .finally(() => { - this.instance.updateKey('hasChanged', false); - this.isSaving = false; - }); - }); + try { + const metricData = await metricService.saveMetric(metric); + const _metric = new Widget().fromJson(metricData); + if (!metric.exists()) { + toast.success('Metric created successfully'); + this.addToList(_metric); + } else { + toast.success('Metric updated successfully'); + this.updateInList(_metric); + } + this.instance = _metric; + this.instance.updateKey('hasChanged', false); + return _metric; + } catch (error) { + toast.error('Error saving metric'); + throw error; + } finally { + this.isSaving = false; + } } fetchList() {