Skip to content

Commit

Permalink
Feature: Toggle display of ingredient references in recipe instructio…
Browse files Browse the repository at this point in the history
…ns (#1268)

* Better cooking mode

* Fix wrong event sent

* feat/cookmode recipe page integration

* implement scaling in cook mode + minor padding

Co-authored-by: Hayden <[email protected]>
  • Loading branch information
Miroito and hay-kot authored Jul 10, 2022
1 parent 2809cef commit c64da1f
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 142 deletions.
2 changes: 1 addition & 1 deletion frontend/components/Domain/Recipe/RecipeActionMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<RecipeFavoriteBadge v-if="loggedIn" class="mx-1" color="info" button-style :slug="slug" show-always />
<v-tooltip v-if="!locked" bottom color="info">
<template #activator="{ on, attrs }">
<v-btn fab small class="mx-1" color="info" v-bind="attrs" v-on="on" @click="$emit('input', true)">
<v-btn fab small class="mx-1" color="info" v-bind="attrs" v-on="on" @click="$emit('edit', true)">
<v-icon> {{ $globals.icons.edit }} </v-icon>
</v-btn>
</template>
Expand Down
77 changes: 69 additions & 8 deletions frontend/components/Domain/Recipe/RecipeInstructions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@
</v-card>
</v-dialog>

<div class="d-flex justify-space-between justify-start">
<div v-if="showCookMode" class="d-flex justify-space-between justify-start">
<h2 class="mb-4 mt-1">{{ $t("recipe.instructions") }}</h2>
<BaseButton v-if="!public" minor :to="$router.currentRoute.path + '/cook'" cancel color="primary">
<BaseButton v-if="!public && !edit" minor cancel color="primary" @click="toggleCookMode()">
<template #icon>
{{ $globals.icons.primary }}
</template>
Cook
Cook Mode
</BaseButton>
</div>
<draggable
Expand Down Expand Up @@ -198,6 +198,14 @@
<div v-show="!isChecked(index) && !edit" class="m-0 p-0">
<v-card-text class="markdown">
<VueMarkdown class="markdown" :source="step.text"> </VueMarkdown>
<div v-if="cookMode && step.ingredientReferences && step.ingredientReferences.length > 0">
<v-divider class="mb-2"></v-divider>
<div
v-for="ing in step.ingredientReferences"
:key="ing.referenceId"
v-html="getIngredientByRefId(ing.referenceId)"
/>
</div>
</v-card-text>
</div>
</v-expand-transition>
Expand All @@ -213,7 +221,16 @@
import draggable from "vuedraggable";
// @ts-ignore vue-markdown has no types
import VueMarkdown from "@adapttive/vue-markdown";
import { ref, toRefs, reactive, defineComponent, watch, onMounted, useContext } from "@nuxtjs/composition-api";
import {
ref,
toRefs,
reactive,
defineComponent,
watch,
onMounted,
useContext,
computed,
} from "@nuxtjs/composition-api";
import { RecipeStep, IngredientReferences, RecipeIngredient, RecipeAsset } from "~/types/api-types/recipe";
import { parseIngredientText } from "~/composables/recipes";
import { uuid4, detectServerBaseUrl } from "~/composables/use-utils";
Expand Down Expand Up @@ -264,6 +281,14 @@ export default defineComponent({
type: Array as () => RecipeAsset[],
required: true,
},
cookMode: {
type: Boolean,
default: false,
},
scale: {
type: Number,
default: 1,
},
},
setup(props, context) {
Expand Down Expand Up @@ -313,13 +338,20 @@ export default defineComponent({
});
});
const showCookMode = ref(false);
// Eliminate state with an eager call to watcher?
onMounted(() => {
props.value.forEach((element) => {
props.value.forEach((element: RecipeStep) => {
if (element.id !== undefined) {
showTitleEditor.value[element.id] = validateTitle(element.title);
}
// showCookMode.value = false;
if (showCookMode.value === false && element.ingredientReferences && element.ingredientReferences.length > 0) {
showCookMode.value = true;
}
showTitleEditor.value = { ...showTitleEditor.value };
});
});
Expand Down Expand Up @@ -376,6 +408,14 @@ export default defineComponent({
referenceId: ref,
};
});
// Update the visibility of the cook mode button
showCookMode.value = false;
props.value.forEach((element) => {
if (showCookMode.value === false && element.ingredientReferences && element.ingredientReferences.length > 0) {
showCookMode.value = true;
}
});
state.dialog = false;
}
Expand Down Expand Up @@ -446,12 +486,27 @@ export default defineComponent({
});
}
function getIngredientByRefId(refId: string) {
const ing = props.ingredients.find((ing) => ing.referenceId === refId) || "";
const ingredientLookup = computed(() => {
const results: { [key: string]: RecipeIngredient } = {};
return props.ingredients.reduce((prev, ing) => {
if (ing.referenceId === undefined) {
return prev;
}
prev[ing.referenceId] = ing;
return prev;
}, results);
});
function getIngredientByRefId(refId: string | undefined) {
if (refId === undefined) {
return "";
}
const ing = ingredientLookup.value[refId] ?? "";
if (ing === "") {
return "";
}
return parseIngredientText(ing, props.disableAmount);
return parseIngredientText(ing, props.disableAmount, props.scale);
}
// ===============================================================
Expand Down Expand Up @@ -571,6 +626,10 @@ export default defineComponent({
props.value[index].text += text;
}
function toggleCookMode() {
context.emit("cookModeToggle");
}
return {
// Image Uploader
toggleDragMode,
Expand Down Expand Up @@ -598,6 +657,8 @@ export default defineComponent({
updateIndex,
autoSetReferences,
parseIngredientText,
toggleCookMode,
showCookMode,
};
},
});
Expand Down
120 changes: 0 additions & 120 deletions frontend/pages/recipe/_slug/cook.vue

This file was deleted.

Loading

0 comments on commit c64da1f

Please sign in to comment.