34 lines
874 B
TypeScript
34 lines
874 B
TypeScript
import { StateCreator } from 'zustand'
|
|
import { Income, AppState } from '@/lib/types'
|
|
import { v4 as uuidv4 } from 'uuid'
|
|
|
|
export interface IncomesSlice {
|
|
incomes: Income[]
|
|
addIncome: (income: Omit<Income, 'id'>) => void
|
|
removeIncome: (id: string) => void
|
|
updateIncome: (id: string, income: Partial<Income>) => void
|
|
}
|
|
|
|
export const createIncomesSlice: StateCreator<
|
|
AppState & IncomesSlice,
|
|
[],
|
|
[],
|
|
IncomesSlice
|
|
> = (set) => ({
|
|
incomes: [],
|
|
addIncome: (income) =>
|
|
set((state) => ({
|
|
incomes: [...(state.incomes || []), { ...income, id: uuidv4() }],
|
|
})),
|
|
removeIncome: (id) =>
|
|
set((state) => ({
|
|
incomes: state.incomes.filter((i) => i.id !== id),
|
|
})),
|
|
updateIncome: (id, updatedIncome) =>
|
|
set((state) => ({
|
|
incomes: state.incomes.map((i) =>
|
|
i.id === id ? { ...i, ...updatedIncome } : i
|
|
),
|
|
})),
|
|
})
|