-
Notifications
You must be signed in to change notification settings - Fork 4
/
CirculationEvents.tsx
188 lines (163 loc) · 4.99 KB
/
CirculationEvents.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import * as React from "react";
import { Store } from "@reduxjs/toolkit";
import { connect } from "react-redux";
import ActionCreator from "../actions";
import ErrorMessage from "./ErrorMessage";
import LoadingIndicator from "@thepalaceproject/web-opds-client/lib/components/LoadingIndicator";
import CatalogLink from "@thepalaceproject/web-opds-client/lib/components/CatalogLink";
import * as PropTypes from "prop-types";
import CirculationEventsDownloadForm from "./CirculationEventsDownloadForm";
import { CirculationEventData } from "../interfaces";
import { FetchErrorData } from "@thepalaceproject/web-opds-client/lib/interfaces";
import { RootState } from "../store";
import { Button } from "library-simplified-reusable-components";
export interface CirculationEventsStateProps {
events?: CirculationEventData[];
fetchError?: FetchErrorData;
isLoaded?: boolean;
}
export interface CirculationEventsDispatchProps {
fetchCirculationEvents?: () => Promise<any>;
}
export interface CirculationEventsOwnProps {
store?: Store<RootState>;
wait?: number;
library?: string;
}
export interface CirculationEventsProps
extends CirculationEventsStateProps,
CirculationEventsDispatchProps,
CirculationEventsOwnProps {}
export interface CirculationEventsState {
showDownloadForm: boolean;
}
/** Shows a continuously updating list of the most recent circulation events, and
a button to download a CSV of circulation events for a particular date. */
export class CirculationEvents extends React.Component<
CirculationEventsProps,
CirculationEventsState
> {
timer: any;
context: { showCircEventsDownload: boolean };
_isMounted: boolean;
constructor(props) {
super(props);
this.state = { showDownloadForm: false };
this.showDownloadForm = this.showDownloadForm.bind(this);
this.hideDownloadForm = this.hideDownloadForm.bind(this);
this.fetchAndQueue = this.fetchAndQueue.bind(this);
this._isMounted = false;
}
static contextTypes: React.ValidationMap<any> = {
showCircEventsDownload: PropTypes.bool,
};
render(): JSX.Element {
const { library, fetchError, isLoaded, events } = this.props;
return (
<div className="circulation-events">
<h2>Circulation Events</h2>
{this.context.showCircEventsDownload && (
<Button
callback={this.showDownloadForm}
content="Download CSV"
className="left-align"
/>
)}
<CirculationEventsDownloadForm
show={this.state.showDownloadForm}
hide={this.hideDownloadForm}
library={library}
/>
{fetchError && <ErrorMessage error={fetchError} />}
{!isLoaded && !library && <LoadingIndicator />}
{!library && (
<table className="table table-striped">
<thead>
<tr>
<th>Book</th>
<th>Event</th>
<th>Time</th>
</tr>
</thead>
<tbody>
{events.map((event) => (
<tr key={event.id}>
<td>
<CatalogLink bookUrl={event.book.url}>
{event.book.title}
</CatalogLink>
</td>
<td>{this.formatType(event.type)}</td>
<td>{this.formatTime(event.time)}</td>
</tr>
))}
</tbody>
</table>
)}
</div>
);
}
UNSAFE_componentWillMount() {
this._isMounted = true;
if (!this.props.library) {
this.fetchAndQueue();
}
}
componentWillUnmount() {
if (!this.props.library) {
clearTimeout(this.timer);
}
this._isMounted = false;
}
fetchAndQueue(): Promise<any> {
return this.props.fetchCirculationEvents().then(() => {
if (this._isMounted) {
this.timer = setTimeout(
this.fetchAndQueue,
(this.props.wait || 10) * 1000
);
}
});
}
formatType(str) {
return str.replace("_", " ");
}
formatTime(str) {
const date = new Date(str);
return date.toLocaleString("en-US", {
weekday: "short",
month: "short",
day: "numeric",
hour: "numeric",
minute: "numeric",
});
}
showDownloadForm() {
this.setState({ showDownloadForm: true });
}
hideDownloadForm() {
this.setState({ showDownloadForm: false });
}
}
function mapStateToProps(state, ownProps) {
return {
events: state.editor.circulationEvents.data || [],
fetchError: state.editor.circulationEvents.fetchError,
isLoaded: state.editor.circulationEvents.isLoaded,
};
}
function mapDispatchToProps(dispatch) {
const actions = new ActionCreator();
return {
fetchCirculationEvents: () => dispatch(actions.fetchCirculationEvents()),
};
}
const ConnectedCirculationEvents = connect<
CirculationEventsStateProps,
CirculationEventsDispatchProps,
CirculationEventsOwnProps
>(
mapStateToProps,
mapDispatchToProps
)(CirculationEvents);
export default ConnectedCirculationEvents;