Skip to content
This repository was archived by the owner on Dec 9, 2021. It is now read-only.

Commit d41c3e8

Browse files
authored
Create ActionUnion (#41)
* create ActionUnion * refactor other actions to use ActionUnion * make ActionUnion names more specific
1 parent 24d7d39 commit d41c3e8

File tree

6 files changed

+48
-58
lines changed

6 files changed

+48
-58
lines changed

src/stores/meta/MetaAction.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import IAction from '../IAction';
22
import ITitleDescription from './models/ITitleDescription';
33

4+
export type MetaActionUnion = ITitleDescription;
5+
46
export default class MetaAction {
57

68
public static readonly SET_META: string = 'MetaAction.SET_META';

src/stores/meta/MetaReducer.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import MetaAction from './MetaAction';
1+
import MetaAction, {MetaActionUnion} from './MetaAction';
22
import IMetaReducerState from './IMetaReducerState';
33
import IAction from '../IAction';
44
import ITitleDescription from './models/ITitleDescription';
@@ -10,21 +10,19 @@ export default class MetaReducer {
1010
description: '',
1111
};
1212

13-
public static reducer(state: IMetaReducerState = MetaReducer._initialState, action: IAction<any>): IMetaReducerState {
13+
public static reducer(state: IMetaReducerState = MetaReducer._initialState, action: IAction<MetaActionUnion>): IMetaReducerState {
1414
switch (action.type) {
1515
case MetaAction.SET_META:
16-
return MetaReducer._setMeta(state, action);
16+
const model: ITitleDescription = action.payload as ITitleDescription;
17+
18+
return {
19+
...state,
20+
description: model.description || '',
21+
title: model.title,
22+
};
1723
default:
1824
return state;
1925
}
2026
}
2127

22-
private static _setMeta(state: IMetaReducerState, action: IAction<ITitleDescription>): IMetaReducerState {
23-
return {
24-
...state,
25-
description: action.payload.description || '',
26-
title: action.payload.title,
27-
};
28-
}
29-
3028
}

src/stores/modal/ModalAction.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import IAction from '../IAction';
22

3+
export type ModalActionUnion = void | JSX.Element;
4+
35
export default class ModalAction {
46

57
public static readonly ADD_MODAL: string = 'ModalAction.ADD_MODAL';

src/stores/modal/ModalReducer.ts

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import ModalAction from './ModalAction';
1+
import ModalAction, {ModalActionUnion} from './ModalAction';
22
import IModalReducerState from './IModalReducerState';
33
import IAction from '../IAction';
44

@@ -9,39 +9,33 @@ export default class ModalReducer {
99
modals: [],
1010
};
1111

12-
public static reducer(state: IModalReducerState = ModalReducer._initialState, action: IAction<any>): IModalReducerState {
12+
public static reducer(state: IModalReducerState = ModalReducer._initialState, action: IAction<ModalActionUnion>): IModalReducerState {
1313
switch (action.type) {
1414
case ModalAction.ADD_MODAL:
15-
return ModalReducer._addModal(state, action);
15+
const modal: JSX.Element = action.payload as JSX.Element;
16+
17+
return {
18+
...state,
19+
currentModal: modal,
20+
modals: [...state.modals, modal],
21+
};
1622
case ModalAction.REMOVE_MODAL:
17-
return ModalReducer._closeCurrentModal(state, action);
23+
const currentModal: JSX.Element = state.currentModal;
24+
const modalIndex: number = state.modals.indexOf(currentModal);
25+
const modals = [
26+
...state.modals.slice(0, modalIndex),
27+
...state.modals.slice(modalIndex + 1),
28+
];
29+
const previousModal: JSX.Element = modals[modals.length - 1];
30+
31+
return {
32+
...state,
33+
currentModal: previousModal || null,
34+
modals,
35+
};
1836
default:
1937
return state;
2038
}
2139
}
2240

23-
private static _addModal(state: IModalReducerState, action: IAction<JSX.Element>): IModalReducerState {
24-
return {
25-
...state,
26-
currentModal: action.payload,
27-
modals: [...state.modals, action.payload],
28-
};
29-
}
30-
31-
private static _closeCurrentModal(state: IModalReducerState, action: IAction<void>): IModalReducerState {
32-
const currentModal: JSX.Element = state.currentModal;
33-
const modalIndex: number = state.modals.indexOf(currentModal);
34-
const modals = [
35-
...state.modals.slice(0, modalIndex),
36-
...state.modals.slice(modalIndex + 1),
37-
];
38-
const previousModal: JSX.Element = modals[modals.length - 1];
39-
40-
return {
41-
...state,
42-
currentModal: previousModal || null,
43-
modals,
44-
};
45-
}
46-
4741
}

src/stores/user/UserAction.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import IAction from '../IAction';
22
import UserModel from './models/UserModel';
33

4+
export type UserActionUnion = void | UserModel;
5+
46
export default class UserAction {
57

68
public static readonly LOAD_USER: string = 'UserAction.LOAD_USER';

src/stores/user/UserReducer.ts

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import UserAction from './UserAction';
1+
import UserAction, {UserActionUnion} from './UserAction';
22
import IUserReducerState from './IUserReducerState';
33
import IAction from '../IAction';
44
import UserModel from './models/UserModel';
@@ -10,30 +10,22 @@ export default class UserReducer {
1010
isLoadingUser: false,
1111
};
1212

13-
public static reducer(state: IUserReducerState = UserReducer._initialState, action: IAction<any>): IUserReducerState {
13+
public static reducer(state: IUserReducerState = UserReducer._initialState, action: IAction<UserActionUnion>): IUserReducerState {
1414
switch (action.type) {
1515
case UserAction.LOAD_USER:
16-
return UserReducer._loadUser(state, action);
16+
return {
17+
...state,
18+
isLoadingUser: true,
19+
};
1720
case UserAction.LOAD_USER_SUCCESS:
18-
return UserReducer._loadUserSuccess(state, action);
21+
return {
22+
...state,
23+
isLoadingUser: false,
24+
currentUser: action.payload as UserModel,
25+
};
1926
default:
2027
return state;
2128
}
2229
}
2330

24-
private static _loadUser(state: IUserReducerState, action: IAction<UserModel>): IUserReducerState {
25-
return {
26-
...state,
27-
isLoadingUser: true,
28-
};
29-
}
30-
31-
private static _loadUserSuccess(state: IUserReducerState, action: IAction<UserModel>): IUserReducerState {
32-
return {
33-
...state,
34-
currentUser: action.payload,
35-
isLoadingUser: false,
36-
};
37-
}
38-
3931
}

0 commit comments

Comments
 (0)