This repository was archived by the owner on Feb 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathReduxBlockUi.js
100 lines (84 loc) · 2.18 KB
/
ReduxBlockUi.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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import BlockUi from './BlockUi';
import { register, unregister } from './reduxMiddleware';
class ReduxBlockUi extends Component {
constructor(props) {
super(props);
this.middleware = this.middleware.bind(this);
this.blocking = 0;
this.state = {
blocking: 0,
};
}
componentWillMount() {
register(this.middleware);
}
componentWillUnmount() {
unregister(this.middleware);
}
middleware(action) {
const { block, unblock } = this.props;
this.checkAction(action, block);
this.checkAction(action, unblock, false);
}
checkAction(action, check, block = true) {
if (check) {
if (!Array.isArray(check)) {
check = [check];
}
check.forEach(value => {
let result;
if (typeof value === 'function') {
result = value(action);
} else if (value instanceof RegExp) {
value.lastIndex = 0;
result = value.test(action.type);
} else {
result = (value === action.type);
}
if (result === true) {
const oldValue = this.blocking;
if (block) {
this.setState({ blocking: ++this.blocking });
} else {
if (this.blocking < 1) {
this.blocking = 0;
} else {
--this.blocking;
}
this.setState({ blocking: this.blocking });
}
this.props.onChange && this.props.onChange(this.blocking, oldValue);
}
});
}
}
render() {
const {
blocking,
block: omit1,
unblock: omit2,
onChange: omit3,
...attributes
} = this.props;
return (<BlockUi {...attributes} blocking={blocking || (this.state.blocking > 0)} />);
}
}
ReduxBlockUi.propTypes = {
blocking: PropTypes.bool,
block: PropTypes.oneOfType([
PropTypes.instanceOf(RegExp),
PropTypes.string,
PropTypes.array,
PropTypes.func,
]),
unblock: PropTypes.oneOfType([
PropTypes.instanceOf(RegExp),
PropTypes.string,
PropTypes.array,
PropTypes.func,
]),
onChange: PropTypes.func,
};
export default ReduxBlockUi;