/
GEOSHEV
/
Event-app
Обзор
Документация
Войти
/
GEOSHEV
/
Event-app
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
develop
src/store/useChildrenStore.ts
71 строка
2 KB
Gosha
add zustand store
12 июн 2026, 19:56
12 июн 2026, 19:56
4d5982d
Код
Авторство
О чём код?
import { createChild, deleteChild, getChildren, updateChild, } from "@/src/api/user"; import { Child, CreateChildDto, UpdateChildDto } from "@/src/types/auth"; import { create } from "zustand"; type ChildrenStore = { children: Child[]; selectedChildId: string | null; isLoading: boolean; fetchChildren: () => Promise<void>; addChild: (dto: CreateChildDto) => Promise<void>; editChild: (id: string, dto: UpdateChildDto) => Promise<void>; removeChild: (id: string) => Promise<void>; setSelectedChildId: (id: string | null) => void; }; export const useChildrenStore = create<ChildrenStore>((set, get) => ({ children: [], selectedChildId: null, isLoading: false, fetchChildren: async () => { set({ isLoading: true }); try { const children = await getChildren(); set({ children }); if (!get().selectedChildId && children.length > 0) { set({ selectedChildId: children[0].id }); } } catch (error) { console.log("fetchChildren error:", error); } finally { set({ isLoading: false }); } }, addChild: async (dto) => { await createChild(dto); const children = await getChildren(); const newChild = children[children.length - 1]; set({ children, selectedChildId: newChild ? newChild.id : get().selectedChildId, }); }, editChild: async (id, dto) => { await updateChild(id, dto); set((state) => ({ children: state.children.map((c) => c.id === id ? { ...c, firstName: dto.firstName, secondName: dto.secondName } : c, ), })); }, removeChild: async (id) => { await deleteChild(id); set((state) => ({ children: state.children.filter((c) => c.id !== id), })); }, setSelectedChildId: (id) => set({ selectedChildId: id }), }));