Skip to content

Commit 1c0cacf

Browse files
committed
feat: add warnings for unsaved vote changes
1 parent fa2e9fa commit 1c0cacf

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

frontend/src/components/Vote/VoteEdit.vue

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@
4848
<cdx-button weight="quiet" action="progressive" v-if="isVoting('ranking')" @click="saveRanking">
4949
<content-save-outline style="font-size: 6px" /> {{ $t('montage-round-save') }}
5050
</cdx-button>
51+
52+
<cdx-button weight="quiet" action="default" @click="gobackToRound">
53+
<ArrowLeftThick />
54+
<span>Back to Round</span>
55+
</cdx-button>
5156
</div>
5257
<div class="image-grid" :class="'grid-size-' + gridSize" v-if="!isVoting('ranking')">
5358
<div v-for="image in votes" :key="image.id" class="gallery-image link" :class="getImageSizeClass()">
@@ -125,7 +130,7 @@
125130
</template>
126131

127132
<script setup>
128-
import { onMounted, onUnmounted, ref, watch } from 'vue'
133+
import { onMounted, onUnmounted, ref, watch} from 'vue'
129134
import _ from 'lodash'
130135
import { useRouter, useRoute } from 'vue-router'
131136
import { useI18n } from 'vue-i18n'
@@ -145,6 +150,10 @@ import { CdxButton, CdxSelect } from '@wikimedia/codex'
145150
import { VueDraggableNext as draggable } from 'vue-draggable-next'
146151
import ImageReviewDialog from './ImageReviewDialog.vue'
147152
153+
//Services
154+
// import { EventBus } from 'vue-toastification' // Currently depricated
155+
import { onBeforeRouteLeave } from 'vue-router'
156+
148157
// Icons
149158
import ContentSaveOutline from 'vue-material-design-icons/ContentSaveOutline.vue'
150159
import ImageSizeSelectActual from 'vue-material-design-icons/ImageSizeSelectActual.vue'
@@ -155,6 +164,8 @@ import ThumbDown from 'vue-material-design-icons/ThumbDown.vue'
155164
import Star from 'vue-material-design-icons/Star.vue'
156165
import ArrowExpandAll from 'vue-material-design-icons/ArrowExpandAll.vue'
157166
import Heart from 'vue-material-design-icons/Heart.vue'
167+
import ArrowRight from 'vue-material-design-icons/ArrowRight.vue'
168+
import ArrowLeftThick from 'vue-material-design-icons/ArrowLeftThick.vue'
158169
159170
// Hooks
160171
const { t: $t } = useI18n()
@@ -358,6 +369,13 @@ watch(locale, (newLocale) => {
358369
dayjs.locale(newLocale)
359370
})
360371
372+
const beforeUnloadListner = (event) => {
373+
if (edits.value.length){
374+
event.preventDefault()
375+
event.returnValue="";
376+
}
377+
};
378+
361379
onMounted(() => {
362380
getRoundDetails(voteId)
363381
getPastVotes(voteId)
@@ -368,13 +386,30 @@ onMounted(() => {
368386
369387
handleResize()
370388
window.addEventListener('resize', handleResize)
389+
window.addEventListener('beforeunload',beforeUnloadListner)
390+
})
391+
392+
const gobackToRound = () => {
393+
router.push(`/vote/${round.value.link}`)
394+
}
395+
396+
onBeforeRouteLeave((to,from,next)=> {
397+
if (edits.value.length) {
398+
if (confirm("There are unsaved changes. Are you sure you want to leave?")) {
399+
next()
400+
} else {
401+
next(false)
402+
}
403+
} else {
404+
next()
405+
}
371406
})
372407
373408
onUnmounted(() => {
374409
if (editVoteContainer.value) {
375410
editVoteContainer.value.removeEventListener('scroll', handleScroll)
376411
}
377-
412+
window.removeEventListener('beforeunload',beforeUnloadListner)
378413
window.removeEventListener('resize', handleResize)
379414
})
380415
</script>

0 commit comments

Comments
 (0)