diff --git a/src/components/event/EventFormBase.vue b/src/components/event/EventFormBase.vue index 3933b7a0..8d6f53fa 100644 --- a/src/components/event/EventFormBase.vue +++ b/src/components/event/EventFormBase.vue @@ -75,6 +75,10 @@ import EventFormSummary, { } from '@/components/event/EventFormSummary.vue' import FormNextButton from '@/components/shared/FormNextButton.vue' import FormBackButton from '@/components/shared/FormBackButton.vue' +import { useDraftConfirmer } from '@/workers/draftConfirmer' +import { removeDraftConfirmer } from '@/workers/draftConfirmer' +import router from '@/router' +import { Route } from 'vue-router' export type EventInput = EventInputContent & ( @@ -115,6 +119,10 @@ export default class EventFormBase extends Vue { instant: boolean = null! + originalSummary: EventSummary = null! + + beforeEachControl: (() => void) | null = null + created() { this.content = { name: this.event?.name ?? '', @@ -197,8 +205,107 @@ export default class EventFormBase extends Vue { } } + isChanged(): boolean { + if (this.summary && this.originalSummary) { + return ( + JSON.stringify(this.summary) !== JSON.stringify(this.originalSummary) + ) + } + return false + } + + cleanupContent(): void { + this.summary.name = '' + this.summary.description = '' + this.summary.tags = [] + this.summary.groupName = '' + this.summary.place = '' + this.summary.timeStart = '' + this.summary.timeEnd = '' + this.summary.open = false + this.summary.sharedRoom = true + } + + isEventNewOrEdit(): boolean { + const currentRoute: Route = this.$route + return currentRoute.name === 'EventNew' + } + + beforLeaveGuardinEventEdit = (to, from, next) => { + if (from.name !== 'EventEdit') { + return next() + } + + if (!this.isChanged()) { + return next() + } + + if ( + confirm( + '入力されたデータは送信されないまま破棄されますが,よろしいですか。' + ) + ) { + removeDraftConfirmer() + this.cleanupContent() + return next() + } + + return next(false) + } + + beforLeaveGuardinEventNew = (to, from, next) => { + if (from.name !== 'EventNew' || !this.isChanged()) { + return next() + } + + if ( + confirm( + '入力されたデータは送信されないまま破棄されますが,よろしいですか。' + ) + ) { + removeDraftConfirmer() + this.cleanupContent() + return next() + } + + return next(false) + } + + mounted() { + this.$watch( + 'summary', + () => { + if (this.isChanged()) { + useDraftConfirmer() + } else { + removeDraftConfirmer() + } + }, + { deep: true } + ) + if (this.isEventNewOrEdit()) { + this.beforeEachControl = router.beforeEach(this.beforLeaveGuardinEventNew) + } else { + this.beforeEachControl = router.beforeEach( + this.beforLeaveGuardinEventEdit + ) + } + this.originalSummary = JSON.parse(JSON.stringify(this.summary)) + } + + beforeDestroy() { + if (this.beforeEachControl) { + this.beforeEachControl() + } + } + @Emit() submit(): EventOutput { + removeDraftConfirmer() + this.cleanupContent() + if (this.beforeEachControl) { + this.beforeEachControl() + } return { ...this.content, group: this.content.group!, diff --git a/src/components/main/SidebarFooter.vue b/src/components/main/SidebarFooter.vue index b6fc9e1f..f4bbe878 100644 --- a/src/components/main/SidebarFooter.vue +++ b/src/components/main/SidebarFooter.vue @@ -59,6 +59,8 @@