This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
import { create } from "zustand";
|
||||
import type { CouponStrategy } from "@/lib/api/predictions/types";
|
||||
import type { CouponItemDto } from "@/lib/api/coupons/types";
|
||||
|
||||
interface CouponState {
|
||||
strategy: CouponStrategy;
|
||||
items: CouponItemDto[];
|
||||
isBuilding: boolean;
|
||||
}
|
||||
|
||||
interface CouponActions {
|
||||
setStrategy: (strategy: CouponStrategy) => void;
|
||||
addItem: (item: CouponItemDto) => void;
|
||||
removeItem: (matchId: string) => void;
|
||||
clearCoupon: () => void;
|
||||
setIsBuilding: (isBuilding: boolean) => void;
|
||||
}
|
||||
|
||||
type CouponStore = CouponState & CouponActions;
|
||||
|
||||
const initialState: CouponState = {
|
||||
strategy: "BALANCED",
|
||||
items: [],
|
||||
isBuilding: false,
|
||||
};
|
||||
|
||||
export const useCouponStore = create<CouponStore>()((set) => ({
|
||||
...initialState,
|
||||
|
||||
setStrategy: (strategy) => set({ strategy }),
|
||||
|
||||
addItem: (item) =>
|
||||
set((state) => {
|
||||
// Prevent duplicate match entries
|
||||
const exists = state.items.some((i) => i.matchId === item.matchId);
|
||||
if (exists) return state;
|
||||
return { items: [...state.items, item] };
|
||||
}),
|
||||
|
||||
removeItem: (matchId) =>
|
||||
set((state) => ({
|
||||
items: state.items.filter((i) => i.matchId !== matchId),
|
||||
})),
|
||||
|
||||
clearCoupon: () => set(initialState),
|
||||
|
||||
setIsBuilding: (isBuilding) => set({ isBuilding }),
|
||||
}));
|
||||
@@ -0,0 +1,43 @@
|
||||
import { create } from "zustand";
|
||||
import type { SportType } from "@/lib/api/matches/types";
|
||||
|
||||
interface MatchState {
|
||||
selectedMatchIds: Set<string>;
|
||||
sport: SportType;
|
||||
leagueFilter: string | null;
|
||||
}
|
||||
|
||||
interface MatchActions {
|
||||
toggleMatch: (matchId: string) => void;
|
||||
clearSelection: () => void;
|
||||
setSport: (sport: SportType) => void;
|
||||
setLeague: (leagueId: string | null) => void;
|
||||
isSelected: (matchId: string) => boolean;
|
||||
}
|
||||
|
||||
type MatchStore = MatchState & MatchActions;
|
||||
|
||||
export const useMatchStore = create<MatchStore>()((set, get) => ({
|
||||
selectedMatchIds: new Set<string>(),
|
||||
sport: "football",
|
||||
leagueFilter: null,
|
||||
|
||||
toggleMatch: (matchId) =>
|
||||
set((state) => {
|
||||
const next = new Set(state.selectedMatchIds);
|
||||
if (next.has(matchId)) {
|
||||
next.delete(matchId);
|
||||
} else {
|
||||
next.add(matchId);
|
||||
}
|
||||
return { selectedMatchIds: next };
|
||||
}),
|
||||
|
||||
clearSelection: () => set({ selectedMatchIds: new Set<string>() }),
|
||||
|
||||
setSport: (sport) => set({ sport, leagueFilter: null }),
|
||||
|
||||
setLeague: (leagueId) => set({ leagueFilter: leagueId }),
|
||||
|
||||
isSelected: (matchId) => get().selectedMatchIds.has(matchId),
|
||||
}));
|
||||
Reference in New Issue
Block a user