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

mendersoftware / gui / 901187442

pending completion
901187442

Pull #3795

gitlab-ci

mzedel
feat: increased chances of adopting our intended navigation patterns instead of unsupported browser navigation

Ticket: None
Changelog: None
Signed-off-by: Manuel Zedel <manuel.zedel@northern.tech>
Pull Request #3795: feat: increased chances of adopting our intended navigation patterns instead of unsupported browser navigation

4389 of 6365 branches covered (68.96%)

5 of 5 new or added lines in 1 file covered. (100.0%)

1729 existing lines in 165 files now uncovered.

8274 of 10019 relevant lines covered (82.58%)

144.86 hits per line

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

68.42
/src/js/components/devices/widgets/attribute-autocomplete.js
1
// Copyright 2022 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, { useCallback, useEffect, useRef, useState } from 'react';
15

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

19
import { TIMEOUTS } from '../../../constants/appConstants';
20
import { getFilterLabelByKey } from './filters';
21

22
const textFieldStyle = { marginTop: 0, marginBottom: 15 };
13✔
23

24
const defaultScope = 'inventory';
13✔
25

26
const getOptionLabel = option => option.value || option.key || option;
113✔
27

28
const FilterOption = (props, option) => {
13✔
29
  let content = getOptionLabel(option);
19✔
30
  if (option.category === 'recently used') {
19!
UNCOV
31
    content = (
×
32
      <div className="flexbox center-aligned space-between" style={{ width: '100%' }}>
33
        <div>{content}</div>
34
        <div className="muted slightly-smaller">({option.scope})</div>
35
      </div>
36
    );
37
  }
38
  return <li {...props}>{content}</li>;
19✔
39
};
40

41
const optionsFilter = createFilterOptions();
13✔
42

43
const filterOptions = (options, params) => {
13✔
44
  const filtered = optionsFilter(options, params);
9✔
45
  if (filtered.length !== 1 && params.inputValue !== '') {
9✔
46
    filtered.push({
7✔
47
      inputValue: params.inputValue,
48
      key: 'custom',
49
      value: `Use "${params.inputValue}"`,
50
      category: 'custom',
51
      priority: 99
52
    });
53
  }
54
  return filtered;
9✔
55
};
56

57
const defaultFilter = { key: '', scope: defaultScope };
13✔
58

59
export const AttributeAutoComplete = ({ attributes, disabled, filter = defaultFilter, label = 'Attribute', onRemove, onSelect }) => {
13!
60
  const [key, setKey] = useState(filter.key); // this refers to the selected filter with key as the id
14✔
61
  const [options, setOptions] = useState([]);
14✔
62
  const [reset, setReset] = useState(true);
14✔
63
  const [scope, setScope] = useState(filter.scope);
14✔
64
  const timer = useRef();
14✔
65

66
  useEffect(() => {
14✔
67
    return () => {
4✔
68
      clearTimeout(timer.current);
4✔
69
    };
70
  }, []);
71

72
  useEffect(() => {
14✔
73
    setKey('');
6✔
74
    setScope(defaultScope);
6✔
75
    setOptions(attributes.sort((a, b) => a.priority - b.priority));
12✔
76
  }, [attributes.length, reset]);
77

78
  useEffect(() => {
14✔
79
    setKey(filter.key);
4✔
80
  }, [filter.key]);
81

82
  useEffect(() => {
14✔
83
    clearTimeout(timer.current);
6✔
84
    timer.current = setTimeout(() => onSelect({ key, scope }), TIMEOUTS.debounceDefault);
6✔
85
  }, [key, scope]);
86

87
  const updateFilterKey = (value, selectedScope) => {
14✔
UNCOV
88
    if (!value) {
×
UNCOV
89
      return removeFilter();
×
90
    }
UNCOV
91
    const { key = value, scope: fallbackScope } = attributes.find(filter => filter.key === value) ?? {};
×
UNCOV
92
    setKey(key);
×
UNCOV
93
    setScope(selectedScope || fallbackScope);
×
94
  };
95

96
  const removeFilter = useCallback(() => {
14✔
UNCOV
97
    if (key) {
×
UNCOV
98
      onRemove({ key, scope });
×
99
    }
UNCOV
100
    setReset(!reset);
×
101
  }, [key, onRemove, reset, setReset, scope]);
102

103
  return (
14✔
104
    <Autocomplete
105
      autoComplete
106
      autoHighlight
107
      autoSelect
108
      disabled={disabled}
109
      freeSolo
110
      filterSelectedOptions
111
      filterOptions={filterOptions}
112
      getOptionLabel={getOptionLabel}
113
      groupBy={option => option.category}
19✔
114
      renderOption={FilterOption}
115
      id="filter-selection"
116
      includeInputInList={true}
117
      onChange={(e, changedValue) => {
118
        const { inputValue, key = changedValue, scope } = changedValue || {};
1!
119
        if (inputValue) {
1!
120
          // only circumvent updateFilterKey if we deal with a custom attribute - those will be treated as inventory attributes
121
          setKey(inputValue);
1✔
122
          return setScope(defaultScope);
1✔
123
        }
UNCOV
124
        updateFilterKey(key, scope);
×
125
      }}
126
      options={options}
127
      renderInput={params => <TextField {...params} label={label} style={textFieldStyle} />}
25✔
128
      key={reset}
129
      value={getFilterLabelByKey(key, attributes)}
130
    />
131
  );
132
};
133

134
export default AttributeAutoComplete;
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