Skip to content

Commit

Permalink
Merge pull request #4703 from wri/FLAG-343-fire-alert-widget-crashes-…
Browse files Browse the repository at this point in the history
…page-when-clicking-settings-button

[FLAG-343] fire alert widget crashes page when clicking settings button
  • Loading branch information
SARodrigues authored Nov 9, 2023
2 parents b8e4d93 + 1a4e29e commit 2daa452
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 28 deletions.
68 changes: 46 additions & 22 deletions components/ui/datepicker/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
import { Portal } from 'react-portal';
import OutsideClickHandler from 'react-outside-click-handler';
import moment from 'moment';
import Spinner from 'components/spinner';

import ReactDatePicker, {
registerLocale,
Expand All @@ -23,7 +24,14 @@ registerLocale('zh', zh);
registerLocale('pt_BR', ptBR);
registerLocale('id', id);

const Datepicker = ({ lang, selected, minDate, maxDate, ...props }) => {
const Datepicker = ({
lang,
loading = false,
selected,
minDate,
maxDate,
...props
}) => {
const [open, setOpen] = useState(false);
const inputEl = useRef();

Expand All @@ -37,6 +45,17 @@ const Datepicker = ({ lang, selected, minDate, maxDate, ...props }) => {
</button>
));

const LoadingInput = () => (
<button className="datepicker-input loading">
<Spinner
position="relative"
style={{
box: { width: 12, height: 12 },
}}
/>
</button>
);

const CalendarWrapper = ({ className, children }) => (
<OutsideClickHandler onOutsideClick={() => setOpen(false)}>
<CalendarContainer className={className}>{children}</CalendarContainer>
Expand Down Expand Up @@ -78,27 +97,31 @@ const Datepicker = ({ lang, selected, minDate, maxDate, ...props }) => {

return (
<div className="c-datepicker notranslate" ref={inputEl}>
<ReactDatePicker
open={open}
dateFormat="dd MMM yyyy"
onSelect={() => setOpen(false)}
customInput={<CustomInput />}
calendarClassName="datepicker-calendar"
renderCustomHeader={(headerProps) => (
<DatepickerHeader
{...headerProps}
minDate={minDateUTC}
maxDate={maxDateUTC}
/>
)}
popperContainer={PortalContainer}
locale={lang || 'en'}
calendarContainer={CalendarWrapper}
{...props}
selected={selectedUTC}
minDate={minDateUTC}
maxDate={maxDateUTC}
/>
{loading ? (
<LoadingInput />
) : (
<ReactDatePicker
open={open}
dateFormat="dd MMM yyyy"
onSelect={() => setOpen(false)}
customInput={<CustomInput />}
calendarClassName="datepicker-calendar"
renderCustomHeader={(headerProps) => (
<DatepickerHeader
{...headerProps}
minDate={minDateUTC}
maxDate={maxDateUTC}
/>
)}
popperContainer={PortalContainer}
locale={lang || 'en'}
calendarContainer={CalendarWrapper}
{...props}
selected={selectedUTC}
minDate={minDateUTC}
maxDate={maxDateUTC}
/>
)}
</div>
);
};
Expand All @@ -112,6 +135,7 @@ Datepicker.propTypes = {
selected: PropTypes.object,
minDate: PropTypes.object,
maxDate: PropTypes.object,
loading: PropTypes.bool,
};

export default Datepicker;
9 changes: 9 additions & 0 deletions components/ui/datepicker/datepicker-header/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,12 @@
}
}
}

.c-datepicker {
.datepicker-input {
&.loading {
width: 86px;
height: 1.3rem;
}
}
}
1 change: 1 addition & 0 deletions components/widget/component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ class Widget extends PureComponent {
<>
<WidgetHeader
widget={widget}
loading={loading}
title={title}
large={large}
datasets={datasets}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class WidgetSettingsButton extends PureComponent {
proxyOn,
embed,
active,
shouldSettingsOpen,
toggleSettingsMenu,
} = this.props;
const { tooltipOpen } = this.state;
Expand Down Expand Up @@ -105,7 +104,6 @@ class WidgetSettingsButton extends PureComponent {
proxyOn={proxyOn}
handleChangeSettings={handleChangeSettings}
handleShowInfo={handleShowInfo}
showYears={shouldSettingsOpen}
/>
)}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,21 @@ class WidgetSettings extends PureComponent {
/>
</div>
);
case 'datepicker':
case 'datepicker': {
const loadingDatepicker = !startValue || !minDate || !maxDate;

return (
<div className={cx('widget-settings-selector', type)}>
<div className={cx("widget-settings-selector", type)}>
<div className="datepicker-selector">
<div>
<span className="label">From</span>
<Datepicker
{...popperSettings}
loading={loadingDatepicker}
selected={new Date(startValue)}
onChange={(change) =>
propagateChange({
[startKey]: moment(change).format('YYYY-MM-DD'),
[startKey]: moment(change).format("YYYY-MM-DD"),
})}
minDate={new Date(minDate)}
maxDate={new Date(maxDate)}
Expand All @@ -139,10 +142,11 @@ class WidgetSettings extends PureComponent {
<span className="label">To</span>
<Datepicker
{...popperSettings}
loading={loadingDatepicker}
selected={new Date(endValue)}
onChange={(change) =>
propagateChange({
[endKey]: moment(change).format('YYYY-MM-DD'),
[endKey]: moment(change).format("YYYY-MM-DD"),
})}
minDate={new Date(minDate)}
maxDate={new Date(maxDate)}
Expand All @@ -153,6 +157,7 @@ class WidgetSettings extends PureComponent {
</div>
</div>
);
}
default:
return (
options &&
Expand Down

0 comments on commit 2daa452

Please sign in to comment.