-
Notifications
You must be signed in to change notification settings - Fork 4
/
CirculationEventsDownloadForm.tsx
135 lines (122 loc) · 3.39 KB
/
CirculationEventsDownloadForm.tsx
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
import * as React from "react";
import { Modal } from "react-bootstrap";
import { Button, Form } from "library-simplified-reusable-components";
import EditableInput from "./EditableInput";
import * as qs from "qs";
export interface CirculationEventsDownloadFormProps {
show: boolean;
hide: () => void;
library?: string;
}
/** Form for downloading CirculationEvents for a particular date. */
export default class CirculationEventsDownloadForm extends React.Component<
CirculationEventsDownloadFormProps
> {
private dateStart = React.createRef<EditableInput>();
private dateEnd = React.createRef<EditableInput>();
constructor(props) {
super(props);
this.download = this.download.bind(this);
this.renderForm = this.renderForm.bind(this);
this.renderInputs = this.renderInputs.bind(this);
this.getRefValue = this.getRefValue.bind(this);
}
render(): JSX.Element {
return (
<Modal
className="circ-events-download-form"
show={this.props.show}
onHide={this.props.hide}
>
<Modal.Header>
<Modal.Title>Download CSV</Modal.Title>
</Modal.Header>
<Modal.Body>{this.renderForm()}</Modal.Body>
<Modal.Footer>
<Button
className="inverted left-align"
callback={this.props.hide}
content="Close"
/>
</Modal.Footer>
</Modal>
);
}
/**
* Renders the main <form> component
*/
renderForm() {
return (
<Form
onSubmit={this.download}
buttonContent="Download"
buttonClass="left-align"
content={this.renderInputs()}
/>
);
}
/**
* Render all the inputs for the form. The locations input is conditionally
* rendered if it is selected in the Event type dropdown.
*/
renderInputs() {
const today = new Date().toISOString().split("T")[0];
return (
<>
<fieldset>
<legend className="control-label visuallyHidden">
Configuration
</legend>
<EditableInput
ref={this.dateStart}
name="dateStart"
label="Start Date"
type="date"
value={today}
className="date-input"
optionalText={false}
/>
<EditableInput
ref={this.dateEnd}
name="dateEnd"
label="End Date"
type="date"
value={today}
className="date-input"
optionalText={false}
/>
</fieldset>
</>
);
}
/**
* Simple way to get the value of a EditableInput ref.
*/
getRefValue(ref: React.RefObject<EditableInput>) {
const current = ref.current || null;
return (current && current.getValue()) || null;
}
/**
* Send all the data and get back the report.
*/
download() {
const date = this.getRefValue(this.dateStart);
const dateEnd = this.getRefValue(this.dateEnd);
const paramValues = { date, dateEnd };
let library = "";
if (this.props.library) {
library = "/" + this.props.library;
}
let url = library + "/admin/bulk_circulation_events";
const params = qs.stringify(paramValues, { skipNulls: true });
if (params) {
url += "?" + params;
}
const link = document.createElement("a");
link.href = url;
link.target = "_blank";
document.body.appendChild(link);
link.click();
link.remove();
}
}