Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Prompt to save when there are unsaved changes #4945

Open
wants to merge 2 commits into
base: mealie-next
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 34 additions & 7 deletions frontend/components/Domain/Recipe/RecipePage/RecipePage.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
<template>
<div>
<v-snackbar
v-model="snackbar"
:timeout="0"
top
>
{{ snackbarText }}
<template #action="{ attrs }">
<v-btn text v-bind="attrs" @click="discardChanges">
{{ discardText }}
</v-btn>
<v-btn color="primary" text v-bind="attrs" @click="saveRecipe">
{{ saveText }}
</v-btn>
</template>
</v-snackbar>
<v-container v-show="!isCookMode" key="recipe-page" :class="{ 'pa-0': $vuetify.breakpoint.smAndDown }">
<v-card :flat="$vuetify.breakpoint.smAndDown" class="d-print-none">
<RecipePageHeader
Expand Down Expand Up @@ -204,7 +219,7 @@ export default defineComponent({
},
},
setup(props) {
const { $auth } = useContext();
const { $auth, $vuetify, i18n } = useContext();
const route = useRoute();
const groupSlug = computed(() => route.value.params.groupSlug || $auth.user?.groupSlug || "");
const { isOwnGroup } = useLoggedInState();
Expand All @@ -225,7 +240,7 @@ export default defineComponent({
* this is used to determine if the recipe has been changed since the last save
* and prompts the user to save if they have unsaved changes.
*/
const originalRecipe = ref<Recipe | null>(null);
const originalRecipe = ref<Recipe>({});

invoke(async () => {
await until(props.recipe).not.toBeNull();
Expand All @@ -234,7 +249,7 @@ export default defineComponent({

onUnmounted(async () => {
const isSame = JSON.stringify(props.recipe) === JSON.stringify(originalRecipe.value);
if (isEditMode.value && !isSame && props.recipe?.slug !== undefined) {
if (!isSame && props.recipe?.slug !== undefined) {
const save = window.confirm(
i18n.tc("general.unsaved-changes"),
);
Expand Down Expand Up @@ -267,14 +282,15 @@ export default defineComponent({
});

/** =============================================================
* Recipe Save Delete
* Recipe Save Delete Cancel
*/

async function saveRecipe() {
const { data } = await api.recipes.updateOne(props.recipe.slug, props.recipe);
setMode(PageMode.VIEW);
if (data?.slug) {
router.push(`/g/${groupSlug.value}/r/` + data.slug);
originalRecipe.value = deepCopy(props.recipe);
router.push(`/g/${groupSlug.value}/r/${data.slug}`);
}
}

Expand All @@ -285,10 +301,17 @@ export default defineComponent({
}
}

function discardChanges() {
Object.assign(props.recipe, originalRecipe.value);
}

const snackbar = computed(() => {
return JSON.stringify(props.recipe) !== JSON.stringify(originalRecipe.value) && pageMode.value !== PageMode.EDIT;
})

/** =============================================================
* View Preferences
*/
const { $vuetify, i18n } = useContext();

const landscape = computed(() => {
const preferLandscape = props.recipe.settings.landscapeView;
Expand Down Expand Up @@ -347,7 +370,10 @@ export default defineComponent({
scale: ref(1),
EDITOR_OPTIONS,
landscape,

snackbar,
snackbarText: i18n.tc("general.unsaved-changes"),
saveText: i18n.tc("general.save"),
discardText: i18n.tc("general.cancel"),
pageMode,
editMode,
PageMode,
Expand All @@ -359,6 +385,7 @@ export default defineComponent({
toggleCookMode,
saveRecipe,
deleteRecipe,
discardChanges,
addStep,
hasLinkedIngredients,
notLinkedIngredients,
Expand Down