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

Improve the ergonomics of data loading in the front-end #558

Merged
merged 22 commits into from
Jul 30, 2024

Conversation

raix
Copy link
Contributor

@raix raix commented Jul 30, 2024

Summary & Motivation

Wrap the openapi-fetch client as an easy to use client specific to React and PlatformPlatform.

  • Add form Action support matching the new React api's useFormState/useActionState
  • Add a useApi hook for easy data loading, with build in state management
  • Ensure the api client always return errors in the ProblemDetails format

Examples of usage

React hook: useApi - Simple

The hook can be used to simply get the data from the server, but also provides loading, success state and problem detail info like type / status / title / detail and errors compatible with React Aria Components validation in forms.

  const { data } = useApi("/api/account-management/users", {
    params: {
      query: {
        PageSize: 1
      }
    }
  });

React hook: useApi - Debounced fetching

For use with debounced search etc. and threshold of min 3 chars before fetching the result:

  const [subdomain, setSubdomain] = useState("");
  const { data: isSubdomainFree } = useApi(
    "/api/account-management/account-registrations/is-subdomain-free",
    {
      params: {
        query: { Subdomain: subdomain }
      }
    },
    {
      autoFetch: subdomain.length > 3,
      debounceMs: 500
    }
  );

Use with useFormState / useActionState

The action is passed to the Form component and errors can be passed directly to validationErrors:

  const [{ success, errors, data, title, message }, action, isPending] = useFormState(
    api.action("/api/account-management/account-registrations/start"),
    { success: null }
  );

  // ...

    <Form
      action={action}
      validationErrors={errors}
      validationBehavior="aria"
    >

Plain fetch

The result will always be the data and the api client throws ProblemDetailsError

  const result = await api.get("/api/account-management/account-registrations/is-subdomain-free",
    {
      params: {
        query: { Subdomain: subdomain }
      }
    });

Checklist

  • I have added a Label to the pull-request
  • I have added tests, and done manual regression tests
  • I have updated the documentation, if necessary

@raix raix added Enhancement New feature or request .NET labels Jul 30, 2024
@raix raix self-assigned this Jul 30, 2024
Copy link

Copy link
Member

@tjementum tjementum left a comment

Choose a reason for hiding this comment

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

Let me take a look. Please update the pull-request description, so it includes all the details of what is changed and why. It's the only dokumentation we have :)

tjementum
tjementum previously approved these changes Jul 30, 2024
Copy link
Member

@tjementum tjementum left a comment

Choose a reason for hiding this comment

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

I don't have more comments, except for pull-request description. And I would always a bit cleaner commit history.

Plus fix the SonarCloud warnings :)

But overall, this is just awesome :)

@raix raix force-pushed the 557-Improve-the-ergonomics-of-data-loading-in-the-front-end branch from 24e831c to dbfb99f Compare July 30, 2024 15:27
Copy link

Please retry analysis of this Pull-Request directly on SonarCloud

@raix raix force-pushed the 557-Improve-the-ergonomics-of-data-loading-in-the-front-end branch from 01b6347 to eb918df Compare July 30, 2024 16:38
Copy link

@raix raix merged commit 0b38b37 into main Jul 30, 2024
13 checks passed
@raix raix deleted the 557-Improve-the-ergonomics-of-data-loading-in-the-front-end branch July 30, 2024 17:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants