awesome ui changes and expense split restructured
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
"use client";
|
||||
import { create } from "zustand";
|
||||
import { calculateSplits, type Payment } from "@/lib/utils/expense";
|
||||
import type { Session } from "next-auth";
|
||||
import type { PublicUser } from "next-auth";
|
||||
|
||||
export enum SplitTypeEnum {
|
||||
Fixed = "fixed",
|
||||
Percentage = "percentage",
|
||||
Equal = "equal",
|
||||
}
|
||||
export type SplitType = keyof typeof SplitTypeEnum;
|
||||
export const splitTypeKeys = Object.keys(SplitTypeEnum);
|
||||
|
||||
interface ExpenseStore {
|
||||
amount: number;
|
||||
session: Session | null;
|
||||
|
||||
participants: PublicUser[];
|
||||
friendTarget?: string;
|
||||
splitType: SplitType;
|
||||
payments: Payment[];
|
||||
splits: Record<string, string>;
|
||||
|
||||
setAmount: (amount: number) => void;
|
||||
setSession: (session: Session) => void;
|
||||
|
||||
addParticipant: (user: PublicUser) => void;
|
||||
removeParticipant: (userId: string) => void;
|
||||
|
||||
setFriendTarget: (id?: string) => void;
|
||||
setSplitType: (type: SplitType) => void;
|
||||
setPayments: (payments: Array<Payment>) => void;
|
||||
recalculateSplits: () => void;
|
||||
resetExpenseStore: () => void;
|
||||
}
|
||||
|
||||
const defaultValues: Pick<
|
||||
ExpenseStore,
|
||||
| "amount"
|
||||
| "participants"
|
||||
| "friendTarget"
|
||||
| "splitType"
|
||||
| "payments"
|
||||
| "splits"
|
||||
> = {
|
||||
amount: 0,
|
||||
participants: [],
|
||||
friendTarget: undefined,
|
||||
splitType: "Equal",
|
||||
payments: [],
|
||||
splits: {},
|
||||
};
|
||||
|
||||
export const useExpenseStore = create<ExpenseStore>((set, get) => ({
|
||||
...defaultValues,
|
||||
session: null,
|
||||
setAmount: (amount) => set({ amount }),
|
||||
setSession: (session) =>
|
||||
set(
|
||||
{
|
||||
session,
|
||||
participants: [session.user],
|
||||
payments: [{ amount: get().amount, userId: session.user.id }],
|
||||
},
|
||||
false
|
||||
),
|
||||
|
||||
addParticipant: (newUser) => {
|
||||
const { participants } = get();
|
||||
if (!participants.find((user) => user.id === newUser.id)) {
|
||||
set({ participants: [...participants, newUser] });
|
||||
}
|
||||
},
|
||||
|
||||
removeParticipant: (userId) => {
|
||||
const filtered = get().participants.filter((user) => user.id !== userId);
|
||||
set({ participants: filtered });
|
||||
},
|
||||
|
||||
setFriendTarget: (id) => set({ friendTarget: id }),
|
||||
setSplitType: (type) => set({ splitType: type }),
|
||||
setPayments: (payments) => set({ payments }),
|
||||
|
||||
recalculateSplits: () => {
|
||||
const { amount, participants, splitType } = get();
|
||||
const splits = calculateSplits({
|
||||
amount,
|
||||
users: participants.map((u) => u.id!),
|
||||
splitType,
|
||||
});
|
||||
set({ splits });
|
||||
},
|
||||
|
||||
resetExpenseStore: () => set(defaultValues),
|
||||
}));
|
||||
Reference in New Issue
Block a user