From 30173ec87d7a66f5194e87e892e5a95a2c7ec7a5 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Thu, 23 Dec 2021 03:25:18 +0100 Subject: [PATCH] feat(ui): models: Provide Job class --- src/components/management/EditEvent.vue | 6 +-- src/store/models.ts | 72 ++++++++++++++++++------- 2 files changed, 57 insertions(+), 21 deletions(-) diff --git a/src/components/management/EditEvent.vue b/src/components/management/EditEvent.vue index b148395..9e65153 100644 --- a/src/components/management/EditEvent.vue +++ b/src/components/management/EditEvent.vue @@ -111,7 +111,7 @@ import { notEmpty } from '@flaschengeist/api'; import { IsoDateInput } from '@flaschengeist/api/components'; import { useEventStore } from '../../store'; -import { emptyEvent, emptyJob, EditableEvent } from '../../store/models'; +import { emptyEvent, Job, EditableEvent } from '../../store/models'; import { date, ModifyDateOptions } from 'quasar'; import { computed, defineComponent, PropType, ref, onBeforeMount, watch } from 'vue'; @@ -164,11 +164,11 @@ export default defineComponent({ ); function addJob() { - if (!activeJob.value) event.value.jobs.push(emptyJob()); + if (!activeJob.value) event.value.jobs.push(new Job()); else void activeJob.value.validate().then((success) => { if (success) { - event.value.jobs.push(emptyJob()); + event.value.jobs.push(new Job()); active.value = event.value.jobs.length - 1; } }); diff --git a/src/store/models.ts b/src/store/models.ts index 0b10f28..fed3e19 100644 --- a/src/store/models.ts +++ b/src/store/models.ts @@ -4,32 +4,68 @@ import { date } from 'quasar'; export type EditableEvent = Omit, 'type'>, 'id'> & { type?: FG.EventType | number; id?: number; - jobs: EditableJob[]; + jobs: Job[]; }; -/** A new job does not have an id or type assigned */ -export type EditableJob = Omit, 'id'> & { - type?: FG.EventType | number; - id?: number; -}; +export class Job implements FG.Job { + id = NaN; + start: Date; + end?: Date = undefined; + type: FG.JobType | number = NaN; + comment?: string = undefined; + locked = false; + services = [] as FG.Service[]; + required_services = 0; -export function emptyJob(startDate = new Date()): EditableJob { - const start = date.adjustDate(startDate, { - hours: new Date().getHours(), - }); - return { - start: start, - end: date.addToDate(start, { hours: 1 }), - services: [], - locked: false, - required_services: 2, - }; + /** + * Build Job from API Job interface + * @param iJob Object following the API Job interface + */ + constructor(iJob?: Partial) { + if (!iJob || iJob.start === undefined) + this.start = date.buildDate({ + hours: new Date().getHours(), + minutes: 0, + seconds: 0, + milliseconds: 0, + }); + else this.start = new Date(); // <-- make TS happy "no initalizer" + if (iJob !== undefined) Object.assign(this, iJob); + } + + /** + * Create Job from start Date + * @param start when does the event start? + * @param adjustTime Set hours to current value, zero minutes and seconds + * @param duration How long should the job go? Value in hours or undefined + * @returns new Job event + */ + static fromDate(start: Date, adjustTime = true, duration?: number) { + if (adjustTime) + start = date.adjustDate(start, { + hours: new Date().getHours(), + minutes: 0, + seconds: 0, + milliseconds: 0, + }); + return new Job({ + start: start, + end: duration === undefined ? undefined : date.addToDate(start, { hours: duration }), + }); + } + + /** + * Check if this instance was loaded from API + */ + isPersistent() { + return !isNaN(this.id); + } } export function emptyEvent(startDate: Date = new Date()): EditableEvent { return { start: date.adjustDate(startDate, { hours: 0, minutes: 0, seconds: 0, milliseconds: 0 }), - jobs: [emptyJob(startDate)], + jobs: [Job.fromDate(startDate, true, 4)], is_template: false, }; }