From 0d09d870d9adf789232a5e6165f44c605839833a Mon Sep 17 00:00:00 2001 From: Joffrey BION Date: Fri, 3 May 2019 01:27:22 +0200 Subject: Convert reducers to typescript --- frontend/src/reducers.js | 25 ------------------- frontend/src/reducers.ts | 25 +++++++++++++++++++ frontend/src/redux/currentGame.js | 42 -------------------------------- frontend/src/redux/currentGame.ts | 41 +++++++++++++++++++++++++++++++ frontend/src/redux/games.js | 42 -------------------------------- frontend/src/redux/games.ts | 51 +++++++++++++++++++++++++++++++++++++++ frontend/src/redux/user.js | 37 ---------------------------- frontend/src/redux/user.ts | 43 +++++++++++++++++++++++++++++++++ 8 files changed, 160 insertions(+), 146 deletions(-) delete mode 100644 frontend/src/reducers.js create mode 100644 frontend/src/reducers.ts delete mode 100644 frontend/src/redux/currentGame.js create mode 100644 frontend/src/redux/currentGame.ts delete mode 100644 frontend/src/redux/games.js create mode 100644 frontend/src/redux/games.ts delete mode 100644 frontend/src/redux/user.js create mode 100644 frontend/src/redux/user.ts (limited to 'frontend') diff --git a/frontend/src/reducers.js b/frontend/src/reducers.js deleted file mode 100644 index 612bd0c5..00000000 --- a/frontend/src/reducers.js +++ /dev/null @@ -1,25 +0,0 @@ -// @flow -import { routerReducer } from 'react-router-redux'; -import { combineReducers } from 'redux'; -import type { ApiPlayer } from './api/model'; -import type { CurrentGameState } from './redux/currentGame'; -import { createCurrentGameReducer } from './redux/currentGame'; -import type { GamesState } from './redux/games'; -import { createGamesReducer } from './redux/games'; -import { currentUserReducer } from './redux/user'; - -export type GlobalState = { - currentGame: CurrentGameState; - currentUser: ApiPlayer; - games: GamesState; - routing: any; -} - -export function createReducer() { - return combineReducers({ - currentGame: createCurrentGameReducer(), - currentUser: currentUserReducer, - games: createGamesReducer(), - routing: routerReducer, - }); -} diff --git a/frontend/src/reducers.ts b/frontend/src/reducers.ts new file mode 100644 index 00000000..679bffe1 --- /dev/null +++ b/frontend/src/reducers.ts @@ -0,0 +1,25 @@ +// @flow +import { routerReducer } from 'react-router-redux'; +import { combineReducers } from 'redux'; +import { ApiPlayer } from './api/model'; +import { CurrentGameState } from './redux/currentGame'; +import { createCurrentGameReducer } from './redux/currentGame'; +import { GamesState } from './redux/games'; +import { createGamesReducer } from './redux/games'; +import { currentUserReducer } from './redux/user'; + +export type GlobalState = { + currentGame: CurrentGameState; + currentUser: ApiPlayer | null; + games: GamesState; + routing: any; +} + +export function createReducer() { + return combineReducers({ + currentGame: createCurrentGameReducer(), + currentUser: currentUserReducer, + games: createGamesReducer(), + routing: routerReducer, + }); +} diff --git a/frontend/src/redux/currentGame.js b/frontend/src/redux/currentGame.js deleted file mode 100644 index 21b7108e..00000000 --- a/frontend/src/redux/currentGame.js +++ /dev/null @@ -1,42 +0,0 @@ -// @flow -import { combineReducers } from 'redux'; -import type { ApiPlayerTurnInfo, ApiTable } from '../api/model'; -import type { GlobalState } from '../reducers'; -import type { Action } from './actions/all'; -import { TABLE_UPDATE_RECEIVED, TURN_INFO_RECEIVED } from './actions/game'; - -export type CurrentGameState = { - turnInfo: ApiPlayerTurnInfo | null; - table: ApiTable | null; -} - -export function createCurrentGameReducer() { - return combineReducers({ - turnInfo: turnInfoReducer, - table: tableUpdatesReducer, - }); -} - -const turnInfoReducer = (state: ApiPlayerTurnInfo | null = null, action: Action) => { - switch (action.type) { - case TURN_INFO_RECEIVED: - return action.turnInfo; - case TABLE_UPDATE_RECEIVED: - return null; - default: - return state; - } -}; - -const tableUpdatesReducer = (state: ApiTable | null = null, action: Action) => { - switch (action.type) { - case TURN_INFO_RECEIVED: - return action.turnInfo.table; - case TABLE_UPDATE_RECEIVED: - return action.table; - default: - return state; - } -}; - -export const getCurrentTurnInfo = (state: GlobalState): ApiPlayerTurnInfo => state.currentGame.turnInfo; diff --git a/frontend/src/redux/currentGame.ts b/frontend/src/redux/currentGame.ts new file mode 100644 index 00000000..76006677 --- /dev/null +++ b/frontend/src/redux/currentGame.ts @@ -0,0 +1,41 @@ +import { combineReducers } from 'redux'; +import { ApiPlayerTurnInfo, ApiTable } from '../api/model'; +import { GlobalState } from '../reducers'; +import { Action } from './actions/all'; +import { TABLE_UPDATE_RECEIVED, TURN_INFO_RECEIVED } from './actions/game'; + +export type CurrentGameState = { + turnInfo: ApiPlayerTurnInfo | null; + table: ApiTable | null; +} + +export function createCurrentGameReducer() { + return combineReducers({ + turnInfo: turnInfoReducer, + table: tableUpdatesReducer, + }); +} + +const turnInfoReducer = (state: ApiPlayerTurnInfo | null = null, action: Action) => { + switch (action.type) { + case TURN_INFO_RECEIVED: + return action.turnInfo; + case TABLE_UPDATE_RECEIVED: + return null; + default: + return state; + } +}; + +const tableUpdatesReducer = (state: ApiTable | null = null, action: Action) => { + switch (action.type) { + case TURN_INFO_RECEIVED: + return action.turnInfo.table; + case TABLE_UPDATE_RECEIVED: + return action.table; + default: + return state; + } +}; + +export const getCurrentTurnInfo = (state: GlobalState): ApiPlayerTurnInfo | null => state.currentGame.turnInfo; diff --git a/frontend/src/redux/games.js b/frontend/src/redux/games.js deleted file mode 100644 index b5b0c7fc..00000000 --- a/frontend/src/redux/games.js +++ /dev/null @@ -1,42 +0,0 @@ -// @flow -import { List, Map } from 'immutable'; -import { combineReducers } from 'redux'; -import type { ApiLobby } from '../api/model'; -import type { GlobalState } from '../reducers'; -import type { Action } from './actions/all'; -import { ENTER_LOBBY, UPDATE_GAMES } from './actions/lobby'; - -export type GamesState = { - all: Map, - current: string | void -}; - -export const createGamesReducer = () => { - return combineReducers({ - all: allGamesReducer, - current: currentGameIdReducer - }) -}; - -export const allGamesReducer = (state: Map = Map(), action: Action) => { - switch (action.type) { - case UPDATE_GAMES: - let newGames = {}; - action.games.forEach(g => newGames[g.id] = g); - return state.merge(Map(newGames)); - default: - return state; - } -}; - -export const currentGameIdReducer = (state: string | void = null, action: Action) => { - switch (action.type) { - case ENTER_LOBBY: - return `${action.gameId}`; - default: - return state; - } -}; - -export const getAllGames = (state: GlobalState): List => state.games.all.toList(); -export const getCurrentGame = (state: GlobalState): ApiLobby | null => state.games.all.get(state.games.current); diff --git a/frontend/src/redux/games.ts b/frontend/src/redux/games.ts new file mode 100644 index 00000000..5e21211a --- /dev/null +++ b/frontend/src/redux/games.ts @@ -0,0 +1,51 @@ +import { List, Map } from 'immutable'; +import { combineReducers } from 'redux'; +import { ApiLobby } from '../api/model'; +import { GlobalState } from '../reducers'; +import { Action } from './actions/all'; +import { ENTER_LOBBY, UPDATE_GAMES } from './actions/lobby'; + +export type GamesState = { + all: Map, + current: string | null +}; + +export const createGamesReducer = () => { + return combineReducers({ + all: allGamesReducer, + current: currentGameIdReducer + }) +}; + +export const allGamesReducer = (state: Map = Map(), action: Action) => { + switch (action.type) { + case UPDATE_GAMES: + const newGames = mapify(action.games); + return state.merge(newGames); + default: + return state; + } +}; + +function mapify(games: ApiLobby[]): Map { + let newGames: {[id:string]:ApiLobby} = {}; + games.forEach(g => newGames[`${g.id}`] = g); + return Map(newGames); +} + +export const currentGameIdReducer = (state: string | null = null, action: Action) => { + switch (action.type) { + case ENTER_LOBBY: + return `${action.gameId}`; + default: + return state; + } +}; + +export const getAllGames = (state: GlobalState): List => state.games.all.toList(); +export const getCurrentGame = (state: GlobalState): ApiLobby | null => { + if (state.games.current == null) { + return null; + } + return state.games.all.get(state.games.current) || null; +}; diff --git a/frontend/src/redux/user.js b/frontend/src/redux/user.js deleted file mode 100644 index 0876d4c2..00000000 --- a/frontend/src/redux/user.js +++ /dev/null @@ -1,37 +0,0 @@ -import { ApiPlayer } from '../api/model'; -import type { GlobalState } from '../reducers'; -import type { Action } from './actions/all'; -import { SET_CURRENT_PLAYER } from './actions/user'; -import { getCurrentGame } from './games'; - -export type User = { - username: string, - displayName: string, -} - -export const currentUserReducer = (state: ?User = null, action: Action) => { - switch (action.type) { - case SET_CURRENT_PLAYER: - return { - username: action.player.username, - displayName: action.player.displayName - }; - default: - return state; - } -}; - -export function getCurrentUser(state: GlobalState): ?User { - return state.currentUser -} - -export function getCurrentPlayer(state: GlobalState): ApiPlayer { - let game = getCurrentGame(state); - for (let i = 0; i < game.players.length; i++) { - let player = game.players[i]; - if (player.username === state.currentUser.username) { - return player; - } - } - return null; -} diff --git a/frontend/src/redux/user.ts b/frontend/src/redux/user.ts new file mode 100644 index 00000000..2cc25cc0 --- /dev/null +++ b/frontend/src/redux/user.ts @@ -0,0 +1,43 @@ +import { ApiPlayer } from '../api/model'; +import { GlobalState } from '../reducers'; +import { Action } from './actions/all'; +import { SET_CURRENT_PLAYER } from './actions/user'; +import { getCurrentGame } from './games'; + +export type User = { + username: string, + displayName: string, +} + +export const currentUserReducer = (state: User | null = null, action: Action) => { + switch (action.type) { + case SET_CURRENT_PLAYER: + return { + username: action.player.username, + displayName: action.player.displayName + }; + default: + return state; + } +}; + +export function getCurrentUser(state: GlobalState): User | null { + return state.currentUser +} + +export function getCurrentPlayer(state: GlobalState): ApiPlayer | null { + if (state.currentUser == null) { + return null; + } + let game = getCurrentGame(state); + if (game == null) { + return null; + } + for (let i = 0; i < game.players.length; i++) { + let player = game.players[i]; + if (player.username === state.currentUser.username) { + return player; + } + } + return null; +} -- cgit