Skip to content
This repository was archived by the owner on Mar 3, 2020. It is now read-only.

Commit 278dba5

Browse files
committed
created sample redux module
1 parent e6994bb commit 278dba5

File tree

5 files changed

+97
-1
lines changed

5 files changed

+97
-1
lines changed

velog-frontend/.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ module.exports = {
3434
"jsx-a11y/no-autofocus": 0,
3535
"global-require": 0,
3636
"react/no-unused-state": 1,
37-
"dangerouslySetInnerHTML": 0
37+
"dangerouslySetInnerHTML": 0,
38+
"no-param-reassign": 0
3839
}
3940
}

velog-frontend/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
"fs-extra": "3.0.1",
2828
"hellojs": "next",
2929
"html-webpack-plugin": "2.29.0",
30+
"immer": "^1.1.3",
3031
"immutable": "^3.8.2",
3132
"include-media": "^1.4.9",
33+
"invariant": "^2.2.3",
3234
"jest": "20.0.4",
3335
"lodash": "^4.17.5",
3436
"marked": "^0.3.12",

velog-frontend/src/store/modules/base.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const BaseRecord = Record({
2828
fullscreenLoader: false,
2929
});
3030

31+
3132
const initialState: Map<string, *> = BaseRecord();
3233

3334
export default handleActions({
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// @flow
2+
import { createAction, handleActions, type ActionType } from 'redux-actions';
3+
import produce from 'immer';
4+
5+
/* ACTION TYPES */
6+
const PLAIN = 'sample/PLAIN';
7+
const INCREASE = 'sample/INCREASE';
8+
const INSERT = 'sample/INSERT';
9+
const CHANGE = 'sample/CHANGE';
10+
11+
/* ACTION PAYLOADS */
12+
type InsertPayload = {
13+
id: number,
14+
text: string
15+
}
16+
17+
/* ACTION CREATORS */
18+
const plain = createAction(PLAIN);
19+
const increase = createAction(INCREASE, (value: number) => value);
20+
const insert = createAction(INSERT, ({ id, text }: InsertPayload): InsertPayload => ({ id, text }));
21+
const change = createAction(CHANGE, (text: string) => text);
22+
23+
/* ACTION FLOW TYPES */
24+
type PlainAction = ActionType<typeof plain>;
25+
type IncreaseAction = ActionType<typeof increase>;
26+
type InsertAction = ActionType<typeof insert>;
27+
type ChangeAction = ActionType<typeof change>;
28+
29+
/* EXPORTING ACTION CREATORS / ACTION CREATORS TYPES */
30+
export type SampleActionCreators = {
31+
plain(): PlainAction,
32+
increase(value: number): IncreaseAction,
33+
insert(payload: InsertPayload): InsertAction,
34+
change(text: string): IncreaseAction
35+
};
36+
37+
export const actionCreators = {
38+
plain, increase, insert, change,
39+
};
40+
41+
/* State Types */
42+
type TodoItem = {
43+
id: number,
44+
text: string,
45+
done: boolean
46+
};
47+
48+
type State = {
49+
value: number,
50+
text: string,
51+
todos: TodoItem[],
52+
}
53+
54+
const initialState: State = {
55+
value: 0,
56+
text: '',
57+
todos: [],
58+
};
59+
60+
const reducer = handleActions({
61+
[PLAIN]: (state, action: PlainAction) => state,
62+
[INCREASE]: (state, action: IncreaseAction) => {
63+
return produce(state, (draft) => {
64+
if (!action || !draft) return;
65+
draft.value = action.payload;
66+
});
67+
},
68+
[INSERT]: (state, action: InsertAction) => {
69+
return produce(state, (draft) => {
70+
if (!action || !draft) return;
71+
draft.todos.push({ ...action.payload, done: false });
72+
});
73+
},
74+
[CHANGE]: (state, action: ChangeAction) => {
75+
return produce(state, (draft) => {
76+
if (!action || !draft) return;
77+
draft.text = action.payload;
78+
});
79+
},
80+
}, initialState);
81+
82+
export default reducer;

velog-frontend/yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3260,6 +3260,10 @@ ignore@^3.3.3:
32603260
version "3.3.7"
32613261
resolved "https://registry.yarnpkg.com/ignore/-/ignore-3.3.7.tgz#612289bfb3c220e186a58118618d5be8c1bab021"
32623262

3263+
immer@^1.1.3:
3264+
version "1.1.3"
3265+
resolved "https://registry.yarnpkg.com/immer/-/immer-1.1.3.tgz#9d5ac1daaeadd64c587d8dae3515fc1452ea0e1e"
3266+
32633267
immutable@^3.8.2:
32643268
version "3.8.2"
32653269
resolved "https://registry.yarnpkg.com/immutable/-/immutable-3.8.2.tgz#c2439951455bb39913daf281376f1530e104adf3"
@@ -3351,6 +3355,12 @@ invariant@^2.0.0, invariant@^2.2.1, invariant@^2.2.2:
33513355
dependencies:
33523356
loose-envify "^1.0.0"
33533357

3358+
invariant@^2.2.3:
3359+
version "2.2.3"
3360+
resolved "https://registry.yarnpkg.com/invariant/-/invariant-2.2.3.tgz#1a827dfde7dcbd7c323f0ca826be8fa7c5e9d688"
3361+
dependencies:
3362+
loose-envify "^1.0.0"
3363+
33543364
invert-kv@^1.0.0:
33553365
version "1.0.0"
33563366
resolved "https://registry.yarnpkg.com/invert-kv/-/invert-kv-1.0.0.tgz#104a8e4aaca6d3d8cd157a8ef8bfab2d7a3ffdb6"

0 commit comments

Comments
 (0)