-
Notifications
You must be signed in to change notification settings - Fork 481
/
application_loader.jsx
87 lines (75 loc) · 1.96 KB
/
application_loader.jsx
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
/**
* Loads an application from the API by a supplied applicationId
* Upon load it will call the supplied onApplicationLoaded callback if present, (e.g. for redirects), then
* renderApplication to render the loaded application data.
*/
import PropTypes from 'prop-types';
import React from 'react';
import Spinner from '../components/spinner';
import $ from 'jquery';
export default class ApplicationLoader extends React.Component {
static propTypes = {
applicationId: PropTypes.string.isRequired,
onApplicationLoaded: PropTypes.func,
renderApplication: PropTypes.func,
loadRawFormData: PropTypes.bool
};
state = {
loading: true
};
UNSAFE_componentWillMount() {
this.load();
}
componentWillUnmount() {
this.abortLoad();
}
abortLoad() {
if (this.loadRequest) {
this.loadRequest.abort();
this.loadRequest = null;
}
}
load = () => {
this.abortLoad();
let url = `/api/v1/pd/applications/${this.props.applicationId}`;
if (this.props.loadRawFormData) {
url += '?raw_form_data=1';
}
this.loadRequest = $.ajax({
method: 'GET',
url
})
.done(applicationData => {
this.setState({
applicationData,
loading: false
});
if (this.props.onApplicationLoaded) {
this.props.onApplicationLoaded(applicationData);
}
})
.fail(() => {
this.setState({
applicationData: null,
loading: false
});
});
};
handleUpdate = applicationData => {
this.setState({applicationData});
};
render() {
if (this.state.loading) {
return <Spinner />;
} else if (!this.state.applicationData) {
return <div>No application found.</div>;
} else if (this.props.renderApplication) {
return this.props.renderApplication({
applicationData: this.state.applicationData,
handleUpdate: this.handleUpdate
});
} else {
return null;
}
}
}