Skip to content

Commit

Permalink
fix: Random Recipes not choosing from all recipes (#4435)
Browse files Browse the repository at this point in the history
Co-authored-by: Kuchenpirat <24235032+Kuchenpirat@users.noreply.github.com>
  • Loading branch information
michael-genson and Kuchenpirat authored Oct 29, 2024
1 parent 8d1ce5c commit 6e045bf
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 29 deletions.
22 changes: 11 additions & 11 deletions frontend/components/Domain/Recipe/RecipeCardSection.vue
Original file line number Diff line number Diff line change
Expand Up @@ -205,23 +205,14 @@ export default defineComponent({
const route = useRoute();
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 perPage = 32;
const hasMore = ref(true);
const ready = 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 orderBy = props.query?.orderBy || preferences.value.orderBy;
Expand Down Expand Up @@ -383,6 +374,15 @@ export default defineComponent({
}, 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() {
preferences.value.useMobileCards = !preferences.value.useMobileCards;
}
Expand Down
57 changes: 39 additions & 18 deletions frontend/composables/recipes/use-recipes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,32 @@ import { RecipeSearchQuery } from "~/lib/api/user/recipes/recipe";
export const allRecipes = 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) {
const router = useRouter();

Expand All @@ -25,24 +51,11 @@ export const useLazyRecipes = function (publicGroupSlug: string | null = null) {
queryFilter: string | null = null,
) {

const { data, error } = await api.recipes.getAll(page, perPage, {
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,
});
const { data, error } = await api.recipes.getAll(
page,
perPage,
getParams(orderBy, orderDirection, query, queryFilter),
);

if (error?.response?.status === 404) {
router.push("/login");
Expand Down Expand Up @@ -74,13 +87,21 @@ export const useLazyRecipes = function (publicGroupSlug: string | null = null) {
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 {
recipes,
fetchMore,
appendRecipes,
assignSorted,
removeRecipe,
replaceRecipes,
getRandom,
};
};

Expand Down

0 comments on commit 6e045bf

Please sign in to comment.