Skip to content
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

WizardV2: validate uniqueness of name (HMS-3801) #1944

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ezr-ondrej
Copy link
Contributor

@ezr-ondrej ezr-ondrej commented Apr 16, 2024

Fetches Bluepritnts with the same name as inputed and when there is already one in the database, fails the validation.

This is implemented by adding async validation to the StateValidatedInput, which is bit complex as we wanted to add debounce for that validation as well, so it doesn't fire too frequently.

@ezr-ondrej ezr-ondrej changed the title WizardV2: validate uniqueness of name WizardV2: validate uniqueness of name (HMS-3801) Apr 16, 2024
Comment on lines 39 to 42
const { data: sameNameBlueprint, isFetching } = useGetBlueprintsQuery(
{ name: blueprintName },
{ skip: blueprintName.length < 2 }
);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're firing for every name change, which is bit unfortunate, but I didn't manage to make debounce work for this.

Copy link
Member

@amirfefer amirfefer Apr 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have you tried something like:

  const [debouncedBlueprintName, setDebouncedBlueprintName] = useState(blueprintName);

  useEffect(() => {
    const handler = setTimeout(() => {
      setDebouncedBlueprintName(blueprintName);
    }, 500); 
    return () => {
      clearTimeout(handler);
    };
  }, [blueprintName]);

  const { data: sameNameBlueprint, isFetching } = useGetBlueprintsQuery(
    { name: debouncedBlueprintName },
    { skip: debouncedBlueprintName.length < 2 }
  );

this is not a classic debounced function, but it uses the state update mechanism of react to achieve it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't, I guess this would work, but it would be setting the name to the local state on top of the global one.
Tho I guess it's simpliest solution we have now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've fixed this in new version, but unfortunatelly it requires the caller to provide a memoized function, so it's prone to mistakes.

@ezr-ondrej
Copy link
Contributor Author

Actually #1949 might make it bit easier :)

@ezr-ondrej ezr-ondrej force-pushed the blueprint_unique_name branch 2 times, most recently from edc05f2 to d341a95 Compare April 18, 2024 09:20
@ezr-ondrej
Copy link
Contributor Author

I've rebased this to use the new state validation :)

Comment on lines +34 to +37
asyncValidator?: (
value: string,
callback: (isValid: boolean) => void
) => void;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We now have second validator prop, that allows for async validation, when provided, it waits for the async validation to finish to mark validation success.

@ezr-ondrej ezr-ondrej force-pushed the blueprint_unique_name branch 5 times, most recently from 8e8b8fd to f1c4111 Compare April 22, 2024 18:19
@lucasgarfield
Copy link
Collaborator

Is this ready for review, @ezr-ondrej ?

@ezr-ondrej
Copy link
Contributor Author

Is this ready for review, @ezr-ondrej ?

Definitelly ready for review, but if we prefer to wait until we figure out the validation properly, we can do that as well :)

@regexowl
Copy link
Collaborator

Heya! I guess we went with the "wait until we figure out the validation properly" plan? Added a "blocked" label, feel free to replace it with "ready for review" in case I guessed wrong 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants