Skip to content

Commit

Permalink
Show close confirmation dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexVelezLl committed Jan 13, 2025
1 parent 27c2f9f commit 24150e1
Showing 1 changed file with 68 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
:setTitle="setTitle"
:setGoBack="setGoBack"
:topic="topic"
:disabled="isSaving"
:channelsFetch="channelsFetch"
:bookmarksFetch="bookmarksFetch"
:treeFetch="treeFetch"
Expand All @@ -51,12 +52,25 @@
</KRouterLink>
<KButton
primary
:disabled="isSaving"
:text="saveAndFinishAction$()"
@click="saveAndClose"
@click="save"
/>
</KButtonGroup>
</div>
</template>

<KModal
v-if="isCloseConfirmationOpen"
appendToOverlay
:submitText="continueAction$()"
:cancelText="cancelAction$()"
:title="$tr('closeConfirmationTitle')"
@cancel="isCloseConfirmationOpen = false"
@submit="closeSidePanel(false)"
>
{{ $tr('closeConfirmationMessage') }}
</KModal>
</SidePanelModal>

</template>
Expand Down Expand Up @@ -101,8 +115,12 @@
function notifyResourcesAdded(count) {
createSnackbar(resourcesAddedWithCount$({ count }));
}
const { saveLessonError$ } = coachStrings;
function notifySaveLessonError() {
createSnackbar(saveLessonError$());
}
const { saveAndFinishAction$ } = coreStrings;
const { saveAndFinishAction$, continueAction$, cancelAction$ } = coreStrings;
return {
loading,
Expand All @@ -116,13 +134,18 @@
deselectResources,
setSelectedResources,
notifyResourcesAdded,
notifySaveLessonError,
cancelAction$,
continueAction$,
saveAndFinishAction$,
};
},
data() {
return {
title: '',
goBack: null,
isSaving: false,
isCloseConfirmationOpen: false,
PageNames,
};
},
Expand Down Expand Up @@ -151,14 +174,8 @@
...mapMutations('lessonSummary', {
setWorkingResources: 'SET_WORKING_RESOURCES',
}),
async saveAndClose() {
if (this.selectedResources.length > 0) {
await this.save();
}
this.closeSidePanel();
},
async save() {
const newResources = uniqBy(
getNewResources() {
return uniqBy(
[
...this.workingResources,
...this.selectedResources.map(resource => ({
Expand All @@ -169,15 +186,29 @@
],
'contentnode_id',
);
},
async save() {
if (!this.selectedResources.length) {
this.closeSidePanel(false);
return;
}
this.isSaving = true;
const newResources = this.getNewResources();
// As we are just adding resources, we can rely on the difference in length
// to determine if there are new resources to save.
const countNewResources = newResources.length - this.workingResources.length;
if (countNewResources > 0) {
await this.saveLessonResources({
lessonId: this.currentLesson.id,
resources: newResources,
});
try {
await this.saveLessonResources({
lessonId: this.currentLesson.id,
resources: newResources,
});
} catch (error) {
this.notifySaveLessonError();
this.isSaving = false;
throw error;
}
for (const resource of this.selectedResources) {
this.addToResourceCache({ node: resource });
}
Expand All @@ -187,11 +218,18 @@
this.$emit('workingResourcesUpdated');
this.notifyResourcesAdded(countNewResources);
}
this.closeSidePanel(false);
},
closeSidePanel() {
this.$router.push({
name: PageNames.LESSON_SUMMARY_BETTER,
});
closeSidePanel(verifyHasNewResources = true) {
const newResources = this.getNewResources();
const hasNewResources = newResources.length > this.workingResources.length;
if (hasNewResources && verifyHasNewResources) {
this.isCloseConfirmationOpen = true;
} else {
this.$router.push({
name: PageNames.LESSON_SUMMARY_BETTER,
});
}
},
setTitle(title) {
this.title = title;
Expand All @@ -200,6 +238,18 @@
this.goBack = goBack;
},
},
$trs: {
closeConfirmationTitle: {
message: 'Are you sure you want to leave this page?',
context:
'The title of a confirmation modal informing the user that they will lose their work if they leave the page',
},
closeConfirmationMessage: {
message: 'You will lose any unsaved edits to your work',
context:
'Warning message for the user that they will lose their work if they leave the page without saving.',
},
},
};
</script>
Expand Down

0 comments on commit 24150e1

Please sign in to comment.