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

mendersoftware / gui / 914712491

pending completion
914712491

Pull #3798

gitlab-ci

mzedel
refac: refactored signup page to make better use of form capabilities

Signed-off-by: Manuel Zedel <manuel.zedel@northern.tech>
Pull Request #3798: MEN-3530 - refactored forms

4359 of 6322 branches covered (68.95%)

92 of 99 new or added lines in 11 files covered. (92.93%)

1715 existing lines in 159 files now uncovered.

8203 of 9941 relevant lines covered (82.52%)

150.06 hits per line

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

75.0
/src/js/components/settings/user-management/selfusermanagement.js
1
// Copyright 2017 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
import { useDispatch, useSelector } from 'react-redux';
16

17
import { Button, Switch, TextField } from '@mui/material';
18
import { makeStyles } from 'tss-react/mui';
19

20
import { setSnackbar } from '../../../actions/appActions';
21
import { editUser, saveUserSettings } from '../../../actions/userActions';
22
import { getToken } from '../../../auth';
23
import * as UserConstants from '../../../constants/userConstants';
24
import { toggle } from '../../../helpers';
25
import { getCurrentUser, getFeatures, getIsEnterprise, getUserSettings } from '../../../selectors';
26
import ExpandableAttribute from '../../common/expandable-attribute';
27
import Form from '../../common/forms/form';
28
import PasswordInput from '../../common/forms/passwordinput';
29
import TextInput from '../../common/forms/textinput';
30
import InfoText from '../../common/infotext';
31
import AccessTokenManagement from '../accesstokenmanagement';
32
import { CopyTextToClipboard } from '../organization/organization';
33
import TwoFactorAuthSetup from './twofactorauthsetup';
34
import { getUserSSOState } from './userdefinition';
35

36
const useStyles = makeStyles()(() => ({
5✔
37
  formField: { width: 400, maxWidth: '100%' },
38
  changeButton: { margin: '30px 0 0 15px' },
39
  infoText: { margin: 0, width: '75%' },
40
  jwt: { maxWidth: '70%' },
41
  oauthIcon: { fontSize: '36px', marginRight: 10 },
42
  widthLimit: { maxWidth: 750 }
43
}));
44

45
export const SelfUserManagement = () => {
5✔
46
  const [editEmail, setEditEmail] = useState(false);
8✔
47
  const [editPass, setEditPass] = useState(false);
7✔
48
  const { classes } = useStyles();
7✔
49
  const dispatch = useDispatch();
7✔
50

51
  const { isHosted } = useSelector(getFeatures);
7✔
52
  const isEnterprise = useSelector(getIsEnterprise);
7✔
53
  const canHave2FA = isEnterprise || isHosted;
7✔
54
  const currentUser = useSelector(getCurrentUser);
7✔
55
  const hasTracking = useSelector(state => !!state.app.trackerCode);
7✔
56
  const { trackingConsentGiven: hasTrackingConsent, mode } = useSelector(getUserSettings);
7✔
57

58
  const editSubmit = userData => {
7✔
UNCOV
59
    if (userData.password != userData.password_confirmation) {
×
UNCOV
60
      dispatch(setSnackbar(`The passwords don't match`));
×
61
    } else {
UNCOV
62
      dispatch(editUser(UserConstants.OWN_USER_ID, userData)).then(() => {
×
UNCOV
63
        setEditEmail(false);
×
UNCOV
64
        setEditPass(false);
×
65
      });
66
    }
67
  };
68

69
  const handleEmail = () => setEditEmail(toggle);
7✔
70

71
  const toggleMode = () => {
7✔
UNCOV
72
    const newMode = mode === 'dark' ? 'light' : 'dark';
×
UNCOV
73
    dispatch(saveUserSettings({ mode: newMode }));
×
74
  };
75

76
  const handlePass = () => setEditPass(toggle);
7✔
77
  const email = currentUser.email;
7✔
78
  const { isOAuth2, provider } = getUserSSOState(currentUser);
7✔
79
  return (
7✔
80
    <div className={`margin-top-small ${classes.widthLimit}`}>
81
      <h2 className="margin-top-small">My profile</h2>
82
      {!editEmail && currentUser.email ? (
20✔
83
        <div className="flexbox space-between">
84
          <TextField className={classes.formField} label="Email" key={email} InputLabelProps={{ shrink: !!email }} disabled defaultValue={email} />
85
          {!isOAuth2 && (
11✔
86
            <Button className={`inline-block ${classes.changeButton}`} color="primary" id="change_email" onClick={handleEmail}>
87
              Change email
88
            </Button>
89
          )}
90
        </div>
91
      ) : (
92
        <Form
93
          defaultValues={{ email }}
94
          onSubmit={editSubmit}
95
          handleCancel={handleEmail}
96
          submitLabel="Save"
97
          showButtons={editEmail}
98
          buttonColor="secondary"
99
          submitButtonId="submit_email"
100
        >
101
          <TextInput disabled={false} hint="Email" id="email" InputLabelProps={{ shrink: !!email }} label="Email" validations="isLength:1,isEmail" />
102
          <PasswordInput id="current_password" label="Current password *" validations={`isLength:8,isNot:${email}`} required={true} />
103
        </Form>
104
      )}
105
      {!isOAuth2 &&
13✔
106
        (!editPass ? (
6✔
107
          <form className="flexbox space-between">
108
            <TextField className={classes.formField} label="Password" key="password-placeholder" disabled defaultValue="********" type="password" />
109
            <Button className={classes.changeButton} color="primary" id="change_password" onClick={handlePass}>
110
              Change password
111
            </Button>
112
          </form>
113
        ) : (
114
          <>
115
            <h3 className="margin-top margin-bottom-none">Change password</h3>
116
            <Form
117
              onSubmit={editSubmit}
118
              handleCancel={handlePass}
119
              submitLabel="Save"
120
              submitButtonId="submit_pass"
121
              buttonColor="secondary"
122
              showButtons={editPass}
123
            >
124
              <PasswordInput id="current_password" label="Current password *" validations={`isLength:8,isNot:${email}`} required />
125
              <PasswordInput className="edit-pass" id="password" label="Password *" validations={`isLength:8,isNot:${email}`} create generate required />
126
              <PasswordInput id="password_confirmation" label="Confirm password *" validations={`isLength:8,isNot:${email}`} required />
127
            </Form>
128
          </>
129
        ))}
130
      <div className="clickable flexbox space-between margin-top" onClick={toggleMode}>
131
        <p className="help-content">Enable dark theme</p>
132
        <Switch checked={mode === 'dark'} />
133
      </div>
134
      {!isOAuth2 ? (
7✔
135
        canHave2FA && <TwoFactorAuthSetup />
11✔
136
      ) : (
137
        <div className="flexbox margin-top">
138
          <div className={classes.oauthIcon}>{provider.icon}</div>
139
          <div className="info">
140
            You are logging in using your <strong>{provider.name}</strong> account.
141
            <br />
142
            Please connect to {provider.name} to update your login settings.
143
          </div>
144
        </div>
145
      )}
146
      <div className="flexbox space-between margin-top-large">
147
        <div className={classes.jwt}>
148
          <div className="help-content">Session token</div>
149
          <ExpandableAttribute
150
            component="div"
151
            disableGutters
152
            dividerDisabled
153
            secondary={getToken()}
154
            textClasses={{ secondary: 'inventory-text tenant-token-text' }}
155
          />
156
        </div>
157
        <div className="flexbox center-aligned">
158
          <CopyTextToClipboard token={getToken()} />
159
        </div>
160
      </div>
161
      {!isOAuth2 && <AccessTokenManagement />}
13✔
162
      {isEnterprise && hasTracking && (
12!
163
        <div className="margin-top">
UNCOV
164
          <div className="clickable flexbox space-between" onClick={() => dispatch(saveUserSettings({ trackingConsentGiven: !hasTrackingConsent }))}>
×
165
            <p className="help-content">Help us improve Mender</p>
166
            <Switch checked={!!hasTrackingConsent} />
167
          </div>
168
          <InfoText className={classes.infoText}>Enable usage data and errors to be sent to help us improve our service.</InfoText>
169
        </div>
170
      )}
171
    </div>
172
  );
173
};
174

175
export default SelfUserManagement;
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