feat(plantes): store plants — actions variety CRUD
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
|
// frontend/src/stores/plants.ts
|
||||||
import { defineStore } from 'pinia'
|
import { defineStore } from 'pinia'
|
||||||
import { ref } from 'vue'
|
import { ref } from 'vue'
|
||||||
import { plantsApi, type Plant } from '@/api/plants'
|
import { plantsApi, type Plant, type PlantVariety } from '@/api/plants'
|
||||||
|
|
||||||
export const usePlantsStore = defineStore('plants', () => {
|
export const usePlantsStore = defineStore('plants', () => {
|
||||||
const plants = ref<Plant[]>([])
|
const plants = ref<Plant[]>([])
|
||||||
@@ -18,10 +19,45 @@ export const usePlantsStore = defineStore('plants', () => {
|
|||||||
return created
|
return created
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function update(id: number, p: Partial<Plant>) {
|
||||||
|
const updated = await plantsApi.update(id, p)
|
||||||
|
const idx = plants.value.findIndex(x => x.id === id)
|
||||||
|
if (idx !== -1) plants.value[idx] = updated
|
||||||
|
return updated
|
||||||
|
}
|
||||||
|
|
||||||
async function remove(id: number) {
|
async function remove(id: number) {
|
||||||
await plantsApi.delete(id)
|
await plantsApi.delete(id)
|
||||||
plants.value = plants.value.filter(p => p.id !== id)
|
plants.value = plants.value.filter(p => p.id !== id)
|
||||||
}
|
}
|
||||||
|
|
||||||
return { plants, loading, fetchAll, create, remove }
|
async function createVariety(plantId: number, v: Partial<PlantVariety>) {
|
||||||
|
const created = await plantsApi.createVariety(plantId, v)
|
||||||
|
const plant = plants.value.find(p => p.id === plantId)
|
||||||
|
if (plant) {
|
||||||
|
if (!plant.varieties) plant.varieties = []
|
||||||
|
plant.varieties.push(created)
|
||||||
|
}
|
||||||
|
return created
|
||||||
|
}
|
||||||
|
|
||||||
|
async function updateVariety(plantId: number, vid: number, v: Partial<PlantVariety>) {
|
||||||
|
const updated = await plantsApi.updateVariety(plantId, vid, v)
|
||||||
|
const plant = plants.value.find(p => p.id === plantId)
|
||||||
|
if (plant?.varieties) {
|
||||||
|
const idx = plant.varieties.findIndex(x => x.id === vid)
|
||||||
|
if (idx !== -1) plant.varieties[idx] = updated
|
||||||
|
}
|
||||||
|
return updated
|
||||||
|
}
|
||||||
|
|
||||||
|
async function removeVariety(plantId: number, vid: number) {
|
||||||
|
await plantsApi.deleteVariety(plantId, vid)
|
||||||
|
const plant = plants.value.find(p => p.id === plantId)
|
||||||
|
if (plant?.varieties) {
|
||||||
|
plant.varieties = plant.varieties.filter(v => v.id !== vid)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return { plants, loading, fetchAll, create, update, remove, createVariety, updateVariety, removeVariety }
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user