Skip to content

Commit a2aff1f

Browse files
chore(site): replace xstate by react-query on update password (#9696)
1 parent e74d8a7 commit a2aff1f

File tree

3 files changed

+27
-90
lines changed

3 files changed

+27
-90
lines changed

site/src/api/queries/users.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as API from "api/api";
2+
import { UpdateUserPasswordRequest } from "api/typesGenerated";
3+
4+
export const updatePassword = () => {
5+
return {
6+
mutationFn: ({
7+
userId,
8+
...request
9+
}: UpdateUserPasswordRequest & { userId: string }) =>
10+
API.updateUserPassword(userId, request),
11+
};
12+
};

site/src/pages/UserSettingsPage/SecurityPage/SecurityPage.tsx

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,21 @@
1-
import { useMachine } from "@xstate/react";
21
import { useMe } from "hooks/useMe";
32
import { ComponentProps, FC } from "react";
4-
import { userSecuritySettingsMachine } from "xServices/userSecuritySettings/userSecuritySettingsXService";
5-
import { Section } from "../../../components/SettingsLayout/Section";
3+
import { Section } from "components/SettingsLayout/Section";
64
import { SecurityForm } from "./SettingsSecurityForm";
7-
import { useQuery } from "@tanstack/react-query";
5+
import { useMutation, useQuery } from "@tanstack/react-query";
86
import { getAuthMethods, getUserLoginType } from "api/api";
97
import {
108
SingleSignOnSection,
119
useSingleSignOnSection,
1210
} from "./SingleSignOnSection";
1311
import { Loader } from "components/Loader/Loader";
1412
import { Stack } from "components/Stack/Stack";
13+
import { updatePassword } from "api/queries/users";
14+
import { displaySuccess } from "components/GlobalSnackbar/utils";
1515

1616
export const SecurityPage: FC = () => {
1717
const me = useMe();
18-
const [securityState, securitySend] = useMachine(
19-
userSecuritySettingsMachine,
20-
{
21-
context: {
22-
userId: me.id,
23-
},
24-
},
25-
);
26-
const { error } = securityState.context;
18+
const updatePasswordMutation = useMutation(updatePassword());
2719
const { data: authMethods } = useQuery({
2820
queryKey: ["authMethods"],
2921
queryFn: getAuthMethods,
@@ -43,13 +35,17 @@ export const SecurityPage: FC = () => {
4335
security={{
4436
form: {
4537
disabled: userLoginType.login_type !== "password",
46-
error,
47-
isLoading: securityState.matches("updatingSecurity"),
48-
onSubmit: (data) => {
49-
securitySend({
50-
type: "UPDATE_SECURITY",
51-
data,
38+
error: updatePasswordMutation.error,
39+
isLoading: updatePasswordMutation.isLoading,
40+
onSubmit: async (data) => {
41+
await updatePasswordMutation.mutateAsync({
42+
userId: me.id,
43+
...data,
5244
});
45+
displaySuccess("Updated password.");
46+
// Refresh the browser session. We need to improve the AuthProvider
47+
// to include better API to handle these scenarios
48+
window.location.href = location.origin;
5349
},
5450
},
5551
}}

site/src/xServices/userSecuritySettings/userSecuritySettingsXService.ts

Lines changed: 0 additions & 71 deletions
This file was deleted.

0 commit comments

Comments
 (0)