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

feat: Prevent Shopping Lists From Rendering If Redirecting #3768

Merged
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
40 changes: 28 additions & 12 deletions frontend/pages/shopping-lists/index.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<v-container v-if="shoppingListChoices" class="narrow-container">
<v-container v-if="shoppingListChoices && ready" class="narrow-container">
<BaseDialog v-model="createDialog" :title="$tc('shopping-list.create-shopping-list')" @submit="createOne">
<v-card-text>
<v-text-field v-model="createName" autofocus :label="$t('shopping-list.new-list')"> </v-text-field>
Expand Down Expand Up @@ -48,7 +48,7 @@
</template>

<script lang="ts">
import { computed, defineComponent, useAsync, useContext, reactive, toRefs, useRoute, useRouter } from "@nuxtjs/composition-api";
import { computed, defineComponent, useAsync, useContext, reactive, ref, toRefs, useRoute, useRouter, watch } from "@nuxtjs/composition-api";
import { useUserApi } from "~/composables/api";
import { useAsyncKey } from "~/composables/use-utils";
import { useShoppingListPreferences } from "~/composables/use-users/preferences";
Expand All @@ -57,11 +57,13 @@ export default defineComponent({
middleware: "auth",
setup() {
const { $auth } = useContext();
const ready = ref(false);
const userApi = useUserApi();
const route = useRoute();
const router = useRouter();
const groupSlug = computed(() => route.value.params.groupSlug || $auth.user?.groupSlug || "");
const disableRedirect = computed(() => route.value.query.disableRedirect === "true");
const overrideDisableRedirect = ref(false);
const disableRedirect = computed(() => route.value.query.disableRedirect === "true" || overrideDisableRedirect.value);
const preferences = useShoppingListPreferences();

const state = reactive({
Expand All @@ -83,6 +85,28 @@ export default defineComponent({
return shoppingLists.value.filter((list) => preferences.value.viewAllLists || list.userId === $auth.user?.id);
});

// This has to appear before the shoppingListChoices watcher, otherwise that runs first and the redirect is not disabled
watch(
() => preferences.value.viewAllLists,
() => {
overrideDisableRedirect.value = true;
},
);

watch(
() => shoppingListChoices,
() => {
if (!disableRedirect.value && shoppingListChoices.value.length === 1) {
router.push(`/shopping-lists/${shoppingListChoices.value[0].id}`);
} else {
ready.value = true;
}
},
{
deep: true,
},
);

async function fetchShoppingLists() {
const { data } = await userApi.shopping.lists.getAll(1, -1, { orderBy: "name", orderDirection: "asc" });

Expand Down Expand Up @@ -118,17 +142,9 @@ export default defineComponent({
}
}

if (!disableRedirect.value) {
useAsync(async () => {
await refresh();
if (shoppingListChoices.value?.length === 1) {
router.push(`/shopping-lists/${shoppingListChoices.value[0].id}`);
}
}, useAsyncKey());
}

return {
...toRefs(state),
ready,
groupSlug,
preferences,
shoppingListChoices,
Expand Down
Loading