feat: restrict date navigation within shared link range
This commit is contained in:
@@ -274,7 +274,7 @@ function Map({end, duration, slider, mapState, setMapState, setSubmenu, showDire
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function Menu({duration, setDuration, end, setEnd, slider, setSlider, submenu, setSubmenu, showDirection, setShowDirection, setMapState}) {
|
function Menu({duration, setDuration, end, setEnd, slider, setSlider, submenu, setSubmenu, showDirection, setShowDirection, setMapState, shareStart, shareEnd}) {
|
||||||
const [showRange, setShowRange] = useState(false);
|
const [showRange, setShowRange] = useState(false);
|
||||||
|
|
||||||
const chooseDuration = (x) => {
|
const chooseDuration = (x) => {
|
||||||
@@ -338,6 +338,22 @@ function Menu({duration, setDuration, end, setEnd, slider, setSlider, submenu, s
|
|||||||
const range = parseSlider(end, duration, slider);
|
const range = parseSlider(end, duration, slider);
|
||||||
const startDate = moment(end).subtract(...duration.delta);
|
const startDate = moment(end).subtract(...duration.delta);
|
||||||
|
|
||||||
|
const isPrevDisabled = shareStart ? moment(startDate).subtract(...duration.delta).isBefore(shareStart) : false;
|
||||||
|
const isNextDisabled = shareEnd ? moment(end).add(...duration.delta).isAfter(shareEnd) : false;
|
||||||
|
|
||||||
|
const isValidDate = (current) => {
|
||||||
|
if (!shareStart || !shareEnd) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
const proposedStartDate = moment(current).startOf('day');
|
||||||
|
const proposedEndDate = moment(proposedStartDate).add(...duration.delta);
|
||||||
|
|
||||||
|
const isAfterOrOnShareStart = proposedStartDate.isSameOrAfter(shareStart, 'day');
|
||||||
|
const isBeforeOrOnShareEnd = proposedEndDate.isSameOrBefore(shareEnd, 'day');
|
||||||
|
|
||||||
|
return isAfterOrOnShareStart && isBeforeOrOnShareEnd;
|
||||||
|
};
|
||||||
|
|
||||||
const shareRange = async () => {
|
const shareRange = async () => {
|
||||||
const shareStart = moment(range[0]).unix();
|
const shareStart = moment(range[0]).unix();
|
||||||
const shareEnd = moment(range[1]).unix();
|
const shareEnd = moment(range[1]).unix();
|
||||||
@@ -442,6 +458,7 @@ function Menu({duration, setDuration, end, setEnd, slider, setSlider, submenu, s
|
|||||||
timeFormat={false}
|
timeFormat={false}
|
||||||
onChange={(x) => chooseEnd(x)}
|
onChange={(x) => chooseEnd(x)}
|
||||||
value={startDate}
|
value={startDate}
|
||||||
|
isValidDate={isValidDate}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -496,7 +513,7 @@ function Menu({duration, setDuration, end, setEnd, slider, setSlider, submenu, s
|
|||||||
}
|
}
|
||||||
|
|
||||||
<div className='menu-container'>
|
<div className='menu-container'>
|
||||||
<button onClick={() => prev()}><</button>
|
<button onClick={() => prev()} disabled={isPrevDisabled}><</button>
|
||||||
|
|
||||||
<button
|
<button
|
||||||
onClick={() => setSubmenu('end')}
|
onClick={() => setSubmenu('end')}
|
||||||
@@ -519,7 +536,7 @@ function Menu({duration, setDuration, end, setEnd, slider, setSlider, submenu, s
|
|||||||
{(duration.shortLen || duration.len)} / {duration.win}
|
{(duration.shortLen || duration.len)} / {duration.win}
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
<button onClick={() => next()}>></button>
|
<button onClick={() => next()} disabled={isNextDisabled}>></button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
@@ -534,6 +551,8 @@ function App() {
|
|||||||
const initialLng = params.get('lng');
|
const initialLng = params.get('lng');
|
||||||
const initialZoom = params.get('zoom');
|
const initialZoom = params.get('zoom');
|
||||||
const initialShowDirection = params.get('showDirection') === 'true';
|
const initialShowDirection = params.get('showDirection') === 'true';
|
||||||
|
const shareStartParam = params.get('shareStart');
|
||||||
|
const shareEndParam = params.get('shareEnd');
|
||||||
|
|
||||||
const initialDuration = (initialDurationId && durations[parseInt(initialDurationId, 10)]) ? durations[parseInt(initialDurationId, 10)] : durations[0];
|
const initialDuration = (initialDurationId && durations[parseInt(initialDurationId, 10)]) ? durations[parseInt(initialDurationId, 10)] : durations[0];
|
||||||
const initialEnd = initialEndTimestamp ? moment.unix(initialEndTimestamp) : moment();
|
const initialEnd = initialEndTimestamp ? moment.unix(initialEndTimestamp) : moment();
|
||||||
@@ -549,6 +568,9 @@ function App() {
|
|||||||
const [submenu, setSubmenu] = useState(false);
|
const [submenu, setSubmenu] = useState(false);
|
||||||
const [showDirection, setShowDirection] = useState(initialShowDirection);
|
const [showDirection, setShowDirection] = useState(initialShowDirection);
|
||||||
|
|
||||||
|
const shareStart = shareStartParam ? moment.unix(shareStartParam) : null;
|
||||||
|
const shareEnd = shareEndParam ? moment.unix(shareEndParam) : null;
|
||||||
|
|
||||||
const isInitialMount = useRef(true);
|
const isInitialMount = useRef(true);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isInitialMount.current) {
|
if (isInitialMount.current) {
|
||||||
@@ -595,6 +617,8 @@ function App() {
|
|||||||
showDirection={showDirection}
|
showDirection={showDirection}
|
||||||
setShowDirection={setShowDirection}
|
setShowDirection={setShowDirection}
|
||||||
setMapState={setMapState}
|
setMapState={setMapState}
|
||||||
|
shareStart={shareStart}
|
||||||
|
shareEnd={shareEnd}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<Map
|
<Map
|
||||||
|
|||||||
Reference in New Issue
Block a user