• 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

64.79
/src/js/components/search-result.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, { useEffect, useState } from 'react';
15
import { useDispatch, useSelector } from 'react-redux';
16
import { useNavigate } from 'react-router-dom';
17

18
import { Close as CloseIcon } from '@mui/icons-material';
19
// material ui
20
import { Drawer, IconButton, Typography } from '@mui/material';
21
import { makeStyles } from 'tss-react/mui';
22

23
import pluralize from 'pluralize';
24

25
import { setSearchState } from '../actions/appActions';
26
import { setDeviceListState } from '../actions/deviceActions';
27
import { SORTING_OPTIONS, TIMEOUTS } from '../constants/appConstants';
28
import { getIdAttribute, getMappedDevicesList, getOnboardingState, getUserSettings } from '../selectors';
29
import { getHeaders } from './devices/authorized-devices';
30
import { routes } from './devices/base-devices';
31
import Devicelist from './devices/devicelist';
32

33
const useStyles = makeStyles()(theme => ({
56✔
34
  drawerOffset: {
35
    top: theme.mixins.toolbar.minHeight + 1,
36
    left: 200
37
  },
38
  paper: {
39
    maxWidth: '100vw',
40
    minHeight: '20vh',
41
    boxShadow: 'none'
42
  }
43
}));
44

45
const leftNavOffset = 500;
3✔
46
const ResultTitle = ({ onClick, term, total }) => {
3✔
47
  const content = `${total ? total : 'No'} ${pluralize('device', total)} found for "${term}"`;
1!
48
  let props = { className: 'bold' };
1✔
49
  let style = {};
1✔
50
  if (!total) {
1!
51
    props = { className: 'info' };
1✔
52
    style = { width: `calc(100% - ${leftNavOffset}px)` };
1✔
53
  }
54
  return (
1✔
55
    <div className={`flexbox ${total ? 'center-aligned' : 'centered'}`} style={style}>
1!
56
      <Typography variant="body2" {...props}>
57
        {content}
58
      </Typography>
59
      <a className="margin-left-large" onClick={onClick}>
60
        clear search
61
      </a>
62
    </div>
63
  );
64
};
65

66
export const SearchResult = ({ onToggleSearchResult, open = true }) => {
3!
67
  const navigate = useNavigate();
525✔
68
  const dispatch = useDispatch();
525✔
69

70
  const { columnSelection } = useSelector(getUserSettings);
525✔
71
  const customColumnSizes = useSelector(state => state.users.customColumns);
955✔
72
  const devices = useSelector(state => getMappedDevicesList(state, 'search'));
955✔
73
  const idAttribute = useSelector(getIdAttribute);
525✔
74
  const onboardingState = useSelector(getOnboardingState);
525✔
75
  const searchState = useSelector(state => state.app.searchState);
955✔
76

77
  const { classes } = useStyles();
525✔
78

79
  const [columnHeaders, setColumnHeaders] = useState(getHeaders(columnSelection, routes.devices.defaultHeaders, idAttribute));
525✔
80

81
  const { isSearching, searchTerm, searchTotal, sort = {} } = searchState;
525!
82
  const { direction: sortDown = SORTING_OPTIONS.desc, key: sortCol } = sort;
525✔
83

84
  useEffect(() => {
525✔
85
    const columnHeaders = getHeaders(columnSelection, routes.devices.defaultHeaders, idAttribute);
13✔
86
    setColumnHeaders(columnHeaders);
13✔
87
  }, [columnSelection, idAttribute.attribute]);
88

89
  useEffect(() => {
525✔
90
    if (!open && isSearching) {
6!
UNCOV
91
      onToggleSearchResult();
×
92
    }
93
  }, [open, isSearching]);
94

95
  useEffect(() => {
525✔
96
    if (open && !searchTerm) {
6!
UNCOV
97
      onToggleSearchResult();
×
98
    }
99
  }, [open, searchTerm]);
100

101
  const onDeviceSelect = device => {
525✔
UNCOV
102
    dispatch(setDeviceListState({ selectedId: device.id }));
×
UNCOV
103
    onToggleSearchResult();
×
UNCOV
104
    setTimeout(() => navigate(`/devices/${device.status}?id=${device.id}`), TIMEOUTS.debounceShort);
×
105
  };
106

107
  const handlePageChange = page => {
525✔
UNCOV
108
    dispatch(setSearchState({ page }));
×
109
  };
110

111
  const onSortChange = attribute => {
525✔
UNCOV
112
    let changedSortCol = attribute.name;
×
UNCOV
113
    let changedSortDown = sortDown === SORTING_OPTIONS.desc ? SORTING_OPTIONS.asc : SORTING_OPTIONS.desc;
×
UNCOV
114
    if (changedSortCol !== sortCol) {
×
UNCOV
115
      changedSortDown = SORTING_OPTIONS.desc;
×
116
    }
UNCOV
117
    dispatch(setSearchState({ page: 1, sort: { direction: changedSortDown, key: changedSortCol, scope: attribute.scope } }));
×
118
  };
119

120
  const onClearClick = () => {
525✔
UNCOV
121
    dispatch(setSearchState({ searchTerm: '' }));
×
UNCOV
122
    onToggleSearchResult();
×
123
  };
124

125
  return (
525✔
126
    <Drawer
127
      anchor="top"
128
      classes={classes}
129
      disableEnforceFocus
130
      open={open}
131
      ModalProps={{ className: classes.drawerOffset, BackdropProps: { className: classes.drawerOffset } }}
132
      PaperProps={{ className: `${classes.drawerOffset} ${classes.paper}` }}
133
      SlideProps={{ direction: 'left' }}
134
    >
135
      <div className="flexbox center-aligned margin-bottom-small space-between">
136
        <ResultTitle onClick={onClearClick} term={searchTerm} total={searchTotal} />
137
        <IconButton onClick={onToggleSearchResult} aria-label="close" size="large">
138
          <CloseIcon />
139
        </IconButton>
140
      </div>
141
      {!!searchTotal && (
525!
142
        <Devicelist
143
          className=""
144
          columnHeaders={columnHeaders}
145
          customColumnSizes={customColumnSizes}
146
          deviceListState={{ perPage: 10, sort: {} }}
147
          devices={devices}
148
          idAttribute={idAttribute}
149
          onboardingState={onboardingState}
150
          onSort={onSortChange}
151
          PaginationProps={{ rowsPerPageOptions: [10] }}
152
          pageTotal={searchTotal}
153
          onPageChange={handlePageChange}
154
          pageLoading={isSearching}
155
          onExpandClick={onDeviceSelect}
156
        />
157
      )}
158
    </Drawer>
159
  );
160
};
161

162
export default SearchResult;
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