-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.js
115 lines (103 loc) · 3.24 KB
/
template.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* $Verb $noun *****************************************************/
/*
0. Paste below into where your actions live
1. Import action constants into the reducer and add to the switch statement
import {
$NOUN_$VERB_INTENT,
$NOUN_$VERB_RECEIPT,
$NOUN_$VERB_ERROR,
} from '../actions'
2. Import $verb$Noun into the UI component
*/
export const $NOUN_$VERB_INTENT = '$NOUN_$VERB_INTENT';
export const $NOUN_$VERB_RECEIPT = '$NOUN_$VERB_RECEIPT';
export const $NOUN_$VERB_ERROR = '$NOUN_$VERB_ERROR';
/**
* The top-level asynchronous action creator.
* Dispatches intent, receipt, and error lifecycle events.
* @param {Object} data The input sent to the remote request
* @return {function} The thunk
*/
export function $verb$Noun($noun) {
return function(dispatch, getState) {
dispatch(intend$Verb$Noun());
return do$Verb$Noun($noun)
.then(function(receipt) {
console.log('$Verb $noun');
dispatch(received$Verb$Noun(receipt));
})
// See <http://stackoverflow.com/questions/34799677/orchestrating-multiple-actions>
// .then(dispatch(somethingElse(getState().otherProp))) // Dispatch subsequent actions here.
.catch(function(error){
console.error(error);
dispatch(error$Verb$Noun(error));
});
}
}
/**
* Perform the actual asynchronous work. There shouldn't be anything
* action-specific in here, just business logic.
* @param {?} $noun
* @return {Promise}
*/
function do$Verb$Noun($noun) {
return new Promise(function(resolve, reject) {
const xhr = new XMLHttpRequest();
xhr.open('POST', '/$noun');
xhr.onload = function() {
if(this.status < 300) {
resolve(JSON.parse(this.responseText));
} else if (this.status >= 300) {
const error = new Error(this.responseText);
error.httpStatus = this.statusText;
error.httpCode = this.status;
reject(error);
}
};
xhr.ontimeout =
xhr.onabort =
xhr.onerror = function(evt) {
reject(new Error('Network Error'));
};
xhr.send($noun);
});
}
/**
* Synchronous action declaring the intent to $verb a $noun. Use this action
* to indicate progress on completing the task as well, for example from a file
* upload XHR request.
* @param {number} progress = 0.0 An optional progress indicator from 0 to 1.0
* @return {Object} The intent action
*/
function intend$Verb$Noun($noun, progress = 0.0) {
return {
type: $NOUN_$VERB_INTENT,
$noun: $noun,
progress: progress
}
}
/**
* Synchronous action dispatched from the asynchronous `$verb$Noun` indicating
* that the remote service has successfully returned data.
* @param {Object} receipt The data returned from the service
* @return {Object} The receipt action
*/
function received$Verb$Noun(receipt) {
return {
type: $NOUN_$VERB_RECEIPT,
receipt: receipt
}
}
/**
* Synchronous action dispatched from the asynchronous `$verb$Noun` indicating
* that the remote service wasn’t able to complete because of an error.
* @param {Error} error An `Error` instance with custom properties
* indicating specifics of the failure
* @return {Object} The action
*/
function error$Verb$Noun(error) {
return {
type: $NOUN_$VERB_ERROR,
error: error
}
}