refactor: migrate to nuxt
This commit is contained in:
12
composables/store/app.ts
Normal file
12
composables/store/app.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { acceptHMRUpdate, defineStore } from 'pinia'
|
||||
|
||||
export const useAppStore = defineStore('app', () => {
|
||||
const deferredPrompt = ref<Event | any>()
|
||||
|
||||
return {
|
||||
deferredPrompt,
|
||||
}
|
||||
})
|
||||
|
||||
if (import.meta.hot)
|
||||
import.meta.hot.accept(acceptHMRUpdate(useAppStore, import.meta.hot))
|
||||
3
composables/store/index.ts
Normal file
3
composables/store/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export * from './app'
|
||||
export * from './recipe'
|
||||
export * from './user'
|
||||
105
composables/store/recipe.ts
Normal file
105
composables/store/recipe.ts
Normal file
@@ -0,0 +1,105 @@
|
||||
import { acceptHMRUpdate, defineStore } from 'pinia'
|
||||
import { useStorage } from '@vueuse/core'
|
||||
import type { RecipeItem, Recipes } from '~/types'
|
||||
|
||||
import recipeData from '~/data/recipe.json'
|
||||
|
||||
const namespace = 'cook'
|
||||
|
||||
/**
|
||||
* 生成随机菜谱
|
||||
* @param recipes
|
||||
* @returns
|
||||
*/
|
||||
function generateRandomRecipe(recipes: Recipes) {
|
||||
return recipes[Math.floor(Math.random() * recipes.length)]
|
||||
}
|
||||
|
||||
/**
|
||||
* survival: 生存模式
|
||||
* strict: 严格
|
||||
* loose: 模糊
|
||||
*/
|
||||
export type SearchMode = 'survival' | 'loose' | 'strict'
|
||||
|
||||
export const useRecipeStore = defineStore('recipe', () => {
|
||||
const recipes = recipeData as Recipes
|
||||
|
||||
/**
|
||||
* 搜索关键字
|
||||
*/
|
||||
const keyword = ref('')
|
||||
|
||||
// can not exported
|
||||
const curStuff = useStorage(`${namespace}:stuff`, new Set<string>())
|
||||
// const curTools = ref(new Set<string>())
|
||||
const curTool = useStorage(`${namespace}:tool`, '')
|
||||
|
||||
const selectedStuff = computed(() => Array.from(curStuff.value))
|
||||
// const selectedTools = computed(() => Array.from(curTools.value))
|
||||
// const selectedTools = ref('')
|
||||
|
||||
const curMode = useStorage<SearchMode>(`${namespace}:mode`, 'loose')
|
||||
|
||||
function toggleStuff(name: string) {
|
||||
if (!curStuff.value)
|
||||
return
|
||||
if (curStuff.value.has(name))
|
||||
curStuff.value.delete(name)
|
||||
else
|
||||
curStuff.value.add(name)
|
||||
}
|
||||
|
||||
function toggleTools(name: string) {
|
||||
if (curTool.value === name)
|
||||
curTool.value = ''
|
||||
else
|
||||
curTool.value = name
|
||||
// if (curTools.value.has(name))
|
||||
// curTools.value.delete(name)
|
||||
// else
|
||||
// curTools.value.add(name)
|
||||
}
|
||||
|
||||
function setMode(mode: SearchMode) {
|
||||
curMode.value = mode
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置
|
||||
*/
|
||||
function reset() {
|
||||
curStuff.value.clear()
|
||||
// curTools.value.clear()
|
||||
curTool.value = ''
|
||||
}
|
||||
|
||||
function addStuff(name: string) {
|
||||
curStuff.value.add(name)
|
||||
}
|
||||
|
||||
const randomRecipe = ref<RecipeItem>(generateRandomRecipe(recipes))
|
||||
|
||||
return {
|
||||
recipes,
|
||||
|
||||
keyword,
|
||||
curTool,
|
||||
curMode,
|
||||
selectedStuff,
|
||||
|
||||
randomRecipe,
|
||||
random: () => { randomRecipe.value = generateRandomRecipe(recipes) },
|
||||
|
||||
clearKeyWord: () => { keyword.value = '' },
|
||||
toggleStuff,
|
||||
toggleTools,
|
||||
reset,
|
||||
setMode,
|
||||
|
||||
addStuff,
|
||||
}
|
||||
})
|
||||
|
||||
if (import.meta.hot)
|
||||
import.meta.hot.accept(acceptHMRUpdate(useRecipeStore, import.meta.hot))
|
||||
34
composables/store/user.ts
Normal file
34
composables/store/user.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { acceptHMRUpdate, defineStore } from 'pinia'
|
||||
|
||||
export const useUserStore = defineStore('user', () => {
|
||||
/**
|
||||
* Current name of the user.
|
||||
*/
|
||||
const savedName = ref('')
|
||||
const previousNames = ref(new Set<string>())
|
||||
|
||||
const usedNames = computed(() => Array.from(previousNames.value))
|
||||
const otherNames = computed(() => usedNames.value.filter(name => name !== savedName.value))
|
||||
|
||||
/**
|
||||
* Changes the current name of the user and saves the one that was used
|
||||
* before.
|
||||
*
|
||||
* @param name - new name to set
|
||||
*/
|
||||
function setNewName(name: string) {
|
||||
if (savedName.value)
|
||||
previousNames.value.add(savedName.value)
|
||||
|
||||
savedName.value = name
|
||||
}
|
||||
|
||||
return {
|
||||
setNewName,
|
||||
otherNames,
|
||||
savedName,
|
||||
}
|
||||
})
|
||||
|
||||
if (import.meta.hot)
|
||||
import.meta.hot.accept(acceptHMRUpdate(useUserStore, import.meta.hot))
|
||||
Reference in New Issue
Block a user