From ff15ceb7d016c5f541788b32ddbd12673ee1a73e Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Tue, 23 Nov 2021 17:32:48 +0100 Subject: [PATCH] [ported] Validate jobs before adding new and minimize inactive jobs Ported from flaschengeist-frontend @7d1993e3faecdca3af47bc19f444857c49c3f3c4 --- package.json | 2 +- src/components/management/EditEvent.vue | 55 ++++++--- src/components/management/EditJobSlot.vue | 138 +++++++++++++--------- src/routes/index.ts | 4 +- src/store/index.ts | 46 ++++---- 5 files changed, 151 insertions(+), 94 deletions(-) diff --git a/package.json b/package.json index 23ea575..df5ed42 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "quasar": "^2.3.3", "axios": "^0.24.0", "prettier": "^2.4.1", - "typescript": "^4.4.4", + "typescript": "^4.5.2", "pinia": "^2.0.4", "@typescript-eslint/eslint-plugin": "^5.4.0", "@typescript-eslint/parser": "^5.4.0", diff --git a/src/components/management/EditEvent.vue b/src/components/management/EditEvent.vue index 02504a2..88b80a9 100644 --- a/src/components/management/EditEvent.vue +++ b/src/components/management/EditEvent.vue @@ -82,7 +82,14 @@ @@ -143,6 +150,8 @@ export default defineComponent({ }); }); + const active = ref(0); + const activeJob = ref<{ validate: () => Promise }>(); const templates = computed(() => store.templates); const template = ref(); const event = ref(props.modelValue || emptyEvent(startDate.value)); @@ -157,34 +166,40 @@ export default defineComponent({ }); function addJob() { - event.value.jobs.push(emptyJob()); + if (!activeJob.value) event.value.jobs.push(emptyJob()); + else + void activeJob.value.validate().then((success) => { + if (success) { + event.value.jobs.push(emptyJob()); + active.value = event.value.jobs.length - 1; + } + }); } function removeJob(index: number) { event.value.jobs.splice(index, 1); + if (active.value >= index) active.value--; } function fromTemplate(tpl: FG.Event) { - const today = new Date() + const today = new Date(); template.value = tpl; - event.value = Object.assign({}, tpl, {id: undefined}); + event.value = Object.assign({}, tpl, { id: undefined }); // Adjust the start to match today - event.value.start = date.adjustDate(event.value.start,{ + event.value.start = date.adjustDate(event.value.start, { date: today.getDate(), month: today.getMonth() + 1, // js inconsitency between getDate (1-31) and getMonth (0-11) - year: today.getFullYear() - }) + year: today.getFullYear(), + }); // Use timestamp difference for faster adjustment - const diff = event.value.start.getTime() - tpl.start.getTime() + const diff = event.value.start.getTime() - tpl.start.getTime(); // Adjust end of event and all jobs - if (event.value.end) - event.value.end.setTime(event.value.end.getTime() + diff) - event.value.jobs.forEach(job => { - job.start.setTime(job.start.getTime() + diff) - if (job.end) - job.end.setTime(job.end.getTime() + diff) - }) + if (event.value.end) event.value.end.setTime(event.value.end.getTime() + diff); + event.value.jobs.forEach((job) => { + job.start.setTime(job.start.getTime() + diff); + if (job.end) job.end.setTime(job.end.getTime() + diff); + }); } async function save(template = false) { @@ -242,14 +257,24 @@ export default defineComponent({ function reset() { event.value = Object.assign({}, props.modelValue || emptyEvent()); + active.value = 0; template.value = undefined; } const afterStart = (d: Date) => !d || event.value.start <= d || 'Das Veranstaltungsende muss vor dem Beginn liegen'; + function activate(idx: number) { + void activeJob.value?.validate().then((s) => { + if (s) active.value = idx; + }); + } + return { + activate, + active, addJob, + activeJob, afterStart, event, eventtypes, diff --git a/src/components/management/EditJobSlot.vue b/src/components/management/EditJobSlot.vue index d87ae7e..c4291ac 100644 --- a/src/components/management/EditJobSlot.vue +++ b/src/components/management/EditJobSlot.vue @@ -1,66 +1,82 @@ diff --git a/src/routes/index.ts b/src/routes/index.ts index dd740db..7a307f8 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -31,7 +31,7 @@ export const innerRoutes: FG_Plugin.MenuRoute[] = [ path: 'schedule-management', name: 'schedule-management', component: () => import('../pages/EventManagement.vue'), - props: (route) => ({date: route.query.date}), + props: (route) => ({ date: route.query.date }), }, }, { @@ -52,7 +52,7 @@ export const privateRoutes: FG_Plugin.NamedRouteRecordRaw[] = [ { name: 'new-event', path: 'new-event', - redirect: {name: 'schedule-management'} + redirect: { name: 'schedule-management' }, }, { name: 'events-edit', diff --git a/src/store/index.ts b/src/store/index.ts index f041db1..9cd873b 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -46,16 +46,14 @@ export const useEventStore = defineStore({ removeJobType(id: number) { return api .delete(`/events/job-types/${id}`) - .then(() => this.jobTypes = this.jobTypes.filter(v => v.id !== id)); + .then(() => (this.jobTypes = this.jobTypes.filter((v) => v.id !== id))); }, renameJobType(id: number, newName: string) { - return api - .put(`/events/job-types/${id}`, { name: newName }) - .then(() => { - const idx = this.jobTypes.findIndex(v=>v.id===id); - if (idx >= 0) this.jobTypes[idx].name = newName; - }) + return api.put(`/events/job-types/${id}`, { name: newName }).then(() => { + const idx = this.jobTypes.findIndex((v) => v.id === id); + if (idx >= 0) this.jobTypes[idx].name = newName; + }); }, async getEventTypes(force = false) { @@ -76,7 +74,7 @@ export const useEventStore = defineStore({ addEventType(name: string) { return api .post('/events/event-types', { name: name }) - .then(({data}) => this.eventTypes.push(data)) + .then(({ data }) => this.eventTypes.push(data)); }, async removeEvent(id: number) { @@ -85,7 +83,7 @@ export const useEventStore = defineStore({ const idx = this.templates.findIndex((v) => v.id === id); if (idx !== -1) this.templates.splice(idx, 1); } catch (e) { - if (isAxiosError(e, 404)) return false + if (isAxiosError(e, 404)) return false; throw e; } return true; @@ -94,16 +92,14 @@ export const useEventStore = defineStore({ removeEventType(id: number) { return api .delete(`/events/event-types/${id}`) - .then(() => this.eventTypes = this.eventTypes.filter(v => v.id !== id)); + .then(() => (this.eventTypes = this.eventTypes.filter((v) => v.id !== id))); }, renameEventType(id: number, newName: string) { - return api - .put(`/events/event-types/${id}`, { name: newName }) - .then(() => { - const idx = this.eventTypes.findIndex(v=>v.id===id); - if (idx >= 0) this.eventTypes[idx].name = newName; - }) + return api.put(`/events/event-types/${id}`, { name: newName }).then(() => { + const idx = this.eventTypes.findIndex((v) => v.id === id); + if (idx >= 0) this.eventTypes[idx].name = newName; + }); }, async getTemplates(force = false) { @@ -115,7 +111,11 @@ export const useEventStore = defineStore({ return this.templates; }, - async getEvents(filter: { from?: Date; to?: Date, limit?: number, offset?: number, descending?: boolean } | undefined = undefined) { + async getEvents( + filter: + | { from?: Date; to?: Date; limit?: number; offset?: number; descending?: boolean } + | undefined = undefined + ) { try { const { data } = await api.get('/events', { params: filter }); data.forEach((element) => fixEvent(element)); @@ -136,10 +136,11 @@ export const useEventStore = defineStore({ }, async assignToJob(jobId: number, service: FG.Service) { - return api.post(`/events/jobs/${jobId}/assign`, service); + return api.post(`/events/jobs/${jobId}/assign`, service); }, async addEvent(event: EditableEvent) { + console.log('addEvent', event); if (event?.id === undefined) { const { data } = await api.post('/events', event); if (data.is_template) this.templates.push(data); @@ -147,7 +148,10 @@ export const useEventStore = defineStore({ } else { if (typeof event.type === 'object') event.type = event.type.id; - const { data } = await api.put(`/events/${event.id}`, Object.assign(event, {jobs: undefined})); + const { data } = await api.put( + `/events/${event.id}`, + Object.assign(event, { jobs: undefined }) + ); if (data.is_template) this.templates.push(data); return data; } @@ -157,8 +161,8 @@ export const useEventStore = defineStore({ return api.post('/events/transfer', { job: job.id, receiver: invitees, - is_invite: isInvite + is_invite: isInvite, }); - } + }, }, });