Skip to content

Commit 673ec55

Browse files
authored
FEATURE: Change expansion language globally (#477)
* FEATURE: Change expansion language globally * TASK: Add tests for global language change for expansions in settings * TASK: Set global language when all expansions have same language
1 parent ab5b2a4 commit 673ec55

File tree

16 files changed

+464
-30
lines changed

16 files changed

+464
-30
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { Cmd, getCmd, getModel } from 'redux-loop'
2+
import { set as setToDb, get as getFromDb } from 'idb-keyval'
3+
4+
import { State } from '../types'
5+
import { GLOBAL_LANGUAGE_DB_KEY } from '../constants'
6+
import { actions } from '../actions'
7+
8+
import { initialState, Reducer } from '../reducer'
9+
10+
const mockSelectedState: State = initialState
11+
12+
describe('Settings | Expansions | GlobalLanguage | reducer', () => {
13+
it('should return the initial state', () => {
14+
// @ts-ignore
15+
const result = Reducer(undefined, {})
16+
17+
expect(result).toEqual(initialState)
18+
})
19+
20+
it('should handle SELECT', () => {
21+
const result = Reducer(mockSelectedState, actions.select('PL'))
22+
23+
const expected = 'PL'
24+
25+
const model = getModel(result)
26+
const cmd = getCmd(result)
27+
28+
expect(model).toEqual(expected)
29+
30+
expect(cmd).toEqual(
31+
Cmd.run(setToDb, {
32+
args: [GLOBAL_LANGUAGE_DB_KEY, expected],
33+
successActionCreator: actions.setToDBSuccessful,
34+
failActionCreator: actions.setToDBFailed,
35+
})
36+
)
37+
})
38+
39+
it('should handle FETCH_FROM_DB', () => {
40+
const result = Reducer(initialState, actions.fetchFromDB())
41+
42+
const model = getModel(result)
43+
const cmd = getCmd(result)
44+
45+
expect(model).toEqual(initialState)
46+
47+
expect(cmd).toEqual(
48+
Cmd.run(getFromDb, {
49+
args: [GLOBAL_LANGUAGE_DB_KEY],
50+
successActionCreator: actions.fetchFromDBSuccessful,
51+
failActionCreator: actions.fetchFromDBFailed,
52+
})
53+
)
54+
})
55+
56+
it('should handle FETCH_FROM_DB_SUCCESS for defined state', () => {
57+
const result = Reducer(
58+
mockSelectedState,
59+
actions.fetchFromDBSuccessful(initialState)
60+
)
61+
62+
expect(getModel(result)).toEqual(mockSelectedState)
63+
})
64+
65+
it('should handle FETCH_FROM_DB_SUCCESS for undefined state', () => {
66+
const result = Reducer(
67+
initialState,
68+
// @ts-ignore
69+
actions.fetchFromDBSuccessful(undefined)
70+
)
71+
72+
expect(getModel(result)).toEqual(initialState)
73+
})
74+
75+
it.todo('should handle FETCH_FROM_DB_FAILURE')
76+
it.todo('should handle SET_TO_DB')
77+
it.todo('should handle SET_TO_DB_SUCCESS')
78+
it.todo('should handle SET_TO_DB_FAILURE')
79+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as types from 'aer-types/types'
2+
3+
import { SelectedGlobalLanguageStateSlice } from '../types'
4+
5+
import { initialState } from '../reducer'
6+
import { selectors } from '../selectors'
7+
8+
const mockLanguagesState: SelectedGlobalLanguageStateSlice = {
9+
Settings: {
10+
Expansions: {
11+
GlobalLanguage: initialState,
12+
},
13+
},
14+
}
15+
16+
describe('Settings | Expansions | GlobalLanguage | selectors', () => {
17+
test('getGlobalLanguageOfExpansions()', () => {
18+
expect(selectors.getGlobalLanguageOfExpansions(mockLanguagesState)).toEqual(
19+
initialState
20+
)
21+
})
22+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { createAction } from '@martin_hotell/rex-tils'
2+
3+
import { ActionTypes, State } from './types'
4+
import { GlobalLanguageKey } from '../types'
5+
6+
export const actions = {
7+
noOp: () => createAction('@@REDUX_LOOP/ENFORCE_DEFAULT_HANDLING'),
8+
select: (lang: GlobalLanguageKey) =>
9+
createAction(ActionTypes.SELECT, { lang }),
10+
setToDB: (state: State) => createAction(ActionTypes.SET_TO_DB, state),
11+
setToDBSuccessful: () => createAction(ActionTypes.SET_TO_DB_SUCCESS),
12+
setToDBFailed: (error: Object) =>
13+
createAction(ActionTypes.SET_TO_DB_FAILURE, error),
14+
fetchFromDB: () => createAction(ActionTypes.FETCH_FROM_DB),
15+
fetchFromDBSuccessful: (language: unknown) =>
16+
createAction(ActionTypes.FETCH_FROM_DB_SUCCESS, language),
17+
fetchFromDBFailed: (error: Object) =>
18+
createAction(ActionTypes.FETCH_FROM_DB_FAILURE, error),
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const GLOBAL_LANGUAGE_DB_KEY = 'global-language-1.8'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export { actions } from './actions'
2+
export { initialState, Reducer } from './reducer'
3+
export { selectors } from './selectors'
4+
export * from './types'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { LoopReducer } from 'redux-loop'
2+
3+
import { Action, ActionTypes, State } from './types'
4+
5+
import * as reducerHelpers from './reducerHelpers'
6+
7+
export const initialState: State = 'ENG'
8+
9+
export const Reducer: LoopReducer<State, Action> = (
10+
state: State = initialState,
11+
action: Action
12+
) => {
13+
switch (action.type) {
14+
case ActionTypes.SELECT: {
15+
return reducerHelpers.select(action)
16+
}
17+
18+
case ActionTypes.FETCH_FROM_DB: {
19+
return reducerHelpers.fetchFromDb(state)
20+
}
21+
22+
case ActionTypes.FETCH_FROM_DB_SUCCESS: {
23+
return reducerHelpers.fetchFromDbSuccess(state, action)
24+
}
25+
26+
case ActionTypes.FETCH_FROM_DB_FAILURE: {
27+
return state
28+
}
29+
30+
default: {
31+
return state
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { loop, Cmd } from 'redux-loop'
2+
import { get as getFromDb } from 'idb-keyval'
3+
4+
import { State } from '../types'
5+
import { GLOBAL_LANGUAGE_DB_KEY } from '../constants'
6+
import { initialState } from '../reducer'
7+
import { actions } from '../actions'
8+
9+
export const fetchFromDb = (state: State) => {
10+
return loop(
11+
state,
12+
Cmd.run(getFromDb, {
13+
args: [GLOBAL_LANGUAGE_DB_KEY],
14+
successActionCreator: actions.fetchFromDBSuccessful,
15+
failActionCreator: actions.fetchFromDBFailed,
16+
})
17+
)
18+
}
19+
20+
export const fetchFromDbSuccess = (
21+
_: State,
22+
action: ReturnType<typeof actions.fetchFromDBSuccessful>
23+
) => {
24+
if (typeof action.payload === 'string' && action.payload !== null) {
25+
return action.payload as State
26+
}
27+
return initialState
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './select'
2+
export * from './fetchFromDb'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Cmd, loop } from 'redux-loop'
2+
import { set as setToDb } from 'idb-keyval'
3+
4+
import { GLOBAL_LANGUAGE_DB_KEY } from '../constants'
5+
import { actions } from '../actions'
6+
7+
export const select = (action: ReturnType<typeof actions.select>) => {
8+
const { lang } = action.payload
9+
const newState = lang
10+
11+
return loop(
12+
newState,
13+
Cmd.run(setToDb, {
14+
args: [GLOBAL_LANGUAGE_DB_KEY, newState],
15+
successActionCreator: actions.setToDBSuccessful,
16+
failActionCreator: actions.setToDBFailed,
17+
})
18+
)
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { SelectedGlobalLanguageStateSlice } from './types'
2+
3+
const getGlobalLanguageOfExpansions = (
4+
state: SelectedGlobalLanguageStateSlice
5+
) => state.Settings.Expansions.GlobalLanguage
6+
7+
export const selectors = {
8+
getGlobalLanguageOfExpansions,
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { ActionsUnion } from '@martin_hotell/rex-tils'
2+
3+
import { actions } from './actions'
4+
import { GlobalLanguageKey } from '../types'
5+
6+
export type State = GlobalLanguageKey
7+
8+
export enum ActionTypes {
9+
SELECT = 'Settings/Expansions/GlobalLanguage/SELECT',
10+
SET_TO_DB = 'Settings/Expansions/GlobalLanguage/SET_TO_DB',
11+
SET_TO_DB_SUCCESS = 'Settings/Expansions/GlobalLanguage/SET_TO_DB_SUCCESS',
12+
SET_TO_DB_FAILURE = 'Settings/Expansions/GlobalLanguage/SET_TO_DB_FAILURE',
13+
FETCH_FROM_DB = 'Settings/Expansions/GlobalLanguage/FETCH_FROM_DB',
14+
FETCH_FROM_DB_SUCCESS = 'Settings/Expansions/GlobalLanguage/FETCH_FROM_DB_SUCCESS',
15+
FETCH_FROM_DB_FAILURE = 'Settings/Expansions/GlobalLanguage/FETCH_FROM_DB_FAILURE',
16+
}
17+
18+
export type Action = ActionsUnion<typeof actions>
19+
20+
export type SelectedGlobalLanguageStateSlice = {
21+
Settings: {
22+
Expansions: {
23+
GlobalLanguage: State
24+
}
25+
}
26+
}

src/Redux/Store/Settings/Expansions/index.ts

+7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import * as Treasures from './Treasures'
1010
import * as BasicNemesisCards from './BasicNemesisCards'
1111
import * as UpgradedBasicNemesisCards from './UpgradedBasicNemesisCards'
1212
import * as Languages from './Languages'
13+
import * as GlobalLanguage from './GlobalLanguage'
1314

1415
import * as topLevelSelectors from './selectors'
1516

@@ -20,6 +21,7 @@ import * as types from 'aer-types/types'
2021
///////////
2122

2223
export type State = {
24+
GlobalLanguage: GlobalLanguage.State
2325
Languages: Languages.State
2426
Expansions: Expansions.State
2527
Cards: Cards.State
@@ -31,6 +33,7 @@ export type State = {
3133
}
3234

3335
export const initialState: State = {
36+
GlobalLanguage: GlobalLanguage.initialState,
3437
Languages: Languages.initialState,
3538
Expansions: Expansions.initialState,
3639
Cards: Cards.initialState,
@@ -63,6 +66,7 @@ export const mainActions = {
6366
export type MainAction = ActionsUnion<typeof mainActions>
6467

6568
export type Action =
69+
| GlobalLanguage.Action
6670
| Languages.Action
6771
| Expansions.Action
6872
| Cards.Action
@@ -74,6 +78,7 @@ export type Action =
7478
| MainAction
7579

7680
export const actions = {
81+
GlobalLanguage: GlobalLanguage.actions,
7782
Languages: Languages.actions,
7883
Expansions: Expansions.actions,
7984
Cards: Cards.actions,
@@ -90,6 +95,7 @@ export const actions = {
9095
/////////////
9196

9297
export const Reducer = combineReducers({
98+
GlobalLanguage: GlobalLanguage.Reducer,
9399
Languages: Languages.Reducer,
94100
Expansions: Expansions.Reducer,
95101
Cards: Cards.Reducer,
@@ -131,6 +137,7 @@ const getAllContentOfExpansionSelected = createSelector(
131137
)
132138

133139
export const selectors = {
140+
GlobalLanguage: GlobalLanguage.selectors,
134141
Languages: Languages.selectors,
135142
Expansions: Expansions.selectors,
136143
Cards: Cards.selectors,

src/Redux/Store/Settings/Expansions/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ import AERData from 'aer-data/src/index'
22

33
export const LANGUAGE_KEYS = Object.keys(AERData.normalizedData)
44
export type LanguageKey = keyof typeof AERData.normalizedData
5+
6+
export type GlobalLanguageKey = LanguageKey | 'CUSTOM'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import styled from 'styled-components'
2+
3+
import { Select } from 'components/atoms/SelectField'
4+
5+
const Wrapper = styled('div')`
6+
${Select} {
7+
margin: 16px 0 0;
8+
}
9+
`
10+
11+
export default Wrapper

0 commit comments

Comments
 (0)