-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adapted first user form to be shown in a page instead of a popup #1306
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
aa99978
Adapted users form to be shown in a page
teclator 9f89942
Make user password set consistent
teclator 703fd03
Removed unnecesary delete
teclator 049fa8c
Show password required validation in case of editing
teclator 5e19ba3
Use all fields are required validation
teclator 2f11032
Skip FirstUser tests
teclator File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,12 @@ import { | |
Menu, | ||
MenuContent, | ||
MenuList, | ||
MenuItem | ||
MenuItem, | ||
Card, | ||
Grid, | ||
GridItem, | ||
Stack, | ||
Switch | ||
} from "@patternfly/react-core"; | ||
|
||
import { Loading } from "~/components/layout"; | ||
|
@@ -79,17 +84,20 @@ export default function FirstUserForm() { | |
const [insideDropDown, setInsideDropDown] = useState(false); | ||
const [focusedIndex, setFocusedIndex] = useState(-1); | ||
const [suggestions, setSuggestions] = useState([]); | ||
const [changePassword, setChangePassword] = useState(true); | ||
const usernameInputRef = useRef(); | ||
const navigate = useNavigate(); | ||
const passwordRef = useRef(); | ||
|
||
useEffect(() => { | ||
cancellablePromise(client.users.getUser()).then(userValues => { | ||
const editing = userValues.userName !== ""; | ||
setState({ | ||
load: true, | ||
user: userValues, | ||
isEditing: userValues.username !== "" | ||
isEditing: editing | ||
}); | ||
setChangePassword(!editing); | ||
}); | ||
}, [client.users, cancellablePromise]); | ||
|
||
|
@@ -123,6 +131,11 @@ export default function FirstUserForm() { | |
return user; | ||
}, user); | ||
|
||
if (!changePassword) { | ||
delete user.password; | ||
delete user.passwordConfirmation; | ||
} | ||
|
||
// Preserve current password value if the user was not editing it. | ||
if (state.isEditing && user.password === "") delete user.password; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd say that line is no longer relevant |
||
delete user.passwordConfirmation; | ||
|
@@ -139,6 +152,11 @@ export default function FirstUserForm() { | |
return; | ||
} | ||
|
||
if (state.isEditing && changePassword && !user.password) { | ||
setErrors([_("Password is required")]); | ||
return; | ||
} | ||
|
||
const { result, issues = [] } = await client.users.setUser({ ...state.user, ...user }); | ||
if (!result || issues.length) { | ||
// FIXME: improve error handling. See client. | ||
|
@@ -191,68 +209,94 @@ export default function FirstUserForm() { | |
|
||
return ( | ||
<> | ||
<Page.Header> | ||
<h2>{state.isEditing ? _("Edit user") : _("Create user")}</h2> | ||
</Page.Header> | ||
|
||
<Page.MainContent> | ||
<Form id="firstUserForm" onSubmit={onSubmit}> | ||
{errors.length > 0 && | ||
<Alert variant="warning" isInline title={_("Something went wrong")}> | ||
{errors.map((e, i) => <p key={`error_${i}`}>{e}</p>)} | ||
</Alert>} | ||
<Grid hasGutter> | ||
<GridItem sm={12} xl={6} rowSpan={2}> | ||
<Page.CardSection isFullHeight> | ||
<Stack hasGutter> | ||
<FormGroup fieldId="userFullName" label={_("Full name")}> | ||
<TextInput | ||
id="userFullName" | ||
name="fullName" | ||
aria-label={_("User full name")} | ||
defaultValue={state.user.fullName} | ||
label={_("User full name")} | ||
onBlur={(e) => setSuggestions(suggestUsernames(e.target.value))} | ||
/> | ||
</FormGroup> | ||
|
||
<FormGroup fieldId="userFullName" label={_("Full name")}> | ||
<TextInput | ||
id="userFullName" | ||
name="fullName" | ||
aria-label={_("User full name")} | ||
defaultValue={state.user.fullName} | ||
label={_("User full name")} | ||
onBlur={(e) => setSuggestions(suggestUsernames(e.target.value))} | ||
/> | ||
</FormGroup> | ||
|
||
<FormGroup | ||
className="first-username-wrapper" | ||
fieldId="userName" | ||
label={_("Username")} | ||
isRequired | ||
> | ||
<TextInput | ||
id="userName" | ||
name="userName" | ||
aria-label={_("Username")} | ||
ref={usernameInputRef} | ||
defaultValue={state.user.userName} | ||
label={_("Username")} | ||
isRequired | ||
onFocus={renderSuggestions} | ||
onKeyDown={handleKeyDown} | ||
onBlur={() => !insideDropDown && setShowSuggestions(false)} | ||
/> | ||
<If | ||
condition={showSuggestions} | ||
then={ | ||
<UsernameSuggestions | ||
entries={suggestions} | ||
onSelect={onSuggestionSelected} | ||
setInsideDropDown={setInsideDropDown} | ||
focusedIndex={focusedIndex} | ||
<FormGroup | ||
className="first-username-wrapper" | ||
fieldId="userName" | ||
label={_("Username")} | ||
isRequired | ||
> | ||
<TextInput | ||
id="userName" | ||
name="userName" | ||
aria-label={_("Username")} | ||
ref={usernameInputRef} | ||
defaultValue={state.user.userName} | ||
label={_("Username")} | ||
isRequired | ||
onFocus={renderSuggestions} | ||
onKeyDown={handleKeyDown} | ||
onBlur={() => !insideDropDown && setShowSuggestions(false)} | ||
/> | ||
<If | ||
condition={showSuggestions} | ||
then={ | ||
<UsernameSuggestions | ||
entries={suggestions} | ||
onSelect={onSuggestionSelected} | ||
setInsideDropDown={setInsideDropDown} | ||
focusedIndex={focusedIndex} | ||
/> | ||
} | ||
/> | ||
</FormGroup> | ||
</Stack> | ||
</Page.CardSection> | ||
</GridItem> | ||
<GridItem sm={12} xl={6}> | ||
<Page.CardSection> | ||
<Stack hasGutter> | ||
{state.isEditing && | ||
<Switch | ||
label={_("Change password")} | ||
isChecked={changePassword} | ||
onChange={() => setChangePassword(!changePassword)} | ||
/>} | ||
<PasswordAndConfirmationInput | ||
inputRef={passwordRef} | ||
isDisabled={!changePassword} | ||
showErrors={false} | ||
/> | ||
</Stack> | ||
</Page.CardSection> | ||
</GridItem> | ||
<GridItem sm={12} xl={6}> | ||
<Page.CardSection> | ||
<Checkbox | ||
aria-label={_("user autologin")} | ||
id="autologin" | ||
name="autologin" | ||
// TRANSLATORS: check box label | ||
label={_("Auto-login")} | ||
defaultChecked={state.user.autologin} | ||
/> | ||
} | ||
/> | ||
</FormGroup> | ||
|
||
<PasswordAndConfirmationInput | ||
inputRef={passwordRef} | ||
showErrors={false} | ||
/> | ||
|
||
<Checkbox | ||
aria-label={_("user autologin")} | ||
id="autologin" | ||
name="autologin" | ||
// TRANSLATORS: check box label | ||
label={_("Auto-login")} | ||
defaultChecked={state.user.autologin} | ||
/> | ||
</Page.CardSection> | ||
</GridItem> | ||
</Grid> | ||
</Form> | ||
</Page.MainContent> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd say passwordConfirmation should be always deleted, see few lines below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
true