From f4f695ce80232910eb9a14c36b945ccb20223d1d Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Sat, 14 Mar 2020 22:47:30 -0500 Subject: [PATCH] web: WIP edit measure configuration. --- web/src/router.ts | 6 ++++ web/src/services/pm-api-client.ts | 5 +++ web/src/store-modules/measure.ts | 10 ++++-- web/src/views/EditMeasure.vue | 52 +++++++++++++++++++++++++++++++ web/src/views/Measure.vue | 26 ++++++++++++---- web/src/views/edit-measure.ts | 43 +++++++++++++++++++++++++ web/src/views/measure.ts | 2 ++ web/src/views/new-measure.ts | 12 ++----- 8 files changed, 139 insertions(+), 17 deletions(-) create mode 100644 web/src/views/EditMeasure.vue create mode 100644 web/src/views/edit-measure.ts diff --git a/web/src/router.ts b/web/src/router.ts index 2a06cba..7b1ef8d 100644 --- a/web/src/router.ts +++ b/web/src/router.ts @@ -8,6 +8,7 @@ import Measures from '@/views/Measures.vue'; import NewMeasure from '@/views/NewMeasure.vue'; import NewMeasurement from '@/views/NewMeasurement.vue'; import DeleteMeasure from '@/views/DeleteMeasure.vue'; +import EditMeasure from '@/views/EditMeasure.vue'; import NotFound from '@/views/NotFound.vue'; import QuickPanels from '@/views/QuickPanels.vue'; import UserAccount from '@/views/UserAccount.vue'; @@ -74,6 +75,11 @@ const router = new Router({ name: 'delete-measure', component: DeleteMeasure }, + { + path: '/edit/measure/:slug', + name: 'edit-measure', + component: EditMeasure + }, { path: '*', name: 'not-found', diff --git a/web/src/services/pm-api-client.ts b/web/src/services/pm-api-client.ts index 816fd87..97fff4d 100644 --- a/web/src/services/pm-api-client.ts +++ b/web/src/services/pm-api-client.ts @@ -123,6 +123,11 @@ export class PmApiClient { return resp.data; } + public async updateMeasure(measure: Measure): Promise> { + const resp = await this.http.post(`/measures/${measure.slug}`, measure); + return resp.data; + } + public async deleteMeasure(slug: string): Promise { const resp = await this.http.delete(`/measures/${slug}`); return true; diff --git a/web/src/store-modules/measure.ts b/web/src/store-modules/measure.ts index 236b024..cecba2d 100644 --- a/web/src/store-modules/measure.ts +++ b/web/src/store-modules/measure.ts @@ -30,18 +30,24 @@ export class MeasureStoreModule extends VuexModule { } @Action({ rawError: true }) - public async createMeasure(m: Measure) { + public async createMeasure(m: Measure) { const newMeasure = await api.createMeasure(m); this.context.commit('SET_MEASURE', newMeasure); return newMeasure; } @Action({ rawError: true }) - public async deleteMeasure(m: Measure) { + public async deleteMeasure(m: Measure) { const delResponse = await api.deleteMeasure(m.slug); this.context.commit('DELETE_MEASURE', m); } + @Action({ rawError: true }) + public async updateMeasure(m: Measure) { + const updatedMeasure = await api.updateMeasure(m); + return updatedMeasure; + } + @Mutation private SET_MEASURE(measure: Measure) { this.measures = assign({}, this.measures, {[measure.slug]: measure}); } diff --git a/web/src/views/EditMeasure.vue b/web/src/views/EditMeasure.vue new file mode 100644 index 0000000..7dbb05f --- /dev/null +++ b/web/src/views/EditMeasure.vue @@ -0,0 +1,52 @@ + + diff --git a/web/src/views/Measure.vue b/web/src/views/Measure.vue index 75742ac..1d17a37 100644 --- a/web/src/views/Measure.vue +++ b/web/src/views/Measure.vue @@ -5,12 +5,26 @@

{{measure.name}}

{{measure.description}}

- - - - - Add Measurement - +
+ + + + + + + + Add Measurement + +
diff --git a/web/src/views/edit-measure.ts b/web/src/views/edit-measure.ts new file mode 100644 index 0000000..2e73001 --- /dev/null +++ b/web/src/views/edit-measure.ts @@ -0,0 +1,43 @@ +import { Component, Prop, Vue, Watch } from 'vue-property-decorator'; +import { logService } from '@/services/logging'; +import { measureStore, userStore } from '@/store'; +import { Measure, MeasureConfig, MeasureType } from '@/models'; +import MeasureConfigForm from '@/components/measure-config/MeasureConfigForm.vue'; +import { slugify } from '@/util'; + +const logger = logService.getLogger('/views/edit-measure'); + +@Component({ + components: { MeasureConfigForm } +}) +export class EditMeasure extends Vue { + private waiting = false; + + private get measure(): Measure | null { + return measureStore.measures[this.$route.params.slug] || null; + } + + private get slugFromName() { + return slugify(this.measure.name); + } + + private async updateMeasure() { + if (this.measure) { + if (!this.measure.slug) { + this.measure.slug = slugify(this.measure.name); + } + + this.waiting = true; + try { + await measureStore.updateMeasure(this.measure); + this.$router.push({name: 'measure', params: { slug: this.measure.slug }}); + } catch (e) { + logger.error('Unable to update measure. \n\t' + JSON.stringify(this.measure), e.stack); + } finally { + this.waiting = false; + } + } + } +} + +export default EditMeasure; diff --git a/web/src/views/measure.ts b/web/src/views/measure.ts index e4733d7..83bf218 100644 --- a/web/src/views/measure.ts +++ b/web/src/views/measure.ts @@ -1,10 +1,12 @@ import { Component, Prop, Vue } from 'vue-property-decorator'; import { library } from '@fortawesome/fontawesome-svg-core'; +import { faPencilAlt } from '@fortawesome/free-solid-svg-icons'; import { faTrash } from '@fortawesome/free-solid-svg-icons'; import { Measure as MeasureModel, MeasureConfig } from '@/models'; import { measureStore, measurementStore } from '@/store'; import MeasureDetails from '@/components/measure-details/MeasureDetails.vue'; +library.add(faPencilAlt); library.add(faTrash); @Component({ diff --git a/web/src/views/new-measure.ts b/web/src/views/new-measure.ts index 4c6cc42..dab57fc 100644 --- a/web/src/views/new-measure.ts +++ b/web/src/views/new-measure.ts @@ -5,6 +5,7 @@ import { logService } from '@/services/logging'; import { measureStore, userStore } from '@/store'; import { Measure, MeasureConfig, MeasureType } from '@/models'; import MeasureConfigForm from '@/components/measure-config/MeasureConfigForm.vue'; +import { slugify } from '@/util'; library.add(faSync); @@ -29,19 +30,12 @@ export class NewMeasure extends Vue { }; private get slugFromName() { - return this.slugify(this.measure.name); - } - - private slugify(s: string): string { - return s - .toLowerCase() - .replace(/[^\w\s\-]/g, '') - .replace(/\s+/g, '-'); + return slugify(this.measure.name); } private async createMeasure() { if (!this.measure.slug) { - this.measure.slug = this.slugify(this.measure.name); + this.measure.slug = slugify(this.measure.name); } this.waiting = true;