• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / gui / 1081664682

22 Nov 2023 02:11PM UTC coverage: 82.798% (-17.2%) from 99.964%
1081664682

Pull #4214

gitlab-ci

tranchitella
fix: Fixed the infinite page redirects when the back button is pressed

Remove the location and navigate from the useLocationParams.setValue callback
dependencies as they change the set function that is presented in other
useEffect dependencies. This happens when the back button is clicked, which
leads to the location changing infinitely.

Changelog: Title
Ticket: MEN-6847
Ticket: MEN-6796

Signed-off-by: Ihor Aleksandrychiev <ihor.aleksandrychiev@northern.tech>
Signed-off-by: Fabio Tranchitella <fabio.tranchitella@northern.tech>
Pull Request #4214: fix: Fixed the infinite page redirects when the back button is pressed

4319 of 6292 branches covered (0.0%)

8332 of 10063 relevant lines covered (82.8%)

191.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

5.71
/src/js/components/dashboard/widgets/mapwidget.js
1
// Copyright 2023 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14
import React, { useEffect, useMemo, useState } from 'react';
15

16
import { MenuItem, Select } from '@mui/material';
17

18
import Leaflet from 'leaflet';
19

20
import { geoAttributes } from '../../../actions/deviceActions';
21
import { MAX_PAGE_SIZE } from '../../../api/general-api';
22
import { ALL_DEVICES } from '../../../constants/deviceConstants';
23
import Map from './map';
24

25
const fallbackCenter = [59.913333, 10.738889];
4✔
26

27
const MapWrapper = ({ groups, groupNames, devicesById, getGroupDevices, getDevicesInBounds }) => {
4✔
28
  const [group, setGroup] = useState(ALL_DEVICES);
×
29
  const [bounds, setBounds] = useState();
×
30
  const [items, setItems] = useState([]);
×
31

32
  useEffect(() => {
×
33
    if (groupNames.length && !groupNames.includes(ALL_DEVICES)) {
×
34
      setGroup(groupNames[0]);
×
35
    }
36
    // eslint-disable-next-line react-hooks/exhaustive-deps
37
  }, [groupNames.join('')]);
38

39
  useEffect(() => {
×
40
    if (!bounds) {
×
41
      getGroupDevices(group, { perPage: MAX_PAGE_SIZE, selectedAttributes: geoAttributes });
×
42
      return;
×
43
    }
44
    getDevicesInBounds(bounds, group);
×
45
    // eslint-disable-next-line react-hooks/exhaustive-deps
46
  }, [getDevicesInBounds, getGroupDevices, group, JSON.stringify(bounds)]);
47

48
  const deviceIds = group !== ALL_DEVICES ? groups[group].deviceIds : Object.keys(devicesById);
×
49
  useEffect(() => {
×
50
    const items = deviceIds.reduce((accu, id) => {
×
51
      const device = devicesById[id];
×
52
      const { attributes = {} } = device;
×
53
      if (attributes['geo-lat']) {
×
54
        accu.push({ device, position: [attributes['geo-lat'], attributes['geo-lon']] });
×
55
      }
56
      return accu;
×
57
    }, []);
58
    setItems(items);
×
59
    // eslint-disable-next-line react-hooks/exhaustive-deps
60
  }, [deviceIds.join('')]);
61

62
  const boundaries = useMemo(() => {
×
63
    if (!items.length) {
×
64
      return;
×
65
    }
66
    const boundaries = items.reduce((accu, { position }) => {
×
67
      accu.push(position);
×
68
      return accu;
×
69
    }, []);
70
    return Leaflet.latLngBounds(boundaries);
×
71
    // eslint-disable-next-line react-hooks/exhaustive-deps
72
  }, [JSON.stringify(items)]);
73

74
  const onGroupSelect = ({ target: { value } }) => {
×
75
    setBounds();
×
76
    setGroup(value);
×
77
  };
78

79
  const mapProps = boundaries ? { bounds: boundaries } : { center: fallbackCenter, zoom: 10 };
×
80

81
  return (
×
82
    <span>
83
      <div className="flexbox center-aligned space-between">
84
        <h4>Device locations</h4>
85
        <Select labelId="group-selection-label" onChange={onGroupSelect} value={group}>
86
          {groupNames.map(item => (
87
            <MenuItem key={item} value={item}>
×
88
              {item}
89
            </MenuItem>
90
          ))}
91
        </Select>
92
      </div>
93
      <Map items={items} mapProps={mapProps} onMapMoved={setBounds} />
94
    </span>
95
  );
96
};
97

98
export default MapWrapper;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc