• 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

69.44
/src/js/components/devices/device-details/devicetags.js
1
// Copyright 2021 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, useState } from 'react';
15
import { useDispatch } from 'react-redux';
16

17
import { Button } from '@mui/material';
18
import { useTheme } from '@mui/material/styles';
19

20
import { setDeviceTags } from '../../../actions/deviceActions';
21
import { toggle } from '../../../helpers';
22
import Tracking from '../../../tracking';
23
import ConfigurationObject from '../../common/configurationobject';
24
import { EditButton } from '../../common/confirm';
25
import KeyValueEditor from '../../common/forms/keyvalueeditor';
26
import { HELPTOOLTIPS, MenderHelpTooltip } from '../../helptips/helptooltips';
27
import DeviceDataCollapse from './devicedatacollapse';
28

29
const NameTipComponent = props => <MenderHelpTooltip id={HELPTOOLTIPS.nameTagTip.id} {...props} />;
10✔
30

31
const configHelpTipsMap = {
10✔
32
  name: { component: NameTipComponent, position: 'right' }
33
};
34

35
export const DeviceTags = ({ device, setSnackbar, userCapabilities }) => {
10✔
36
  const { canWriteDevices } = userCapabilities;
6✔
37
  const theme = useTheme();
6✔
38
  const [changedTags, setChangedTags] = useState({});
6✔
39
  const [editableTags, setEditableTags] = useState();
6✔
40
  const [isEditDisabled, setIsEditDisabled] = useState(!canWriteDevices);
6✔
41
  const [isEditing, setIsEditing] = useState(false);
6✔
42
  const [shouldUpdateEditor, setShouldUpdateEditor] = useState(false);
6✔
43
  const dispatch = useDispatch();
6✔
44

45
  const { tags = {} } = device;
6✔
46
  const hasTags = !!Object.keys(tags).length;
6✔
47

48
  useEffect(() => {
6✔
49
    setShouldUpdateEditor(toggle);
4✔
50
  }, [isEditing]);
51

52
  useEffect(() => {
6✔
53
    if (canWriteDevices) {
2!
54
      setIsEditing(!hasTags);
2✔
55
    }
56
  }, [hasTags, canWriteDevices]);
57

58
  const onCancel = () => {
6✔
59
    setIsEditing(false);
×
60
    setChangedTags(tags);
×
61
  };
62

63
  const onStartEdit = e => {
6✔
64
    e.stopPropagation();
×
65
    setEditableTags(tags);
×
66
    setChangedTags(tags);
×
67
    setIsEditing(true);
×
68
  };
69

70
  const onSubmit = () => {
6✔
71
    Tracking.event({ category: 'devices', action: 'modify_tags' });
×
72
    setIsEditDisabled(true);
×
73
    return dispatch(setDeviceTags(device.id, changedTags))
×
74
      .then(() => setIsEditing(false))
×
75
      .finally(() => setIsEditDisabled(false));
×
76
  };
77

78
  const helpTipsMap = Object.entries(configHelpTipsMap).reduce((accu, [key, value]) => {
6✔
79
    accu[key] = {
6✔
80
      ...value,
81
      props: { deviceId: device.id }
82
    };
83
    return accu;
6✔
84
  }, {});
85
  return (
6✔
86
    <DeviceDataCollapse
87
      title={
88
        <div className="two-columns">
89
          <div className="flexbox center-aligned">
90
            <h4 className="margin-right">Tags</h4>
91
            {!isEditing && canWriteDevices && <EditButton onClick={onStartEdit} />}
10✔
92
          </div>
93
        </div>
94
      }
95
    >
96
      <div className="relative" style={{ maxWidth: 700 }}>
97
        {isEditing ? (
6✔
98
          <>
99
            <KeyValueEditor
100
              disabled={isEditDisabled}
101
              errortext=""
102
              initialInput={editableTags}
103
              inputHelpTipsMap={helpTipsMap}
104
              onInputChange={setChangedTags}
105
              reset={shouldUpdateEditor}
106
            />
107
            <div className="flexbox center-aligned margin-bottom-small" style={{ justifyContent: 'flex-end' }}>
108
              <Button color="primary" onClick={onSubmit} variant="contained" style={{ marginRight: theme.spacing(2) }}>
109
                Save
110
              </Button>
111
              <Button onClick={onCancel}>Cancel</Button>
112
            </div>
113
          </>
114
        ) : (
115
          hasTags && <ConfigurationObject config={tags} setSnackbar={setSnackbar} />
2!
116
        )}
117
      </div>
118
    </DeviceDataCollapse>
119
  );
120
};
121

122
export default DeviceTags;
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