-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.jsx
79 lines (71 loc) · 2.71 KB
/
query.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
import { Meteor } from "meteor/meteor";
import React from "react";
import { withTracker } from "meteor/react-meteor-data";
import queryString from "query-string";
import { Breadcrumb } from "react-bootstrap";
import Moment from "moment-timezone";
import { AggregateLocations } from "../collections/aggregations";
import QueryDisplay from "./query_display";
import QuerySymbols from "./query_symbols";
import QueryLocations from "./query_locations";
import BreadcrumbBuilder from "./breadcrumb_builder";
import { locationSorter, testLocation } from "../utils/queryFunctions";
let completeQuery = {};
const Query = (props) => {
const { aggregateLocationsReady, aggregateLocations } = props;
if (!aggregateLocationsReady) {
return <div className="spinner" />;
}
const sortedLocations = locationSorter(aggregateLocations);
const qString = queryString.parse(location.search);
const { city, state, railroad, symbol, year } = qString;
if (city && state && railroad && symbol) {
// completeQuery is for sending into DB - not using raw query string in case errant values are present
completeQuery = { city, state, railroad, symbol };
let invalidDate = false;
if (year) {
invalidDate = !/^\d{4}$/.test(year);
let begin = Moment(year, "YYYY").startOf("year").toDate()
let end = Moment(year, "YYYY").endOf("year").toDate();
// throw dateTime in regardless so we know if we have to render date in breadcrumb
completeQuery.dateTime = { "$gte": begin, "$lte": end }
}
const locationsTested = testLocation(sortedLocations, city, state);
return (
<div className="text-center">
{completeQuery.dateTime ?
<BreadcrumbBuilder qs={qString} howComplete="dates" /> :
<BreadcrumbBuilder qs={qString} howComplete="symbol" />
}
{
invalidDate ? "Sorry, invalid year specified" :
(typeof locationsTested === "string") ? locationsTested : <QueryDisplay query={completeQuery} />
}
</div>
);
}
if (city && state) {
const locationsTested = testLocation(sortedLocations, city, state);
return (
<div className="text-center">
<BreadcrumbBuilder qs={qString} howComplete="city" />
{(typeof locationsTested === "string") ? locationsTested : <QuerySymbols city={city} state={state} />}
</div>
);
}
return (
<div className="text-center fadeIn">
<Breadcrumb>
<Breadcrumb.Item active>Search Home</Breadcrumb.Item>
</Breadcrumb>
<QueryLocations locations={sortedLocations} />
</div>
);
};
export default withTracker(() => {
const aggregateLocationsHandle = Meteor.subscribe("aggregateLocations", {});
return {
aggregateLocations: AggregateLocations.findFromPublication("aggregateLocations", {}).fetch(),
aggregateLocationsReady: aggregateLocationsHandle.ready()
};
})(Query);