fix add jobs
This commit is contained in:
parent
b7741cfa37
commit
994f65c38b
|
@ -112,7 +112,7 @@ import { notEmpty } from '@flaschengeist/api';
|
||||||
import { IsoDateInput } from '@flaschengeist/api/components';
|
import { IsoDateInput } from '@flaschengeist/api/components';
|
||||||
|
|
||||||
import { useEventStore } from '../../store';
|
import { useEventStore } from '../../store';
|
||||||
import { emptyEvent, emptyJob, EditableEvent } from '../../store/models';
|
import { emptyEvent, Job, EditableEvent } from '../../store/models';
|
||||||
|
|
||||||
import { date, DateOptions } from 'quasar';
|
import { date, DateOptions } from 'quasar';
|
||||||
import { computed, defineComponent, PropType, ref, onBeforeMount, watch } from 'vue';
|
import { computed, defineComponent, PropType, ref, onBeforeMount, watch } from 'vue';
|
||||||
|
@ -143,7 +143,7 @@ export default defineComponent({
|
||||||
const store = useEventStore();
|
const store = useEventStore();
|
||||||
|
|
||||||
const active = ref(0);
|
const active = ref(0);
|
||||||
const activeJob = ref<{ validate: () => Promise<boolean> }>();
|
const activeJob = ref<{ validate: () => Promise<boolean> }[]>([]);
|
||||||
const templates = computed(() => store.templates);
|
const templates = computed(() => store.templates);
|
||||||
const template = ref<FG.Event>();
|
const template = ref<FG.Event>();
|
||||||
const event = ref<EditableEvent>(props.modelValue || emptyEvent());
|
const event = ref<EditableEvent>(props.modelValue || emptyEvent());
|
||||||
|
@ -166,12 +166,11 @@ export default defineComponent({
|
||||||
|
|
||||||
function addJob() {
|
function addJob() {
|
||||||
if (!activeJob.value[active.value]) {
|
if (!activeJob.value[active.value]) {
|
||||||
event.value.jobs.push(emptyJob());
|
event.value.jobs.push(new Job());
|
||||||
active.value = event.value.jobs.length - 1;
|
|
||||||
} else
|
} else
|
||||||
void activeJob.value[active.value].validate().then((success) => {
|
void activeJob.value[active.value].validate().then((success) => {
|
||||||
if (success) {
|
if (success) {
|
||||||
event.value.jobs.push(emptyJob());
|
event.value.jobs.push(new Job());
|
||||||
active.value = event.value.jobs.length - 1;
|
active.value = event.value.jobs.length - 1;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -4,32 +4,68 @@ import { date } from 'quasar';
|
||||||
export type EditableEvent = Omit<Omit<Omit<FG.Event, 'jobs'>, 'type'>, 'id'> & {
|
export type EditableEvent = Omit<Omit<Omit<FG.Event, 'jobs'>, 'type'>, 'id'> & {
|
||||||
type?: FG.EventType | number;
|
type?: FG.EventType | number;
|
||||||
id?: number;
|
id?: number;
|
||||||
jobs: EditableJob[];
|
jobs: Job[];
|
||||||
};
|
};
|
||||||
|
|
||||||
/** A new job does not have an id or type assigned */
|
export class Job implements FG.Job {
|
||||||
export type EditableJob = Omit<Omit<FG.Job, 'type'>, 'id'> & {
|
id = NaN;
|
||||||
type?: FG.EventType | number;
|
start: Date;
|
||||||
id?: number;
|
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, {
|
* Build Job from API Job interface
|
||||||
hours: new Date().getHours(),
|
* @param iJob Object following the API Job interface
|
||||||
});
|
*/
|
||||||
return {
|
constructor(iJob?: Partial<FG.Job>) {
|
||||||
start: start,
|
if (!iJob || iJob.start === undefined)
|
||||||
end: date.addToDate(start, { hours: 1 }),
|
this.start = date.buildDate({
|
||||||
services: [],
|
hours: new Date().getHours(),
|
||||||
locked: false,
|
minutes: 0,
|
||||||
required_services: 2,
|
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 {
|
export function emptyEvent(startDate: Date = new Date()): EditableEvent {
|
||||||
return {
|
return {
|
||||||
start: date.adjustDate(startDate, { hours: 0, minutes: 0, seconds: 0, milliseconds: 0 }),
|
start: date.adjustDate(startDate, { hours: 0, minutes: 0, seconds: 0, milliseconds: 0 }),
|
||||||
jobs: [emptyJob(startDate)],
|
jobs: [Job.fromDate(startDate, true, 4)],
|
||||||
is_template: false,
|
is_template: false,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue