You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.2 KiB
42 lines
1.2 KiB
// redux/reducers/moveReducer.ts
|
|
import { CREATE_MOVE, DELETE_MOVE, GET_MOVES, UPDATE_MOVE } from '../constants';
|
|
import { Move } from "../../entities/Move";
|
|
|
|
export type MoveState = {
|
|
moves: Move[];
|
|
};
|
|
|
|
type MoveAction = {
|
|
type: string;
|
|
payload?: Move | Move[] | string;
|
|
};
|
|
|
|
const initialState: MoveState = {
|
|
moves: [],
|
|
}
|
|
|
|
export default function moveReducer(state = initialState, action: MoveAction): MoveState {
|
|
switch (action.type) {
|
|
case GET_MOVES:
|
|
return {
|
|
...state, moves: action.payload as Move[] || []
|
|
};
|
|
case CREATE_MOVE:
|
|
return {
|
|
...state, moves: [...state.moves, action.payload as Move]
|
|
};
|
|
case UPDATE_MOVE:
|
|
return {
|
|
...state,
|
|
moves: state.moves.map(move => move.id === (action.payload as Move).id ? action.payload as Move : move)
|
|
};
|
|
case DELETE_MOVE:
|
|
return {
|
|
...state,
|
|
moves: state.moves.filter(move => move.id !== action.payload)
|
|
};
|
|
default:
|
|
return state;
|
|
}
|
|
}
|