| |
| |
| |
| |
| |
| |
| import { create } from 'zustand'; |
| import { immer } from 'zustand/middleware/immer'; |
| import type { Ride } from '../types/ride'; |
| import type { Match } from '../types/match'; |
|
|
| |
|
|
| interface RideState { |
| |
| activeRide: Ride | null; |
| |
| activeMatch: Match | null; |
| |
| isSearching: boolean; |
| |
| error: string | null; |
| } |
|
|
| |
|
|
| interface RideActions { |
| |
| setActiveRide: (ride: Ride | null) => void; |
| |
| setActiveMatch: (match: Match | null) => void; |
| |
| setSearching: (value: boolean) => void; |
| |
| setError: (error: string | null) => void; |
| |
| clearActiveRide: () => void; |
| |
| updateRideStatus: (status: Ride['status']) => void; |
| |
| updateMatchStatus: (status: Match['status']) => void; |
| } |
|
|
| |
|
|
| export const useRideStore = create<RideState & RideActions>()( |
| immer((set) => ({ |
| |
| activeRide: null, |
| activeMatch: null, |
| isSearching: false, |
| error: null, |
|
|
| |
| setActiveRide: (ride) => |
| set((state) => { |
| state.activeRide = ride; |
| }), |
|
|
| setActiveMatch: (match) => |
| set((state) => { |
| state.activeMatch = match; |
| }), |
|
|
| setSearching: (value) => |
| set((state) => { |
| state.isSearching = value; |
| if (value) { |
| state.error = null; |
| } |
| }), |
|
|
| setError: (error) => |
| set((state) => { |
| state.error = error; |
| }), |
|
|
| clearActiveRide: () => |
| set((state) => { |
| state.activeRide = null; |
| state.activeMatch = null; |
| state.isSearching = false; |
| state.error = null; |
| }), |
|
|
| updateRideStatus: (status) => |
| set((state) => { |
| if (state.activeRide) { |
| state.activeRide.status = status; |
| } |
| }), |
|
|
| updateMatchStatus: (status) => |
| set((state) => { |
| if (state.activeMatch) { |
| state.activeMatch.status = status; |
| } |
| }), |
| })), |
| ); |
|
|
| |
|
|
| export const selectHasActiveRide = (state: RideState) => |
| state.activeRide !== null && state.activeMatch !== null; |
|
|
| export const selectRideId = (state: RideState) => state.activeRide?.id ?? null; |
|
|
| export const selectMatchId = (state: RideState) => |
| state.activeMatch?.id ?? null; |
|
|
| export const selectRideType = (state: RideState) => |
| state.activeRide?.type ?? null; |
|
|