diff --git a/src/components/management/EditEvent.vue b/src/components/management/EditEvent.vue index 7ea0642..373e058 100644 --- a/src/components/management/EditEvent.vue +++ b/src/components/management/EditEvent.vue @@ -112,7 +112,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, DateOptions } from 'quasar'; import { computed, defineComponent, PropType, ref, onBeforeMount, watch } from 'vue'; @@ -143,7 +143,7 @@ export default defineComponent({ const store = useEventStore(); const active = ref(0); - const activeJob = ref<{ validate: () => Promise }>(); + const activeJob = ref<{ validate: () => Promise }[]>([]); const templates = computed(() => store.templates); const template = ref(); const event = ref(props.modelValue || emptyEvent()); @@ -166,12 +166,11 @@ export default defineComponent({ function addJob() { if (!activeJob.value[active.value]) { - event.value.jobs.push(emptyJob()); - active.value = event.value.jobs.length - 1; + event.value.jobs.push(new Job()); } else void activeJob.value[active.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, }; }