| 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167 |
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
3x
3x
3x
3x
3x
3x
3x
| import React, { PropTypes } from 'react';
import {SimpleListTable, ListPagination} from './ListComponents';
import {devOnly, shallowCopyExcept} from 'i-js-utils';
import {_buildElement} from './utils-internal';
function copyProps(props) {
return shallowCopyExcept({}, props, ['dataTransform', 'fetchDataCallback', 'onFetch', 'onSuccess', 'onError', 'renderRow', 'showPagination', 'loadingComponent', 'errorComponent', 'noDataComponent', 'headerAlwaysOn']);
}
export default class AjaxList extends React.Component {
static propTypes = {
id : React.PropTypes.string, // list id
fetchDataCallback : React.PropTypes.func.isRequired, // func that returns ajax promise.
dataTransform : React.PropTypes.func, // func that transforms data, function(data, response) --> transformed data
onFetch : React.PropTypes.func, // when data loading starts, function(event)
onError : React.PropTypes.func, // when data loading finishes with error, function(event)
onSuccess : React.PropTypes.func, // when data loading finishes with success, function(event)
errorComponent : React.PropTypes.oneOfType([ React.PropTypes.func, React.PropTypes.element ]), // component function or element
loadingComponent : React.PropTypes.oneOfType([ React.PropTypes.func, React.PropTypes.element ]), // component function or element
noDataComponent : React.PropTypes.oneOfType([ React.PropTypes.func, React.PropTypes.element ]), // component function or element
renderRow : React.PropTypes.func.isRequired,
showPagination : React.PropTypes.bool, // Default true
headerAlwaysOn : React.PropTypes.bool // if show header and footer when no data is available. Default: true
};
static defaultProps = {
id : "ajaxList-" + Math.floor(Math.random() * 1000000).toString(22),
showPagination : true,
headerAlwaysOn : true,
className : "ajaxList",
errorComponent : (<div className="center-block ajaxList-error">An error occurred.</div>),
loadingComponent : (<div className="center-block ajaxList-loader"></div>)
};
constructor(props) {
super(props);
this.props = props;
this.id = props.id || 'ajaxList-' + (Math.random() * 10000);
this._fetchData = this._fetchData.bind(this);
this._handlePageChange = this._handlePageChange.bind(this);
this.updateAndResetPage = this.updateAndResetPage.bind(this);
this.update = this.update.bind(this);
this.mounted = false;
this.currentPage = 1;
this.htmlProps = copyProps(props);
this.loadingElement = _buildElement(props.loadingComponent, this.htmlProps, []);
this.noDataElement = _buildElement(props.noDataComponent, this.htmlProps, []);
this.state = {"items":null,"paging":{"total":0,"page":1,"count":1}, error:null};
}
componentWillReceiveProps(newProps) {
this.htmlProps = copyProps(newProps);
if (this.props.loadingComponent != newProps.loadingComponent) {
this.loadingElement = _buildElement(newProps.loadingComponent, this.htmlProps, []);
}
if (this.props.noDataComponent != newProps.noDataComponent) {
this.noDataElement = _buildElement(newProps.noDataComponent, this.htmlProps, []);
}
this.props = newProps;
}
_checkData(data) {
if (Array.isArray(data) && this.props.showPagination && this.pagesCount == undefined) {
throw new Error('Got array of data and pagination was required but pagesCount is not set.');
}
}
_fetchData(page, withClear) {
this.currentPage = page;
Eif (this.props.onFetch) {
this.props.onFetch({ page : page });
}
Iif (withClear) {
this.setState({items: null, error:null});
} else {
this.setState({error:null});
}
let promise = this.props.fetchDataCallback(page);
Eif (promise) {
promise.then((resp) => {
let data = resp.data;
Iif (this.props.dataTransform) {
data = this.props.dataTransform(data, resp);
}
Eif (this.mounted) {
Iif (Array.isArray(data)) {
let page = this.state.paging.page + 1;
const np = {... this.state.paging};
np.page = page;
this.setState({items: data, paging: np});
} else {
this.setState(data);
}
}
Eif (this.props.onSuccess) {
this.props.onSuccess({ page : page, data : data });
}
},
(err) => {
devOnly(() => {console.log("AjaxList: fetch rejected: ", err);});
if (this.props.onError) {
this.props.onError(err);
}
if (this.mounted) {
const errCompProps = {...this.htmlProps, error: err};
const errorElement = _buildElement(this.props.errorComponent, errCompProps, []);
this.setState({error: errorElement});
}
});
} else {
this.setState({"items":[],"paging":{"total":0,"page":1,"count":1}}); // empty list
}
}
componentWillMount() {
this.mounted = true;
}
componentDidMount() {
this._fetchData(1, false);
}
componentWillUnmount() {
this.mounted = false;
}
_handlePageChange(pg) {
this._fetchData(pg, true);
}
updateAndResetPage() {
this._fetchData(1, true);
}
update() {
this._fetchData(this.currentPage, false);
}
render() {
const {paging, items, error} = this.state;
let noDataElem;
Iif (error) {
noDataElem = error;
} else {
noDataElem = items == null ? this.loadingElement : this.noDataElement;
}
const indexOffset = Math.max(0, paging.page -1) * paging.count;
return (
<div id={this.id} {...this.htmlProps}>
<SimpleListTable className={this.props.className} id={this.id} renderRow={this.props.renderRow} data={items} headerAlwaysOn={this.props.headerAlwaysOn} noDataElement={noDataElem} indexOffset={indexOffset}>
{this.props.children}
</SimpleListTable>
{(() => {
Eif (this.props.showPagination) {
return (<ListPagination className={this.props.className} onPageChanged={this._handlePageChange} id={this.id} total={paging.total} count={paging.count} page={paging.page}/>);
} else {
return null;
}
})()}
</div>
);
}
}
|