Skip to content

Commit

Permalink
[splendo#9] Added redux folder, plus storeConfiguration file and acti…
Browse files Browse the repository at this point in the history
…ons, reducers folders
  • Loading branch information
corrado4eyes committed Dec 3, 2019
1 parent 020fd1f commit ffc3fd3
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 0 deletions.
64 changes: 64 additions & 0 deletions hearthstone/src/redux/actions/cardActions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Action, Dispatch } from "redux";
import Card from "../../model/card";
import { RootState } from "../reducers/mainReducer";
import { ServiceFactory } from "../../services/serviceFactory";

export enum CardActions {
// onStore = "onStore",
// onStoreSucceed = "onStoreSucced",
// onStoreFailed = "onStoreFailed"
onSyncCards = "onSyncCards",
onSyncCardsSucceed = "onSyncCardsSucceed",
onSyncCardsFailed = "onSyncCardsFailed"
}

export interface onSyncCardsAction extends Action {
type: CardActions.onSyncCards
}

export interface onSyncCardsSucceedAction extends Action {
type: CardActions.onSyncCardsSucceed
cards: Card[]
}

export interface onSyncCardsFailedAction extends Action {
type: CardActions.onSyncCardsFailed
error: any
}

export type CardActionsType = onSyncCardsAction |
onSyncCardsSucceedAction |
onSyncCardsFailedAction;

export const onSyncCardsConstructor = (): onSyncCardsAction => {
return {
type: CardActions.onSyncCards
}
}

export const onSyncCardsSucceedConstructor = (cards: Card[]): onSyncCardsSucceedAction => {
return {
type: CardActions.onSyncCardsSucceed,
cards
}
}

export const onSyncCardsFailedConstructor = (error: any): onSyncCardsFailedAction => {
return {
type: CardActions.onSyncCardsFailed,
error
}
}

export const dispatchSyncCard = (index: string | undefined = undefined) => {
return (dispatch: Dispatch<CardActionsType>, getState: () => RootState, serviceFactory: ServiceFactory) => {
const cardService = serviceFactory.getCardService()
cardService.getAll()
.then((cards: Card[]) => {
dispatch(onSyncCardsSucceedConstructor(cards))
})
.catch((err: string) => {
dispatch(onSyncCardsFailedConstructor(err))
});
}
}
37 changes: 37 additions & 0 deletions hearthstone/src/redux/reducers/cardReduces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {CardActionsType, CardActions} from '../actions/cardActions';
import Card from '../../model/card';

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

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

export const reducer = (state: State = initialState, action: CardActionsType) => {
switch(action.type) {
case CardActions.onSyncCards:
return Object.assign({}, state, {
loading: true
});
case CardActions.onSyncCardsSucceed:
return Object.assign({}, state, {
loading: false,
cards: action.cards
});
case CardActions.onSyncCardsFailed:
return Object.assign({}, state, {
loading: false,
error: action.error,
});
default:
return Object.assign({}, state);
}
}
11 changes: 11 additions & 0 deletions hearthstone/src/redux/reducers/mainReducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {combineReducers} from 'redux';
import * as fromCard from './cardReduces';
export interface RootState {
card: fromCard.State
}

export const initialState: RootState = {
card: fromCard.initialState
}

export const mainReducer = combineReducers<RootState>({card: fromCard.reducer})
13 changes: 13 additions & 0 deletions hearthstone/src/redux/store/storeConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {Container} from 'inversify';
import {applyMiddleware, createStore} from 'redux';
import {composeWithDevTools} from 'redux-devtools-extension';
import thunk from 'redux-thunk';
import ServiceFactory from '../../services/serviceFactory';
import {mainReducer} from '../reducers/mainReducer'

export const configureStore = (container: Container) => {
const serviceFactory = new ServiceFactory(container);
const middleware = applyMiddleware(thunk.withExtraArgument(serviceFactory));
const store = createStore(mainReducer, composeWithDevTools(middleware));
return store;
}

0 comments on commit ffc3fd3

Please sign in to comment.