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

mendersoftware / gui / 1113439055

19 Dec 2023 09:01PM UTC coverage: 82.752% (-17.2%) from 99.964%
1113439055

Pull #4258

gitlab-ci

mender-test-bot
chore: Types update

Signed-off-by: Mender Test Bot <mender@northern.tech>
Pull Request #4258: chore: Types update

4326 of 6319 branches covered (0.0%)

8348 of 10088 relevant lines covered (82.75%)

189.39 hits per line

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

67.57
/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 { getDeviceAttributes, 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(() => {
75
        dispatch(getDeviceAttributes());
×
76
        setIsEditing(false);
×
77
      })
78
      .finally(() => setIsEditDisabled(false));
×
79
  };
80

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

125
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