Skip to content

Commit

Permalink
[splendo#15] Added cases for new Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
corrado4eyes committed Dec 6, 2019
1 parent 6ce5241 commit fe1d6e0
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions hearthstone/src/redux/reducers/cardReduces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,61 @@ import Card from '../../model/card';

export interface State {
cards: Card[]
filteredCards: Card[]
loading: boolean
error: string
cardSet: string,
filters: {
[key: string]: string
}
}

export const initialState = {
cards: [],
loading: false,
error: "",
cardSet: "Basic"
filters: {
cardSet: "Basic",
rarity: "Free",
},
filteredCards: []
}

export const reducer = (state: State = initialState, action: CardActionsType) => {
const stateCopy = Object.assign({}, state)
let filteredCards: Card[] = []
switch(action.type) {
case CardActions.onSyncCards:
return Object.assign({}, state, {
loading: true
});
case CardActions.onSyncCardsSucceed:
const cardInitialState = action.cards.filter((el: Card) => el.cardSet === state.filters['cardSet'])
return Object.assign({}, state, {
loading: false,
cards: action.cards
cards: action.cards,
filteredCards: cardInitialState
});
case CardActions.onSyncCardsFailed:
return Object.assign({}, state, {
loading: false,
error: action.error,
});
case CardActions.onSubmitFilter:
const newFilter = Object.assign({}, stateCopy.filters, {[action.filterKey]: action.filter});
filteredCards = stateCopy.cards.filter((el: Card) => (el as any)[action.filterKey] === newFilter[action.filterKey]);
return Object.assign({}, state, {
filteredCards: filteredCards,
filters: newFilter
});
case CardActions.onFilterByName:
filteredCards = stateCopy.cards.filter((el: Card) => {
if (el.name!.indexOf(action.name) >= 0) {
return el
}else{}
});
return Object.assign({}, state, {
filteredCards
});
default:
return Object.assign({}, state);
}
Expand Down

0 comments on commit fe1d6e0

Please sign in to comment.