1
0
Fork 0
mirror of https://github.com/mealie-recipes/mealie.git synced 2025-07-23 15:19:41 +02:00

fix: Random Recipes not choosing from all recipes (#4435)

Co-authored-by: Kuchenpirat <24235032+Kuchenpirat@users.noreply.github.com>
This commit is contained in:
Michael Genson 2024-10-29 09:47:54 -05:00 committed by GitHub
parent 8d1ce5c190
commit 6e045bf0c3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 50 additions and 29 deletions

View file

@ -205,23 +205,14 @@ export default defineComponent({
const route = useRoute(); const route = useRoute();
const groupSlug = computed(() => route.value.params.groupSlug || $auth.user?.groupSlug || ""); const groupSlug = computed(() => route.value.params.groupSlug || $auth.user?.groupSlug || "");
const router = useRouter();
function navigateRandom() {
if (props.recipes.length > 0) {
const recipe = props.recipes[Math.floor(Math.random() * props.recipes.length)];
if (recipe.slug !== undefined) {
router.push(`/g/${groupSlug.value}/r/${recipe.slug}`);
}
}
}
const page = ref(1); const page = ref(1);
const perPage = 32; const perPage = 32;
const hasMore = ref(true); const hasMore = ref(true);
const ready = ref(false); const ready = ref(false);
const loading = ref(false); const loading = ref(false);
const { fetchMore } = useLazyRecipes(isOwnGroup.value ? null : groupSlug.value); const { fetchMore, getRandom } = useLazyRecipes(isOwnGroup.value ? null : groupSlug.value);
const router = useRouter();
const queryFilter = computed(() => { const queryFilter = computed(() => {
const orderBy = props.query?.orderBy || preferences.value.orderBy; const orderBy = props.query?.orderBy || preferences.value.orderBy;
@ -383,6 +374,15 @@ export default defineComponent({
}, useAsyncKey()); }, useAsyncKey());
} }
async function navigateRandom() {
const recipe = await getRandom(props.query, queryFilter.value);
if (!recipe?.slug) {
return;
}
router.push(`/g/${groupSlug.value}/r/${recipe.slug}`);
}
function toggleMobileCards() { function toggleMobileCards() {
preferences.value.useMobileCards = !preferences.value.useMobileCards; preferences.value.useMobileCards = !preferences.value.useMobileCards;
} }

View file

@ -8,6 +8,32 @@ import { RecipeSearchQuery } from "~/lib/api/user/recipes/recipe";
export const allRecipes = ref<Recipe[]>([]); export const allRecipes = ref<Recipe[]>([]);
export const recentRecipes = ref<Recipe[]>([]); export const recentRecipes = ref<Recipe[]>([]);
function getParams(
orderBy: string | null = null,
orderDirection = "desc",
query: RecipeSearchQuery | null = null,
queryFilter: string | null = null
) {
return {
orderBy,
orderDirection,
paginationSeed: query?._searchSeed, // propagate searchSeed to stabilize random order pagination
searchSeed: query?._searchSeed, // unused, but pass it along for completeness of data
search: query?.search,
cookbook: query?.cookbook,
households: query?.households,
categories: query?.categories,
requireAllCategories: query?.requireAllCategories,
tags: query?.tags,
requireAllTags: query?.requireAllTags,
tools: query?.tools,
requireAllTools: query?.requireAllTools,
foods: query?.foods,
requireAllFoods: query?.requireAllFoods,
queryFilter,
};
};
export const useLazyRecipes = function (publicGroupSlug: string | null = null) { export const useLazyRecipes = function (publicGroupSlug: string | null = null) {
const router = useRouter(); const router = useRouter();
@ -25,24 +51,11 @@ export const useLazyRecipes = function (publicGroupSlug: string | null = null) {
queryFilter: string | null = null, queryFilter: string | null = null,
) { ) {
const { data, error } = await api.recipes.getAll(page, perPage, { const { data, error } = await api.recipes.getAll(
orderBy, page,
orderDirection, perPage,
paginationSeed: query?._searchSeed, // propagate searchSeed to stabilize random order pagination getParams(orderBy, orderDirection, query, queryFilter),
searchSeed: query?._searchSeed, // unused, but pass it along for completeness of data );
search: query?.search,
cookbook: query?.cookbook,
households: query?.households,
categories: query?.categories,
requireAllCategories: query?.requireAllCategories,
tags: query?.tags,
requireAllTags: query?.requireAllTags,
tools: query?.tools,
requireAllTools: query?.requireAllTools,
foods: query?.foods,
requireAllFoods: query?.requireAllFoods,
queryFilter,
});
if (error?.response?.status === 404) { if (error?.response?.status === 404) {
router.push("/login"); router.push("/login");
@ -74,6 +87,13 @@ export const useLazyRecipes = function (publicGroupSlug: string | null = null) {
recipes.value = val; recipes.value = val;
} }
async function getRandom(query: RecipeSearchQuery | null = null, queryFilter: string | null = null) {
const { data } = await api.recipes.getAll(1, 1, getParams("random", "desc", query, queryFilter));
if (data?.items.length) {
return data.items[0];
}
}
return { return {
recipes, recipes,
fetchMore, fetchMore,
@ -81,6 +101,7 @@ export const useLazyRecipes = function (publicGroupSlug: string | null = null) {
assignSorted, assignSorted,
removeRecipe, removeRecipe,
replaceRecipes, replaceRecipes,
getRandom,
}; };
}; };