• 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

61.36
/src/js/components/devices/group-management/group-definition.js
1
// Copyright 2020 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, { useState } from 'react';
15

16
import { Autocomplete, FormHelperText, TextField } from '@mui/material';
17
import { createFilterOptions } from '@mui/material/useAutocomplete';
18

19
import validator from 'validator';
20

21
import { UNGROUPED_GROUP } from '../../../constants/deviceConstants';
22
import { fullyDecodeURI } from '../../../helpers';
23
import DocsLink from '../../common/docslink';
24
import InfoText from '../../common/infotext';
25

26
const filter = createFilterOptions();
5✔
27

28
export const validateGroupName = (encodedName, groups = [], selectedDevices = [], isCreationDynamic) => {
5!
29
  const name = fullyDecodeURI(encodedName);
5✔
30
  let invalid = false;
5✔
31
  let errortext = null;
5✔
32
  const isModification = name.length && groups.some(group => decodeURIComponent(group) === name);
5✔
33
  if (!name && !isModification) {
5✔
34
    invalid = true;
2✔
35
  } else if (!validator.isWhitelisted(name, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.')) {
3✔
36
    invalid = true;
1✔
37
    errortext = 'Valid characters are a-z, A-Z, 0-9, ., _ and -';
1✔
38
  } else if (selectedDevices.length && selectedDevices.every(({ group }) => group === name)) {
2✔
39
    invalid = true;
1✔
40
    errortext = `${name} is the same group the selected devices are already in`;
1✔
41
  } else if (isModification && isCreationDynamic) {
1!
42
    invalid = true;
1✔
43
    errortext = 'A group with the same name already exists';
1✔
44
  } else if (name === UNGROUPED_GROUP.name) {
×
45
    invalid = true;
×
46
    errortext = `A group with the name ${name} is created automatically`;
×
47
  }
48
  return { errortext, invalid, isModification, name };
5✔
49
};
50

51
const GroupOption = (props, option) => <li {...props}>{option.title}</li>;
5✔
52

53
export const GroupDefinition = ({ isCreationDynamic, groups, newGroup, onInputChange, selectedDevices, selectedGroup }) => {
5✔
54
  const [errortext, setErrorText] = useState('');
2✔
55

56
  const validateName = encodedName => {
2✔
57
    const { errortext: error, invalid, isModification, name } = validateGroupName(encodedName, groups, selectedDevices, isCreationDynamic);
×
58
    setErrorText(error);
×
59
    onInputChange(invalid, name, isModification);
×
60
  };
61

62
  const filteredGroups = groups
2✔
63
    .filter(group => group !== selectedGroup)
1✔
64
    .map(group => ({
1✔
65
      value: group,
66
      title: group
67
    }));
68
  return (
2✔
69
    <>
70
      <Autocomplete
71
        id="group-creation-selection"
72
        autoSelect
73
        freeSolo
74
        filterSelectedOptions
75
        filterOptions={(options, params) => {
76
          const filtered = filter(options, params);
×
77
          if (
×
78
            params.inputValue !== '' &&
×
79
            !groups.some(group => decodeURIComponent(group) === params.inputValue) &&
×
80
            (filtered.length !== 1 || (filtered.length === 1 && filtered[0].title !== params.inputValue))
81
          ) {
82
            filtered.push({
×
83
              inputValue: params.inputValue,
84
              title: `Create "${params.inputValue}" group`
85
            });
86
          }
87
          return filtered;
×
88
        }}
89
        getOptionLabel={option => {
90
          if (typeof option === 'string') {
×
91
            return option;
×
92
          }
93
          if (option.inputValue) {
×
94
            return option.inputValue;
×
95
          }
96
          return option.title;
×
97
        }}
98
        handleHomeEndKeys
99
        inputValue={newGroup}
100
        options={filteredGroups}
101
        onInputChange={(e, newValue) => validateName(newValue)}
×
102
        renderInput={params => <TextField {...params} label="Select a group, or type to create new" InputProps={{ ...params.InputProps }} />}
4✔
103
        renderOption={GroupOption}
104
      />
105
      <FormHelperText>{errortext}</FormHelperText>
106
      {isCreationDynamic && (
3✔
107
        <InfoText>
108
          Note: individual devices can&apos;t be added to dynamic groups.
109
          <br />
110
          <DocsLink path="overview/device-group" title="Learn more about static vs. dynamic groups" />
111
        </InfoText>
112
      )}
113
    </>
114
  );
115
};
116

117
export default GroupDefinition;
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