From ebc6e2d037a99f7cf9112e88ab4e5ab6d6066441 Mon Sep 17 00:00:00 2001 From: YunYouJun Date: Mon, 9 May 2022 02:51:29 +0800 Subject: [PATCH] refactor: add useRecipe --- src/components/ChooseFood.vue | 66 ++--------------------------- src/composables/recipe.ts | 78 +++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 62 deletions(-) create mode 100644 src/composables/recipe.ts diff --git a/src/components/ChooseFood.vue b/src/components/ChooseFood.vue index cb4656d..452f8eb 100644 --- a/src/components/ChooseFood.vue +++ b/src/components/ChooseFood.vue @@ -9,50 +9,15 @@ import { useRecipeStore } from '~/stores/recipe' import { useInvisibleElement } from '~/composables/helper' import { useEmojiAnimation } from '~/composables/animation' +import { useRecipe } from '~/composables/recipe' const recipe = ref(recipeData as Recipe) const rStore = useRecipeStore() -const { curMode, curTool } = storeToRefs(rStore) +const { curTool } = storeToRefs(rStore) const curStuff = computed(() => rStore.selectedStuff) -// 默认严格模式 -const displayedRecipe = computed(() => { - if (curMode.value === 'strict') { - return recipe.value.filter((item) => { - const stuffFlag = curStuff.value.every(stuff => item.stuff.includes(stuff)) - const toolFlag = item.tools?.includes(curTool.value) - return curTool.value ? stuffFlag && toolFlag : stuffFlag - }) - } - else if (curMode.value === 'loose') { - return recipe.value.filter((item) => { - const stuffFlag = curStuff.value.some(stuff => item.stuff.includes(stuff)) - const toolFlag = item.tools?.includes(curTool.value) - - // 同时存在 厨具和材料,则同时判断 - if (curTool.value && curStuff.value.length) { - return stuffFlag && toolFlag - } - else { - if (curStuff.value.length) - return stuffFlag - else if (curTool.value) - return toolFlag - - return false - } - }) - } - // survival - else { - return recipe.value.filter((item) => { - const stuffFlag = item.stuff.every(stuff => curStuff.value.includes(stuff)) - const toolFlag = item.tools?.includes(curTool.value) - return curTool.value ? stuffFlag && toolFlag : stuffFlag - }) - } -}) +const { displayedRecipe, clickTool } = useRecipe(recipe) const recipeBtn = ref() const { playAnimation } = useEmojiAnimation(recipeBtn) @@ -77,31 +42,9 @@ const toggleStuff = (item: StuffItem, category = '', e?: Event) => { }) } -/** - * toggle tool - * @param item - */ -const clickTool = (item: StuffItem) => { - const value = item.name - rStore.toggleTools(value) - - gtm?.trackEvent({ - event: 'click', - category: `tool_${value}`, - action: 'click_tool', - label: '工具', - }) - gtm?.trackEvent({ - event: 'click_tool', - action: item.name, - }) -} - const recipePanel = ref() const { isVisible, show } = useInvisibleElement(recipePanel) - - function generateRandomRecipe() { return recipe.value[Math.floor(Math.random() * recipe.value.length)] } @@ -238,10 +181,9 @@ const randomRecipe = ref(generateRandomRecipe()) 反馈新的菜谱! - -
+
今天吃什么?
diff --git a/src/composables/recipe.ts b/src/composables/recipe.ts new file mode 100644 index 0000000..80d5df3 --- /dev/null +++ b/src/composables/recipe.ts @@ -0,0 +1,78 @@ +import type { Ref } from 'vue' +import { storeToRefs } from 'pinia' +import { useGtm } from '@gtm-support/vue-gtm' +import type { Recipe } from '~/types' + +import { useRecipeStore } from '~/stores/recipe' +import type { StuffItem } from '~/data/food' + +export function useRecipe(recipe: Ref) { + const gtm = useGtm() + + const rStore = useRecipeStore() + const { curMode, curTool } = storeToRefs(rStore) + const curStuff = computed(() => rStore.selectedStuff) + + // 默认严格模式 + const displayedRecipe = computed(() => { + if (curMode.value === 'strict') { + return recipe.value.filter((item) => { + const stuffFlag = curStuff.value.every(stuff => item.stuff.includes(stuff)) + const toolFlag = item.tools?.includes(curTool.value) + return curTool.value ? stuffFlag && toolFlag : stuffFlag + }) + } + else if (curMode.value === 'loose') { + return recipe.value.filter((item) => { + const stuffFlag = curStuff.value.some(stuff => item.stuff.includes(stuff)) + const toolFlag = item.tools?.includes(curTool.value) + + // 同时存在 厨具和材料,则同时判断 + if (curTool.value && curStuff.value.length) { + return stuffFlag && toolFlag + } + else { + if (curStuff.value.length) + return stuffFlag + else if (curTool.value) + return toolFlag + + return false + } + }) + } + // survival + else { + return recipe.value.filter((item) => { + const stuffFlag = item.stuff.every(stuff => curStuff.value.includes(stuff)) + const toolFlag = item.tools?.includes(curTool.value) + return curTool.value ? stuffFlag && toolFlag : stuffFlag + }) + } + }) + + /** + * toggle tool + * @param item + */ + const clickTool = (item: StuffItem) => { + const value = item.name + rStore.toggleTools(value) + + gtm?.trackEvent({ + event: 'click', + category: `tool_${value}`, + action: 'click_tool', + label: '工具', + }) + gtm?.trackEvent({ + event: 'click_tool', + action: item.name, + }) + } + + return { + displayedRecipe, + clickTool, + } +}