Files
oil-formula-calculator/frontend/src/stores/plans.js
Hera Zhao 1ca9bc1758
All checks were successful
PR Preview / teardown-preview (pull_request) Has been skipped
Test / unit-test (push) Successful in 5s
Test / build-check (push) Successful in 4s
PR Preview / test (pull_request) Successful in 5s
PR Preview / deploy-preview (pull_request) Successful in 17s
Test / e2e-test (push) Successful in 50s
fix: plans store使用api()替代不存在的requestJSON
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-12 21:55:00 +00:00

66 lines
1.9 KiB
JavaScript

import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { api } from '../composables/useApi'
export const usePlansStore = defineStore('plans', () => {
const plans = ref([])
const teachers = ref([])
const shoppingList = ref([])
const activePlan = computed(() => plans.value.find(p => p.status === 'active'))
const pendingPlans = computed(() => plans.value.filter(p => p.status === 'pending'))
async function loadPlans() {
const res = await api('/api/oil-plans')
if (res.ok) plans.value = await res.json()
}
async function loadTeachers() {
const res = await api('/api/teachers')
if (res.ok) teachers.value = await res.json()
}
async function createPlan(healthDesc, teacherId) {
const res = await api('/api/oil-plans', {
method: 'POST',
body: JSON.stringify({ health_desc: healthDesc, teacher_id: teacherId }),
})
if (!res.ok) throw new Error('创建失败')
await loadPlans()
return res.json()
}
async function updatePlan(planId, data) {
await api(`/api/oil-plans/${planId}`, {
method: 'PUT',
body: JSON.stringify(data),
})
await loadPlans()
}
async function addRecipe(planId, recipeName, ingredients, timesPerMonth) {
await api(`/api/oil-plans/${planId}/recipes`, {
method: 'POST',
body: JSON.stringify({ recipe_name: recipeName, ingredients, times_per_month: timesPerMonth }),
})
await loadPlans()
}
async function removeRecipe(planId, recipeId) {
await api(`/api/oil-plans/${planId}/recipes/${recipeId}`, { method: 'DELETE' })
await loadPlans()
}
async function loadShoppingList(planId) {
const res = await api(`/api/oil-plans/${planId}/shopping-list`)
if (res.ok) shoppingList.value = await res.json()
}
return {
plans, teachers, shoppingList,
activePlan, pendingPlans,
loadPlans, loadTeachers, createPlan, updatePlan,
addRecipe, removeRecipe, loadShoppingList,
}
})