Some checks failed
Test / unit-test (push) Successful in 5s
Test / build-check (push) Successful in 4s
Test / e2e-test (push) Failing after 1m7s
PR Preview / test (pull_request) Has been skipped
PR Preview / teardown-preview (pull_request) Successful in 14s
PR Preview / deploy-preview (pull_request) Has been skipped
- Add en_name column to recipes table (migration in database.py) - Include en_name in recipe API responses and RecipeUpdate model - Save en_name when admin/senior_editor applies translation - Load en_name on overlay open, so translation persists across sessions Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
107 lines
3.0 KiB
JavaScript
107 lines
3.0 KiB
JavaScript
import { defineStore } from 'pinia'
|
|
import { ref } from 'vue'
|
|
import { api } from '../composables/useApi'
|
|
|
|
export const useRecipesStore = defineStore('recipes', () => {
|
|
const recipes = ref([])
|
|
const allTags = ref([])
|
|
const userFavorites = ref([])
|
|
|
|
// Actions
|
|
async function loadRecipes() {
|
|
const data = await api.get('/api/recipes')
|
|
recipes.value = data.map((r) => ({
|
|
_id: r._id ?? r.id,
|
|
_owner_id: r._owner_id ?? r.owner_id,
|
|
_owner_name: r._owner_name ?? r.owner_name ?? '',
|
|
_version: r._version ?? r.version ?? 1,
|
|
name: r.name,
|
|
en_name: r.en_name ?? '',
|
|
note: r.note ?? '',
|
|
tags: r.tags ?? [],
|
|
ingredients: (r.ingredients ?? []).map((ing) => ({
|
|
oil: ing.oil_name ?? ing.oil ?? ing.name,
|
|
drops: ing.drops,
|
|
})),
|
|
}))
|
|
}
|
|
|
|
async function loadTags() {
|
|
const data = await api.get('/api/tags')
|
|
const apiTags = data.map((t) => (typeof t === 'string' ? t : t.name))
|
|
const recipeTags = recipes.value.flatMap((r) => r.tags)
|
|
const tagSet = new Set([...apiTags, ...recipeTags])
|
|
allTags.value = [...tagSet].sort((a, b) => a.localeCompare(b, 'zh'))
|
|
}
|
|
|
|
async function loadFavorites() {
|
|
try {
|
|
const data = await api.get('/api/favorites')
|
|
userFavorites.value = data.map((f) => f._id ?? f.id ?? f.recipe_id ?? f)
|
|
} catch {
|
|
userFavorites.value = []
|
|
}
|
|
}
|
|
|
|
async function saveRecipe(recipe) {
|
|
if (recipe._id) {
|
|
const data = await api.put(`/api/recipes/${recipe._id}`, recipe)
|
|
const idx = recipes.value.findIndex((r) => r._id === recipe._id)
|
|
if (idx !== -1) {
|
|
recipes.value[idx] = { ...recipes.value[idx], ...recipe, _version: data._version ?? data.version ?? recipe._version }
|
|
}
|
|
return data
|
|
} else {
|
|
const data = await api.post('/api/recipes', recipe)
|
|
await loadRecipes()
|
|
return data
|
|
}
|
|
}
|
|
|
|
async function deleteRecipe(id) {
|
|
await api.delete(`/api/recipes/${id}`)
|
|
recipes.value = recipes.value.filter((r) => r._id !== id)
|
|
}
|
|
|
|
async function toggleFavorite(recipeId) {
|
|
if (userFavorites.value.includes(recipeId)) {
|
|
await api.delete(`/api/favorites/${recipeId}`)
|
|
userFavorites.value = userFavorites.value.filter((id) => id !== recipeId)
|
|
} else {
|
|
await api.post(`/api/favorites/${recipeId}`)
|
|
userFavorites.value.push(recipeId)
|
|
}
|
|
}
|
|
|
|
function isFavorite(recipe) {
|
|
return userFavorites.value.includes(recipe._id)
|
|
}
|
|
|
|
async function createTag(name) {
|
|
await api.post('/api/tags', { name })
|
|
if (!allTags.value.includes(name)) {
|
|
allTags.value = [...allTags.value, name].sort((a, b) => a.localeCompare(b, 'zh'))
|
|
}
|
|
}
|
|
|
|
async function deleteTag(name) {
|
|
await api.delete(`/api/tags/${encodeURIComponent(name)}`)
|
|
allTags.value = allTags.value.filter((t) => t !== name)
|
|
}
|
|
|
|
return {
|
|
recipes,
|
|
allTags,
|
|
userFavorites,
|
|
loadRecipes,
|
|
loadTags,
|
|
loadFavorites,
|
|
saveRecipe,
|
|
deleteRecipe,
|
|
toggleFavorite,
|
|
isFavorite,
|
|
createTag,
|
|
deleteTag,
|
|
}
|
|
})
|