-
Notifications
You must be signed in to change notification settings - Fork 467
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
UI – Updates to confirm invite flow #25583
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f2ef840
Most updates to confirm invite flow
8efddf1
remove name and email from invite link
c47f038
small cleanup
3066bc2
include all data in API POST
728e008
stabilize onSubmit, use correct button type
131cc2a
better error flash on server error
42df342
add disclaimer to frontend patterns
4a635dd
change file
ac5e9e8
slight cleanup
3e2422d
Update validation, tests
fed2c76
Merge branch 'main' into 24486
53e3014
immediately interpret 404 error as negative response, skip redundant …
8ceca36
update form submission pattern and documentation
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
- Check the server for validity of any Fleet invites |
70 changes: 0 additions & 70 deletions
70
frontend/components/forms/ConfirmInviteForm/ConfirmInviteForm.jsx
This file was deleted.
Oops, something went wrong.
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
149 changes: 149 additions & 0 deletions
149
frontend/components/forms/ConfirmInviteForm/ConfirmInviteForm.tsx
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 |
---|---|---|
@@ -0,0 +1,149 @@ | ||
import React, { useCallback, useState } from "react"; | ||
|
||
import validateEquality from "components/forms/validators/validate_equality"; | ||
|
||
import Button from "components/buttons/Button"; | ||
// @ts-ignore | ||
import InputField from "components/forms/fields/InputField"; | ||
import { IFormField } from "interfaces/form_field"; | ||
|
||
const baseClass = "confirm-invite-page__form"; | ||
export interface IConfirmInviteFormData { | ||
name: string; | ||
password: string; | ||
password_confirmation: string; | ||
} | ||
interface IConfirmInviteFormProps { | ||
defaultFormData?: Partial<IConfirmInviteFormData>; | ||
handleSubmit: (data: IConfirmInviteFormData) => void; | ||
ancestorError?: string; | ||
} | ||
interface IConfirmInviteFormErrors { | ||
name?: string | null; | ||
password?: string | null; | ||
password_confirmation?: string | null; | ||
} | ||
|
||
const validate = (formData: IConfirmInviteFormData) => { | ||
const errors: IConfirmInviteFormErrors = {}; | ||
const { | ||
name, | ||
password, | ||
password_confirmation: passwordConfirmation, | ||
} = formData; | ||
|
||
if (!name) { | ||
errors.name = "Full name must be present"; | ||
} | ||
|
||
if ( | ||
password && | ||
passwordConfirmation && | ||
!validateEquality(password, passwordConfirmation) | ||
) { | ||
errors.password_confirmation = | ||
"Password confirmation does not match password"; | ||
} | ||
|
||
if (!password) { | ||
errors.password = "Password must be present"; | ||
} | ||
|
||
if (!passwordConfirmation) { | ||
errors.password_confirmation = "Password confirmation must be present"; | ||
} | ||
|
||
return errors; | ||
}; | ||
const ConfirmInviteForm = ({ | ||
defaultFormData, | ||
handleSubmit, | ||
ancestorError, | ||
}: IConfirmInviteFormProps) => { | ||
const [formData, setFormData] = useState<IConfirmInviteFormData>({ | ||
name: defaultFormData?.name || "", | ||
password: defaultFormData?.password || "", | ||
password_confirmation: defaultFormData?.password || "", | ||
}); | ||
const [formErrors, setFormErrors] = useState<IConfirmInviteFormErrors>({}); | ||
|
||
const { name, password, password_confirmation } = formData; | ||
|
||
const onInputChange = ({ name: n, value }: IFormField) => { | ||
const newFormData = { ...formData, [n]: value }; | ||
setFormData(newFormData); | ||
const newErrs = validate(newFormData); | ||
// only set errors that are updates of existing errors | ||
// new errors are only set on submit | ||
const errsToSet: Record<string, string> = {}; | ||
Object.keys(formErrors).forEach((k) => { | ||
// @ts-ignore | ||
if (newErrs[k]) { | ||
// @ts-ignore | ||
errsToSet[k] = newErrs[k]; | ||
} | ||
}); | ||
setFormErrors(errsToSet); | ||
}; | ||
|
||
const onSubmit = useCallback( | ||
(evt: React.FormEvent<HTMLFormElement>) => { | ||
evt.preventDefault(); | ||
|
||
const errs = validate(formData); | ||
if (Object.keys(errs).length > 0) { | ||
setFormErrors(errs); | ||
return; | ||
} | ||
handleSubmit(formData); | ||
}, | ||
[formData, handleSubmit] | ||
); | ||
|
||
return ( | ||
<form onSubmit={onSubmit} className={baseClass} autoComplete="off"> | ||
{ancestorError && <div className="form__base-error">{ancestorError}</div>} | ||
<InputField | ||
label="Full name" | ||
autofocus | ||
onChange={onInputChange} | ||
name="name" | ||
value={name} | ||
error={formErrors.name} | ||
parseTarget | ||
maxLength={80} | ||
/> | ||
<InputField | ||
label="Password" | ||
type="password" | ||
placeholder="Password" | ||
helpText="Must include 12 characters, at least 1 number (e.g. 0 - 9), and at least 1 symbol (e.g. &*#)" | ||
onChange={onInputChange} | ||
name="password" | ||
value={password} | ||
error={formErrors.password} | ||
parseTarget | ||
/> | ||
<InputField | ||
label="Confirm password" | ||
type="password" | ||
placeholder="Confirm password" | ||
onChange={onInputChange} | ||
name="password_confirmation" | ||
value={password_confirmation} | ||
error={formErrors.password_confirmation} | ||
parseTarget | ||
/> | ||
<Button | ||
type="submit" | ||
disabled={Object.keys(formErrors).length > 0} | ||
className="confirm-invite-button" | ||
variant="brand" | ||
> | ||
Submit | ||
</Button> | ||
</form> | ||
); | ||
}; | ||
|
||
export default ConfirmInviteForm; |
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { size } from "lodash"; | ||
import validateEquality from "components/forms/validators/validate_equality"; | ||
|
||
const validate = (formData) => { | ||
const errors = {}; | ||
const { | ||
name, | ||
password, | ||
password_confirmation: passwordConfirmation, | ||
} = formData; | ||
|
||
if (!name) { | ||
errors.name = "Full name must be present"; | ||
} | ||
|
||
if ( | ||
password && | ||
passwordConfirmation && | ||
!validateEquality(password, passwordConfirmation) | ||
) { | ||
errors.password_confirmation = | ||
"Password confirmation does not match password"; | ||
} | ||
|
||
if (!password) { | ||
errors.password = "Password must be present"; | ||
} | ||
|
||
if (!passwordConfirmation) { | ||
errors.password_confirmation = "Password confirmation must be present"; | ||
} | ||
|
||
const valid = !size(errors); | ||
|
||
return { valid, errors }; | ||
}; | ||
|
||
export default { validate }; |
File renamed without changes.
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.
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.
This just calls lodash's
isEqual
. Using here for consistency but perhaps we can retire this function in favor of calling the lodash method directly.