Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9736ed9

Browse files
committed
Add flag to createAction function to generate three pronged request action creators
1 parentb725bf3 commit9736ed9

File tree

2 files changed

+49
-12
lines changed

2 files changed

+49
-12
lines changed

‎src/create-action.js‎

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//@flow
2+
importcreateRequestTypesfrom'./create-request-types'
23

34
typeAction={
45
type:string,
@@ -9,22 +10,35 @@ type Action = {
910
/**
1011
* Utility function to build action action creator
1112
*@param {String} type action creator type
13+
*@param {boolean} async flag indicating if action is of async type
1214
*@return {Function}
1315
*/
14-
constcreateAction=(type:string):Function=>(payload: ?any,meta: ?any):Object=>{
15-
constaction:Action={
16-
type
17-
}
16+
constcreateAction=(type:string,async:boolean=false):Function=>{
17+
constactionCreator=(payload: ?any,meta: ?any):Object=>{
18+
constaction:Action={
19+
type
20+
}
1821

19-
if(payload!==undefined){
20-
action.payload=payload
21-
}
22+
if(payload!==undefined){
23+
action.payload=payload
24+
}
25+
26+
if(meta!==undefined){
27+
action.meta=meta
28+
}
2229

23-
if(meta!==undefined){
24-
action.meta=meta
30+
returnaction
2531
}
2632

27-
returnaction
33+
if(!async){returnactionCreator}
34+
35+
constasyncTypes=createRequestTypes(type)
36+
37+
actionCreator.start=createAction(asyncTypes.START)
38+
actionCreator.success=createAction(asyncTypes.SUCCESS)
39+
actionCreator.failure=createAction(asyncTypes.FAILURE)
40+
41+
returnactionCreator
2842
}
2943

3044
exportdefaultcreateAction

‎tests/create-action.spec.js‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
importtestfrom'tape'
2-
importcreateActionfrom'../lib/create-action'
2+
importcreateActionfrom'../src/create-action'
33

44
test('createAction',t=>{
55
t.plan(7)
66

77
letpayload=null
88
letmeta=null
99
consttype='TEST_ACTION'
10-
constaction=createAction('TEST_ACTION')
10+
constaction=createAction(type)
1111

1212
t.deepEqual(action(),{
1313
type
@@ -49,3 +49,26 @@ test('createAction', t => {
4949
meta
5050
})
5151
})
52+
53+
test('createAction async',t=>{
54+
t.plan(4)
55+
56+
consttype='TEST_ASYNC_ACTION'
57+
constasyncAction=createAction(type,true)
58+
59+
t.deepEqual(asyncAction(),{
60+
type
61+
})
62+
63+
t.deepEqual(asyncAction.start(),{
64+
type:`${type}_START`
65+
})
66+
67+
t.deepEqual(asyncAction.success(),{
68+
type:`${type}_SUCCESS`
69+
})
70+
71+
t.deepEqual(asyncAction.failure(),{
72+
type:`${type}_FAILURE`
73+
})
74+
})

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp