diff --git a/hearthstone/src/redux/actions/cardActions.ts b/hearthstone/src/redux/actions/cardActions.ts new file mode 100644 index 00000000..6a434644 --- /dev/null +++ b/hearthstone/src/redux/actions/cardActions.ts @@ -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, getState: () => RootState, serviceFactory: ServiceFactory) => { + const cardService = serviceFactory.getCardService() + cardService.getAll() + .then((cards: Card[]) => { + dispatch(onSyncCardsSucceedConstructor(cards)) + }) + .catch((err: string) => { + dispatch(onSyncCardsFailedConstructor(err)) + }); + } +} \ No newline at end of file diff --git a/hearthstone/src/redux/reducers/cardReduces.ts b/hearthstone/src/redux/reducers/cardReduces.ts new file mode 100644 index 00000000..9368f584 --- /dev/null +++ b/hearthstone/src/redux/reducers/cardReduces.ts @@ -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); + } +} \ No newline at end of file diff --git a/hearthstone/src/redux/reducers/mainReducer.ts b/hearthstone/src/redux/reducers/mainReducer.ts new file mode 100644 index 00000000..92743a28 --- /dev/null +++ b/hearthstone/src/redux/reducers/mainReducer.ts @@ -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({card: fromCard.reducer}) \ No newline at end of file diff --git a/hearthstone/src/redux/store/storeConfig.ts b/hearthstone/src/redux/store/storeConfig.ts new file mode 100644 index 00000000..195dab20 --- /dev/null +++ b/hearthstone/src/redux/store/storeConfig.ts @@ -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; +} \ No newline at end of file