42 lines
1.3 KiB
JavaScript
42 lines
1.3 KiB
JavaScript
|
|
import { create } from 'zustand'
|
||
|
|
|
||
|
|
const useProjectStore = create((set, get) => ({
|
||
|
|
projects: [],
|
||
|
|
loading: false,
|
||
|
|
error: null,
|
||
|
|
|
||
|
|
setProjects: (projects) => set({ projects }),
|
||
|
|
setLoading: (loading) => set({ loading }),
|
||
|
|
setError: (error) => set({ error }),
|
||
|
|
|
||
|
|
addProject: (p) => set((s) => ({ projects: [p, ...s.projects] })),
|
||
|
|
|
||
|
|
updateProject: (updated) => set((s) => ({
|
||
|
|
projects: s.projects.map(p => p.id === updated.id ? { ...updated, deliverables: p.deliverables } : p),
|
||
|
|
})),
|
||
|
|
|
||
|
|
removeProject: (id) => set((s) => ({ projects: s.projects.filter(p => p.id !== id) })),
|
||
|
|
|
||
|
|
addDeliverable: (d) => set((s) => ({
|
||
|
|
projects: s.projects.map(p => p.id === d.project_id
|
||
|
|
? { ...p, deliverables: [...(p.deliverables||[]), d].sort((a,b) => new Date(a.due_date)-new Date(b.due_date)) }
|
||
|
|
: p
|
||
|
|
),
|
||
|
|
})),
|
||
|
|
|
||
|
|
updateDeliverable: (updated) => set((s) => ({
|
||
|
|
projects: s.projects.map(p => p.id === updated.project_id
|
||
|
|
? { ...p, deliverables: p.deliverables.map(d => d.id === updated.id ? updated : d) }
|
||
|
|
: p
|
||
|
|
),
|
||
|
|
})),
|
||
|
|
|
||
|
|
removeDeliverable: (id) => set((s) => ({
|
||
|
|
projects: s.projects.map(p => ({ ...p, deliverables: (p.deliverables||[]).filter(d => d.id !== id) })),
|
||
|
|
})),
|
||
|
|
|
||
|
|
getProjectById: (id) => get().projects.find(p => p.id === id),
|
||
|
|
}))
|
||
|
|
|
||
|
|
export default useProjectStore
|