forked from splendo/HeartstoneAssessment
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[splendo#9] Added redux folder, plus storeConfiguration file and acti…
…ons, reducers folders
- Loading branch information
1 parent
020fd1f
commit ffc3fd3
Showing
4 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |