Skip to content

chore(site): replace setup machine by react-query #9809

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

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions site/src/api/queries/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ export const updatePassword = () => {
API.updateUserPassword(userId, request),
};
};

export const createFirstUser = () => {
return {
mutationFn: API.createFirstUser,
};
};
34 changes: 12 additions & 22 deletions site/src/pages/SetupPage/SetupPage.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,15 @@
import { useMachine } from "@xstate/react";
import { useAuth } from "components/AuthProvider/AuthProvider";
import { FC } from "react";
import { Helmet } from "react-helmet-async";
import { pageTitle } from "utils/page";
import { setupMachine } from "xServices/setup/setupXService";
import { SetupPageView } from "./SetupPageView";
import { Navigate } from "react-router-dom";
import { useMutation } from "@tanstack/react-query";
import { createFirstUser } from "api/queries/users";

export const SetupPage: FC = () => {
const [authState, authSend] = useAuth();
const [setupState, setupSend] = useMachine(setupMachine, {
actions: {
onCreateFirstUser: ({ firstUser }) => {
if (!firstUser) {
throw new Error("First user was not defined.");
}
authSend({
type: "SIGN_IN",
email: firstUser.email,
password: firstUser.password,
});
},
},
});
const { error } = setupState.context;

const createFirstUserMutation = useMutation(createFirstUser());
const userIsSignedIn = authState.matches("signedIn");
const setupIsComplete =
!authState.matches("loadingInitialAuthData") &&
Expand All @@ -46,10 +31,15 @@ export const SetupPage: FC = () => {
<title>{pageTitle("Set up your account")}</title>
</Helmet>
<SetupPageView
isLoading={setupState.hasTag("loading")}
error={error}
onSubmit={(firstUser) => {
setupSend({ type: "CREATE_FIRST_USER", firstUser });
isLoading={createFirstUserMutation.isLoading}
error={createFirstUserMutation.error}
onSubmit={async (firstUser) => {
await createFirstUserMutation.mutateAsync(firstUser);
Copy link
Member

Choose a reason for hiding this comment

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

Forgot to ask earlier, but how has Coder generally approached error handling before? I checked the App.tsx file and didn't see a global error handler for the query client/cache, so I wasn't sure where/how we generally try to address errors

Copy link
Member

@Parkreiner Parkreiner Sep 21, 2023

Choose a reason for hiding this comment

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

Assuming error handling is taken care of, this looks good to me! Going to approve, just to avoid blocking you

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We don't have anything specifically but it depends if it is a form, a "display" page, or a standalone action.

  • Form forms, we get the error and pass down to the form component
  • For pages that show something we use an or a custom if you need it
  • For standalone actions like remove user, we use the displayError()

authSend({
type: "SIGN_IN",
email: firstUser.email,
password: firstUser.password,
});
}}
/>
</>
Expand Down
81 changes: 0 additions & 81 deletions site/src/xServices/setup/setupXService.ts

This file was deleted.