• 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

75.0
/src/js/components/common/forms/keyvalueeditor.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, { createRef, useEffect, useState } from 'react';
15

16
import { Clear as ClearIcon, Add as ContentAddIcon } from '@mui/icons-material';
17
import { Fab, FormControl, FormHelperText, IconButton, Input } from '@mui/material';
18
import { useTheme } from '@mui/material/styles';
19

20
const emptyInput = { helptip: null, key: '', value: '' };
17✔
21

22
export const KeyValueEditor = ({ disabled, errortext, initialInput = {}, inputHelpTipsMap = {}, onInputChange, reset }) => {
17✔
23
  const theme = useTheme();
181✔
24
  const [inputs, setInputs] = useState([{ ...emptyInput }]);
181✔
25
  const [error, setError] = useState('');
181✔
26

27
  useEffect(() => {
181✔
28
    const newInputs = Object.keys(initialInput).length
13!
29
      ? Object.entries(initialInput).map(([key, value]) => ({ helptip: inputHelpTipsMap[key.toLowerCase()], key, ref: createRef(), value }))
×
30
      : [{ ...emptyInput, ref: createRef() }];
31
    setInputs(newInputs);
13✔
32
    // eslint-disable-next-line react-hooks/exhaustive-deps
33
  }, [JSON.stringify(initialInput), JSON.stringify(inputHelpTipsMap), reset]);
34

35
  const onClearClick = () => {
181✔
36
    const changedInputs = [{ ...emptyInput }];
×
37
    setInputs(changedInputs);
×
38
    const inputObject = reducePairs(changedInputs);
×
39
    onInputChange(inputObject);
×
40
  };
41

42
  const updateInputs = (key, index, event) => {
181✔
43
    let changedInputs = [...inputs];
142✔
44
    const {
45
      target: { value }
46
    } = event;
142✔
47
    changedInputs[index][key] = value;
142✔
48
    changedInputs[index].helptip = null;
142✔
49
    const normalizedKey = changedInputs[index].key.toLowerCase();
142✔
50
    if (inputHelpTipsMap[normalizedKey]) {
142✔
51
      changedInputs[index].helptip = inputHelpTipsMap[normalizedKey];
1✔
52
    }
53
    setInputs(changedInputs);
142✔
54
    const inputObject = reducePairs(changedInputs);
142✔
55
    if (changedInputs.every(item => item.key && item.value) && changedInputs.length !== Object.keys(inputObject).length) {
170✔
56
      setError('Duplicate keys exist, only the last set value will be submitted');
10✔
57
    } else {
58
      setError('');
132✔
59
    }
60
    onInputChange(inputObject);
142✔
61
  };
62

63
  const reducePairs = listOfPairs => listOfPairs.reduce((accu, item) => ({ ...accu, ...(item.value ? { [item.key]: item.value } : {}) }), {});
181✔
64

65
  const addKeyValue = () => {
181✔
66
    const changedInputs = [...inputs, { ...emptyInput, ref: createRef() }];
3✔
67
    setInputs(changedInputs);
3✔
68
    setError('');
3✔
69
  };
70

71
  const removeInput = index => {
181✔
72
    let changedInputs = [...inputs];
×
73
    changedInputs.splice(index, 1);
×
74
    setInputs(changedInputs);
×
75
    const inputObject = reducePairs(changedInputs);
×
76
    onInputChange(inputObject);
×
77
    setError('');
×
78
  };
79

80
  return (
181✔
81
    <div>
82
      {inputs.map((input, index) => {
83
        const hasError = Boolean(index === inputs.length - 1 && (errortext || error));
213✔
84
        const hasRemovalDisabled = !(inputs[index].key && inputs[index].value);
213✔
85
        const Helptip = inputs[index].helptip?.component;
213✔
86
        return (
213✔
87
          <div className="key-value-container relative" key={index}>
88
            <FormControl>
89
              <Input disabled={disabled} value={input.key} placeholder="Key" onChange={e => updateInputs('key', index, e)} type="text" />
57✔
90
              {hasError && <FormHelperText>{errortext || error}</FormHelperText>}
281✔
91
            </FormControl>
92
            <FormControl>
93
              <Input disabled={disabled} value={`${input.value}`} placeholder="Value" onChange={e => updateInputs('value', index, e)} type="text" />
85✔
94
            </FormControl>
95
            {inputs.length > 1 && !hasRemovalDisabled ? (
490✔
96
              <IconButton disabled={disabled} onClick={() => removeInput(index)} size="large">
×
97
                <ClearIcon fontSize="small" />
98
              </IconButton>
99
            ) : (
100
              <span />
101
            )}
102
            {Helptip && <Helptip anchor={{ left: -35, top: 5 }} {...inputs[index].helptip.props} />}
214✔
103
          </div>
104
        );
105
      })}
106
      <div className="key-value-container">
107
        <div style={{ minWidth: theme.spacing(30) }}>
108
          <Fab
109
            disabled={disabled || !inputs[inputs.length - 1].key || !inputs[inputs.length - 1].value}
493✔
110
            style={{ marginBottom: 10 }}
111
            color="secondary"
112
            size="small"
113
            onClick={addKeyValue}
114
          >
115
            <ContentAddIcon />
116
          </Fab>
117
        </div>
118
        <div style={{ minWidth: theme.spacing(30) }} />
119
        {inputs.length > 1 ? (
181✔
120
          <a className="margin-left-small" onClick={onClearClick}>
121
            clear all
122
          </a>
123
        ) : (
124
          <div />
125
        )}
126
      </div>
127
    </div>
128
  );
129
};
130

131
export default KeyValueEditor;
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