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
Prevent duplicate form submission in Create realm dialog in admin ui #28256
Comments
Yeah, ideally we'd do this consistently across UIs. We should have some kind of re-usable component or function to handle all this stuff. It looks like React Form Hook accepts a Promise return value in the |
Took a quick look, and it seems we should take several of the fields in the import { type SubmitHandler, useForm, FormState } from 'react-hook-form';
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
const isSubmittable = (formState: FormState<any>) => {
return (
formState.isDirty &&
formState.isValid &&
!formState.isLoading &&
!formState.isValidating &&
!formState.isSubmitting
);
};
interface FormData {
username: string;
}
export default function App() {
const {
register,
handleSubmit,
formState,
} = useForm<FormData>();
const onSubmit: SubmitHandler<FormData> = async (data) => {
await sleep(2000);
console.log(data);
};
return (
<form onSubmit={handleSubmit(onSubmit)}>
<label htmlFor="username">User Name</label>
<input {...register('username')} placeholder="Enter a username" />
<button type="submit" disabled={!isSubmittable(formState)}>Submit</button>
</form>
);
} We should extract the |
That looks good! I think many forms are affected by this and that this is a common "error" that puzzles users. It seems just like a minor improvement, but making this more robust can help improve the trust of users in Keycloak. |
Agreed, should also not be too hard to put this in a utility of |
Hi, can I try to take this issue? Thank you for the sample code provided @jonkoops. Instead of creating a utility method import { Button, ButtonProps } from "@patternfly/react-core"
import { PropsWithChildren } from "react"
import { FieldValues, FormState } from "react-hook-form"
export type FormSubmitButtonProps = ButtonProps & {
formState: FormState<FieldValues>
}
const isSubmittable = (formState: FormState<FieldValues>) => {
return (
formState.isDirty &&
formState.isValid &&
!formState.isLoading &&
!formState.isValidating &&
!formState.isSubmitting
);
};
export const FormSubmitButton = ({ formState, children, ...rest }
: PropsWithChildren<FormSubmitButtonProps>) => {
return <Button {...rest} type="submit" isDisabled={!isSubmittable(formState)}>
{children}
</Button>
} I think this provides more flexibility and reusability compared to a utility method. |
That's a great idea @jhchong92. We're trying to reduce the amount of repetition in our form code, so that would certainly be beneficial. |
closes keycloak#28256 Signed-off-by: jchong <jhchong92@gmail.com>
closes keycloak#28256 Signed-off-by: jchong <jhchong92@gmail.com>
closes keycloak#28256 Signed-off-by: jchong <jhchong92@gmail.com>
closes keycloak#28256 Signed-off-by: jchong <jhchong92@gmail.com>
closes keycloak#28256 Signed-off-by: jchong <jhchong92@gmail.com>
closes keycloak#28256 Signed-off-by: jchong <jhchong92@gmail.com>
closes keycloak#28256 Signed-off-by: jchong <jhchong92@gmail.com>
closes keycloak#28256 Signed-off-by: jchong <jhchong92@gmail.com>
closes keycloak#28256 Signed-off-by: jchong <jhchong92@gmail.com>
closes keycloak#28256 Signed-off-by: jchong <jhchong92@gmail.com>
closes keycloak#28256 Signed-off-by: jchong <jhchong92@gmail.com>
closes keycloak#28256 Signed-off-by: jchong <jhchong92@gmail.com>
closes keycloak#28256 Signed-off-by: jchong <jhchong92@gmail.com>
closes keycloak#28256 Signed-off-by: jchong <jhchong92@gmail.com>
closes keycloak#28256 Signed-off-by: jchong <jhchong92@gmail.com>
Description
The current "Create realm" dialog in the admin-ui does allow clicking the "Create" button multiple times, if the server response is slow. This yields error messages like the following, where the first "Create realm" request succeeds, but the second attempt failes with a conflict error.
Discussion
No response
Motivation
This problem happens quite often in practice, especially during Keycloak demos when one wants to create a realm quickly.
Details
We should disable the "Create" button while the request is pending.
The text was updated successfully, but these errors were encountered: