Skip to content

Commit

Permalink
feat: add random recipes localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
YunYouJun committed Nov 12, 2023
1 parent b2cf053 commit 1ab402a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
9 changes: 5 additions & 4 deletions components/RandomRecipe.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ const { random, randomRecipes } = useRandomRecipe(count)
</button>
</div>

<button cursor-pointer class="inline-flex inline-flex items-center justify-center rounded-md border-none bg-blue-600 px-3 py-1.5 text-sm font-semibold leading-6 text-white shadow-sm hover:bg-blue-500 focus-visible:outline-2 focus-visible:outline-blue-600 focus-visible:outline-offset-2 focus-visible:outline" @click="random">
<div class="transition" hover="text-blue-500" i-ri-refresh-line mr-1 inline-flex />
<div>随机一下</div>
</button>

<div v-show="randomRecipes.length > 0">
<button cursor-pointer class="inline-flex inline-flex items-center justify-center rounded-md border-none bg-blue-600 px-3 py-1.5 text-sm font-semibold leading-6 text-white shadow-sm hover:bg-blue-500 focus-visible:outline-2 focus-visible:outline-blue-600 focus-visible:outline-offset-2 focus-visible:outline" @click="random">
<div class="transition" hover="text-blue-500" i-ri-refresh-line mr-1 inline-flex />
<div>随机一下</div>
</button>
<div m="t-8" flex="~ col">
<template v-for="recipe, i in randomRecipes" :key="i">
<DishTag v-if="recipe" :dish="recipe" />
Expand Down
8 changes: 6 additions & 2 deletions composables/recipe.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { useStorage } from '@vueuse/core'
import { namespace } from '~/constants'
import type { DbRecipeItem } from '~/utils/db'

/**
* 随机几道菜
* @param total
*/
export function useRandomRecipe(total: Ref<number>) {
const randomRecipes = ref<(DbRecipeItem | undefined)[]>([])
const randomRecipes = useStorage<(DbRecipeItem | undefined)[]>(`${namespace}:random:recipes`, [])
async function random() {
const length = await db.recipes.count()
const randomArr = generateRandomArray(length, total.value)
Expand All @@ -19,7 +21,9 @@ export function useRandomRecipe(total: Ref<number>) {
})

onMounted(() => {
random()
// 如果没有随机菜谱,就生成一次
if (randomRecipes.value.length <= 0)
random()
})

return {
Expand Down

1 comment on commit 1ab402a

@vercel
Copy link

@vercel vercel bot commented on 1ab402a Nov 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

cook – ./

cook.yunle.app
cook-yunyoujun.vercel.app
cook-today.vercel.app
cook-git-dev-yunyoujun.vercel.app

Please sign in to comment.