1
1
import { createSelector } from 'reselect' ;
2
2
3
+ /**
4
+ * @typedef {Object.<string, HttpErrorResponseModel> } IErrorState
5
+ */
6
+
7
+ /**
8
+ * ErrorSelector
9
+ *
10
+ * @static
11
+ */
3
12
export class ErrorSelector {
13
+ /**
14
+ * Creates a new object with the key being the finished action type
15
+ * (e.g. "SomeAction.REQUEST_*_FINISHED") and the value being the
16
+ * HttpErrorResponseModel.
17
+ *
18
+ * @param {IErrorState } errorState
19
+ * @param {string[] } actionTypes
20
+ * @returns {IErrorState }
21
+ * @static
22
+ */
4
23
static selectRawErrors ( errorState , actionTypes ) {
5
24
return actionTypes . reduce ( ( partialState , actionType ) => {
6
25
if ( errorState [ actionType ] ) {
@@ -11,12 +30,19 @@ export class ErrorSelector {
11
30
} , { } ) ;
12
31
}
13
32
33
+ /**
34
+ * Finds any errors matching the array of actionTypes and combines all error
35
+ * messages in to a single string.
36
+ *
37
+ * @param {IErrorState } errorState
38
+ * @param {string[] } actionTypes
39
+ * @returns {string }
40
+ * @static
41
+ */
14
42
static selectErrorText ( errorState , actionTypes ) {
15
- const partialErrorState = ErrorSelector . selectRawErrors ( errorState , actionTypes ) ;
16
-
17
43
const errorList = actionTypes . reduce ( ( errorMessages , actionType ) => {
18
- if ( partialErrorState [ actionType ] ) {
19
- const { message, errors } = partialErrorState [ actionType ] ;
44
+ if ( errorState [ actionType ] ) {
45
+ const { message, errors } = errorState [ actionType ] ;
20
46
const arrayOfErrors = errors . length ? errors : [ message ] ;
21
47
22
48
return errorMessages . concat ( arrayOfErrors ) ;
@@ -28,6 +54,14 @@ export class ErrorSelector {
28
54
return errorList . join ( ', ' ) ;
29
55
}
30
56
57
+ /**
58
+ * Returns true or false if there are errors found matching the array of actionTypes.
59
+ *
60
+ * @param {IErrorState } errorState
61
+ * @param {string[] } actionTypes
62
+ * @returns {boolean }
63
+ * @static
64
+ */
31
65
static hasErrors ( errorState , actionTypes ) {
32
66
return actionTypes . map ( ( actionType ) => errorState [ actionType ] ) . filter ( Boolean ) . length > 0 ;
33
67
}
0 commit comments