implement related recipes on home page

This commit is contained in:
smilerz
2021-12-22 15:23:16 -06:00
parent 2d01a2af47
commit 67e4c88be7
4 changed files with 50 additions and 35 deletions

View File

@@ -65,7 +65,6 @@ def test_related_mixed_space(request, recipe, u1_s2):
reverse(RELATED_URL, args={recipe.id})).content)) == 0 reverse(RELATED_URL, args={recipe.id})).content)) == 0
# TODO add tests for mealplan related when thats added
# TODO if/when related recipes includes multiple levels (related recipes of related recipes) add the following tests # TODO if/when related recipes includes multiple levels (related recipes of related recipes) add the following tests
# -- step recipes included in step recipes # -- step recipes included in step recipes
# -- step recipes included in food recipes # -- step recipes included in food recipes

View File

@@ -229,7 +229,6 @@ def test_shopping_recipe_mixed_authors(u1_s1, u2_s1):
assert len(json.loads(u2_s1.get(reverse(SHOPPING_LIST_URL)).content)) == 0 assert len(json.loads(u2_s1.get(reverse(SHOPPING_LIST_URL)).content)) == 0
# TODO test adding recipe with ingredients that are not food
@pytest.mark.parametrize("recipe", [{'steps__ingredients__header': 1}], indirect=['recipe']) @pytest.mark.parametrize("recipe", [{'steps__ingredients__header': 1}], indirect=['recipe'])
def test_shopping_with_header_ingredient(u1_s1, recipe): def test_shopping_with_header_ingredient(u1_s1, recipe):
# with scope(space=recipe.space): # with scope(space=recipe.space):

View File

@@ -1,5 +1,6 @@
<template> <template>
<div id="app" style="margin-bottom: 4vh"> <div id="app" style="margin-bottom: 4vh">
<RecipeSwitcher mode="mealplan" />
<div class="row"> <div class="row">
<div class="col-12 col-xl-8 col-lg-10 offset-xl-2 offset-lg-1"> <div class="col-12 col-xl-8 col-lg-10 offset-xl-2 offset-lg-1">
<div class="row"> <div class="row">
@@ -244,6 +245,7 @@ import RecipeCard from "@/components/RecipeCard"
import GenericMultiselect from "@/components/GenericMultiselect" import GenericMultiselect from "@/components/GenericMultiselect"
import Treeselect from "@riophae/vue-treeselect" import Treeselect from "@riophae/vue-treeselect"
import "@riophae/vue-treeselect/dist/vue-treeselect.css" import "@riophae/vue-treeselect/dist/vue-treeselect.css"
import RecipeSwitcher from "@/components/Buttons/RecipeSwitcher"
Vue.use(BootstrapVue) Vue.use(BootstrapVue)
@@ -252,7 +254,7 @@ let SETTINGS_COOKIE_NAME = "search_settings"
export default { export default {
name: "RecipeSearchView", name: "RecipeSearchView",
mixins: [ResolveUrlMixin, ApiMixin], mixins: [ResolveUrlMixin, ApiMixin],
components: { GenericMultiselect, RecipeCard, Treeselect }, components: { GenericMultiselect, RecipeCard, Treeselect, RecipeSwitcher },
data() { data() {
return { return {
// this.Models and this.Actions inherited from ApiMixin // this.Models and this.Actions inherited from ApiMixin

View File

@@ -34,9 +34,11 @@
<script> <script>
const { ApiApiFactory } = require("@/utils/openapi/api") const { ApiApiFactory } = require("@/utils/openapi/api")
import { ResolveUrlMixin } from "@/utils/utils"
export default { export default {
name: "RecipeSwitcher", name: "RecipeSwitcher",
mixins: [ResolveUrlMixin],
props: { props: {
recipe: { type: Number, default: undefined }, recipe: { type: Number, default: undefined },
name: { type: String, default: undefined }, name: { type: String, default: undefined },
@@ -64,7 +66,14 @@ export default {
}, },
mounted() { mounted() {
this.recipes = [] this.recipes = []
this.loadRecipes() switch (this.mode) {
case "recipe":
this.loadRecipes()
break
case "mealplan":
this.loadMealPlans()
break
}
}, },
methods: { methods: {
@@ -74,7 +83,8 @@ export default {
this.$emit("switch", recipe) this.$emit("switch", recipe)
break break
case "mealplan": case "mealplan":
console.log("navigate to", recipe) console.log("navigate to")
window.location.href = this.resolveDjangoUrl("view_recipe", recipe.id)
break break
default: default:
console.log(this.mode, " isn't defined.") console.log(this.mode, " isn't defined.")
@@ -82,7 +92,7 @@ export default {
}, },
loadRecipes: function () { loadRecipes: function () {
let apiClient = new ApiApiFactory() let apiClient = new ApiApiFactory()
let today = new Date(Date.now()).toISOString().split("T")[0]
apiClient apiClient
.relatedRecipe(this.recipe, { query: { levels: 2 } }) .relatedRecipe(this.recipe, { query: { levels: 2 } })
// get related recipes and save them for later // get related recipes and save them for later
@@ -91,39 +101,44 @@ export default {
}) })
// get all recipes for today // get all recipes for today
.then(() => { .then(() => {
apiClient this.loadMealPlans()
.listMealPlans({ })
query: { },
from_date: today, loadMealPlans: function () {
to_date: today, let apiClient = new ApiApiFactory()
}, let today = new Date(Date.now()).toISOString().split("T")[0]
}) apiClient
.then((result) => { .listMealPlans({
let promises = [] query: {
result.data.forEach((mealplan) => { from_date: today,
this.recipe_list.push(mealplan?.recipe) to_date: today,
},
})
.then((result) => {
let promises = []
result.data.forEach((mealplan) => {
this.recipe_list.push(mealplan?.recipe)
promises.push(
apiClient.relatedRecipe(mealplan?.recipe?.id, { query: { levels: 2 } }).then((r) => {
this.recipe_list = [...this.recipe_list, ...r.data]
})
)
})
return Promise.all(promises).then(() => {
let promises = []
let dedup = []
this.recipe_list.forEach((recipe) => {
if (!dedup.includes(recipe.id)) {
dedup.push(recipe.id)
promises.push( promises.push(
apiClient.relatedRecipe(mealplan?.recipe?.id, { query: { levels: 2 } }).then((r) => { apiClient.retrieveRecipe(recipe.id).then((result) => {
this.recipe_list = [...this.recipe_list, ...r.data] this.recipes.push(result.data)
}) })
) )
}) }
return Promise.all(promises).then(() => {
let promises = []
let dedup = []
this.recipe_list.forEach((recipe) => {
if (!dedup.includes(recipe.id)) {
dedup.push(recipe.id)
promises.push(
apiClient.retrieveRecipe(recipe.id).then((result) => {
this.recipes.push(result.data)
})
)
}
})
return Promise.all(promises)
})
}) })
return Promise.all(promises)
})
}) })
}, },
}, },