Skip to content

Commit 7dc3087

Browse files
Afani97copelco
andauthored
Update react-picker library to fix staging build (#188)
* use react-picker library to fix staging build * Update TableModal.js * Test deploy image build * install all dev reqs * install all reqs * fix precommit --------- Co-authored-by: Colin Copeland <copelco@caktusgroup.com>
1 parent bb638f1 commit 7dc3087

File tree

5 files changed

+90
-107
lines changed

5 files changed

+90
-107
lines changed

.github/workflows/test.yaml

+4-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
sudo apt update
3737
sudo apt install -y --no-install-recommends postgresql-client-14
3838
python -m pip install pip-tools
39-
pip-sync requirements/base/base.txt requirements/test/test.txt
39+
pip-sync requirements/base/base.txt requirements/dev/dev.txt requirements/test/test.txt
4040
- name: Create NC database
4141
run: |
4242
psql $DATABASE_URL -c 'CREATE DATABASE traffic_stops_nc;'
@@ -49,3 +49,6 @@ jobs:
4949
DJANGO_SETTINGS_MODULE: traffic_stops.settings.dev
5050
DATABASE_URL: postgres://postgres:postgres@localhost:5432/traffic_stops
5151
DATABASE_URL_NC: postgres://postgres:postgres@localhost:5432/traffic_stops_nc
52+
- name: Test deploy image build
53+
run: |
54+
inv image.build

frontend/package-lock.json

+32-67
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@
1717
"react": "^18.2.0",
1818
"react-content-loader": "^6.2.0",
1919
"react-csv": "^2.2.2",
20-
"react-datepicker": "^4.8.0",
20+
"react-datepicker": "^4.11.0",
2121
"react-dom": "^18.2.0",
2222
"react-helmet": "^6.1.0",
2323
"react-loadable": "^5.5.0",
24-
"react-month-picker": "^2.2.1",
2524
"react-router-dom": "^5.3.3",
2625
"react-scripts": "5.0.1",
2726
"react-table": "^7.8.0",

frontend/src/Components/Elements/Table/TableModal.js

+48-37
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import React, { useEffect, useRef, useState } from 'react';
1+
import React, { forwardRef, useEffect, useState } from 'react';
22
import ReactDOM from 'react-dom';
33
import { useTheme } from 'styled-components';
44
import * as S from './TableModal.styled';
5-
import Picker from 'react-month-picker';
6-
import 'react-month-picker/css/month-picker.css';
75

86
// Deps
97
import { CSVLink } from 'react-csv';
@@ -50,6 +48,7 @@ import axios from '../../../Services/Axios';
5048
import * as ChartHeaderStyles from '../../Charts/ChartSections/ChartHeader.styled';
5149
import range from 'lodash.range';
5250
import displayMissingPhrase from '../../../util/displayMissingData';
51+
import DatePicker from 'react-datepicker';
5352

5453
const mapDatasetToChartName = {
5554
STOPS: 'Traffic Stops By Percentage',
@@ -71,14 +70,6 @@ const mapDataSetToEnum = {
7170
LIKELIHOOD_OF_SEARCH,
7271
};
7372

74-
function MonthBox({ value, _onClick }) {
75-
return (
76-
<div className="box" onClick={_onClick}>
77-
<Button onClick={() => {}}>{value}</Button>
78-
</div>
79-
);
80-
}
81-
8273
function getRangeValues() {
8374
const today = new Date();
8475

@@ -105,8 +96,9 @@ function TableModal({ chartState, dataSet, columns, isOpen, closeModal }) {
10596
const [rangeValue, setRangeValue] = useState(getRangeValues);
10697
const [tableReloading, setReloading] = useState(false);
10798
const [showDateRangePicker, setShowDateRagePicker] = useState(false);
108-
const monthPickerRef = useRef(null);
10999
const tableChartState = chartState;
100+
const [startDate, setStartDate] = useState(new Date());
101+
const [endDate, setEndDate] = useState(new Date());
110102

111103
const _getEntityReference = () => {
112104
const agencyName = tableChartState.data[AGENCY_DETAILS].name;
@@ -144,8 +136,8 @@ function TableModal({ chartState, dataSet, columns, isOpen, closeModal }) {
144136
setReloading(true);
145137
const _fetchData = async () => {
146138
const getEndpoint = mapDatasetKeyToEndpoint(tableDS);
147-
const _from = `${rangeValue.from.year}-${rangeValue.from.month}-01`;
148-
const _to = `${rangeValue.to.year}-${rangeValue.to.month}-01`;
139+
const _from = `${rangeValue.from.year}-${rangeValue.from.month.toString().padStart(2, 0)}-01`;
140+
const _to = `${rangeValue.to.year}-${rangeValue.to.month.toString().padStart(2, 0)}-01`;
149141
const url = `${getEndpoint(agencyId)}?from=${_from}&to=${_to}`;
150142
yearRange = range(rangeValue.from.year, rangeValue.to.year + 1, 1);
151143
try {
@@ -506,16 +498,6 @@ function TableModal({ chartState, dataSet, columns, isOpen, closeModal }) {
506498
return '';
507499
};
508500

509-
const pickerLang = {
510-
months: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
511-
from: 'From',
512-
to: 'To',
513-
};
514-
const makeText = (m) => {
515-
if (m && m.year && m.month) return pickerLang.months[m.month - 1] + '. ' + m.year;
516-
return '?';
517-
};
518-
519501
const closeRangePicker = () => {
520502
setShowDateRagePicker(false);
521503
setRangeValue(getRangeValues);
@@ -526,6 +508,34 @@ function TableModal({ chartState, dataSet, columns, isOpen, closeModal }) {
526508
closeModal();
527509
};
528510

511+
const MonthPickerButton = forwardRef(({ value, onClick }, ref) => (
512+
<Button onClick={onClick} ref={ref}>
513+
{value}
514+
</Button>
515+
));
516+
517+
const onDateRangeChange = (dates) => {
518+
// eslint-disable-next-line prefer-const
519+
let [start, end] = dates;
520+
521+
setStartDate(start);
522+
523+
// Don't allow same month/year selected
524+
if (end && start.getDay() === end.getDay()) {
525+
end = new Date(end.setMonth(end.getMonth() + 1));
526+
} else {
527+
setEndDate(end);
528+
}
529+
530+
if (start && end) {
531+
// Update range value on when valid start/end dates are selected
532+
setRangeValue({
533+
from: { month: start.getMonth() + 1, year: start.getFullYear() },
534+
to: { month: end.getMonth() + 1, year: end.getFullYear() },
535+
});
536+
}
537+
};
538+
529539
return ReactDOM.createPortal(
530540
isOpen && (
531541
<>
@@ -581,20 +591,21 @@ function TableModal({ chartState, dataSet, columns, isOpen, closeModal }) {
581591
)}
582592
{showDateRangePicker && (
583593
<div style={{ display: 'flex', flexDirection: 'row', marginTop: '10' }}>
584-
<Picker
585-
className="MonthYearPicker"
586-
lang={pickerLang.months}
587-
ref={monthPickerRef}
588-
years={{ min: 2000 }}
589-
value={rangeValue}
590-
theme="light"
591-
onDismiss={(value) => setRangeValue(value)}
592-
>
593-
<MonthBox
594-
value={makeText(rangeValue.from) + ' ~ ' + makeText(rangeValue.to)}
595-
_onClick={() => monthPickerRef.current.show()}
594+
<div style={{ width: '200px' }}>
595+
<DatePicker
596+
selected={startDate}
597+
onChange={onDateRangeChange}
598+
startDate={startDate}
599+
endDate={endDate}
600+
maxDate={new Date()}
601+
dateFormat="MM/yyyy"
602+
showMonthYearPicker
603+
selectsRange
604+
customInput={<MonthPickerButton />}
605+
popperPlacement="bottom-end"
606+
monthClassName={() => 'fj-date-range-month'}
596607
/>
597-
</Picker>
608+
</div>
598609
<Button
599610
variant="positive"
600611
backgroundColor="white"

frontend/src/styles/StyledComponents/GlobalStyles.styled.js

+5
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,9 @@ export default createGlobalStyle`
2222
fieldset {
2323
border: none;
2424
}
25+
26+
.fj-date-range-month {
27+
font-size: 1.5rem;
28+
margin: 15px;
29+
}
2530
`;

0 commit comments

Comments
 (0)