All files / src/store movementSlice.ts

52.83% Statements 84/159
100% Branches 1/1
9.09% Functions 1/11
52.83% Lines 84/159

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198      1x 1x                                     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x                         1x   1x 1x 1x                   1x   1x 1x 1x                   1x   1x 1x 1x                   1x   1x 1x 1x 1x 1x     1x         1x     1x 1x   1x 1x     1x 1x       1x 1x     1x     1x 1x     1x 1x       1x 1x     1x     1x 1x     1x 1x         1x 1x     1x     1x 1x     1x 1x     1x 1x     1x 1x 1x   1x     1x 1x 1x 1x 1x 1x 1x   1x  
/**
 * Redux slice for Line Movement Analytics state management
 */
import { createSlice, createAsyncThunk, PayloadAction } from '@reduxjs/toolkit';
import { lineMovementService } from '../services/line-movement.service';
import { LineMovement, MovementStats, MovementFilters } from '../types/movements.types';
import { RootState } from './index';
 
export interface MovementState {
  liveMovements: LineMovement[];
  steamMoves: LineMovement[];
  selectedGameMovements: LineMovement[] | null;
  selectedGameId: string | null;
  selectedGame: any | null;
  statistics: MovementStats | null;
  steamMovesCount: number;
  totalMovements: number;
  filters: MovementFilters;
  loading: boolean;
  error: string | null;
  lastFetched: string | null;
}
 
const initialState: MovementState = {
  liveMovements: [],
  steamMoves: [],
  selectedGameMovements: null,
  selectedGameId: null,
  selectedGame: null,
  statistics: null,
  steamMovesCount: 0,
  totalMovements: 0,
  filters: {
    movementType: 'steam',
    hoursBack: 4,
    limit: 20
  },
  loading: false,
  error: null,
  lastFetched: null
};
 
// Async thunks
export const fetchLiveMovements = createAsyncThunk(
  'movements/fetchLive',
  async (
    { limit = 20, hoursBack = 4, movementType = 'steam' }: Partial<MovementFilters>,
    { rejectWithValue }
  ) => {
    try {
      const data = await lineMovementService.getLiveMovements(limit, hoursBack, movementType);
      return data;
    } catch (error: any) {
      return rejectWithValue(
        error.response?.data?.error || 'Failed to fetch live movements'
      );
    }
  }
);
 
export const fetchGameMovements = createAsyncThunk(
  'movements/fetchGameMovements',
  async (gameId: string, { rejectWithValue }) => {
    try {
      const data = await lineMovementService.getGameMovements(gameId);
      return data;
    } catch (error: any) {
      return rejectWithValue(
        error.response?.data?.error || 'Failed to fetch game movements'
      );
    }
  }
);
 
export const fetchMovementStats = createAsyncThunk(
  'movements/fetchStats',
  async (hoursBack: number = 24, { rejectWithValue }) => {
    try {
      const data = await lineMovementService.getMovementStats(hoursBack);
      return data;
    } catch (error: any) {
      return rejectWithValue(
        error.response?.data?.error || 'Failed to fetch movement statistics'
      );
    }
  }
);
 
export const fetchSteamMoves = createAsyncThunk(
  'movements/fetchSteamMoves',
  async (limit: number = 20, { rejectWithValue }) => {
    try {
      const data = await lineMovementService.getSteamMoves(limit);
      return data;
    } catch (error: any) {
      return rejectWithValue(
        error.response?.data?.error || 'Failed to fetch steam moves'
      );
    }
  }
);
 
const movementSlice = createSlice({
  name: 'movements',
  initialState,
  reducers: {
    setFilters(state, action: PayloadAction<Partial<MovementFilters>>) {
      state.filters = { ...state.filters, ...action.payload };
    },
    clearGameMovements(state) {
      state.selectedGameMovements = null;
      state.selectedGameId = null;
      state.selectedGame = null;
    },
    clearError(state) {
      state.error = null;
    }
  },
  extraReducers: (builder) => {
    // Fetch Live Movements
    builder
      .addCase(fetchLiveMovements.pending, (state) => {
        state.loading = true;
        state.error = null;
      })
      .addCase(fetchLiveMovements.fulfilled, (state, action) => {
        state.liveMovements = action.payload.movements;
        state.loading = false;
        state.lastFetched = new Date().toISOString();
      })
      .addCase(fetchLiveMovements.rejected, (state, action) => {
        state.loading = false;
        state.error = action.payload as string;
      });
 
    // Fetch Game Movements
    builder
      .addCase(fetchGameMovements.pending, (state) => {
        state.loading = true;
        state.error = null;
      })
      .addCase(fetchGameMovements.fulfilled, (state, action) => {
        state.selectedGameMovements = action.payload.movements;
        state.selectedGame = action.payload.game;
        state.loading = false;
      })
      .addCase(fetchGameMovements.rejected, (state, action) => {
        state.loading = false;
        state.error = action.payload as string;
      });
 
    // Fetch Movement Stats
    builder
      .addCase(fetchMovementStats.pending, (state) => {
        state.loading = true;
        state.error = null;
      })
      .addCase(fetchMovementStats.fulfilled, (state, action) => {
        state.statistics = action.payload.statistics;
        state.steamMovesCount = action.payload.steamMovesCount;
        state.totalMovements = action.payload.totalMovements;
        state.loading = false;
      })
      .addCase(fetchMovementStats.rejected, (state, action) => {
        state.loading = false;
        state.error = action.payload as string;
      });
 
    // Fetch Steam Moves
    builder
      .addCase(fetchSteamMoves.pending, (state) => {
        state.loading = true;
        state.error = null;
      })
      .addCase(fetchSteamMoves.fulfilled, (state, action) => {
        state.steamMoves = action.payload;
        state.loading = false;
      })
      .addCase(fetchSteamMoves.rejected, (state, action) => {
        state.loading = false;
        state.error = action.payload as string;
      });
  }
});
 
export const { setFilters, clearGameMovements, clearError } = movementSlice.actions;
 
// Selectors
export const selectLiveMovements = (state: RootState) => state.movements.liveMovements;
export const selectSteamMoves = (state: RootState) => state.movements.steamMoves;
export const selectGameMovements = (state: RootState) => state.movements.selectedGameMovements;
export const selectMovementStats = (state: RootState) => state.movements.statistics;
export const selectMovementLoading = (state: RootState) => state.movements.loading;
export const selectMovementError = (state: RootState) => state.movements.error;
export const selectSteamMovesCount = (state: RootState) => state.movements.steamMovesCount;
 
export default movementSlice.reducer;