1
- import { useMachine } from "@xstate/react" ;
2
1
import { useMe } from "hooks/useMe" ;
3
2
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" ;
6
4
import { SecurityForm } from "./SettingsSecurityForm" ;
7
- import { useQuery } from "@tanstack/react-query" ;
5
+ import { useMutation , useQuery } from "@tanstack/react-query" ;
8
6
import { getAuthMethods , getUserLoginType } from "api/api" ;
9
7
import {
10
8
SingleSignOnSection ,
11
9
useSingleSignOnSection ,
12
10
} from "./SingleSignOnSection" ;
13
11
import { Loader } from "components/Loader/Loader" ;
14
12
import { Stack } from "components/Stack/Stack" ;
13
+ import { updatePassword } from "api/queries/users" ;
14
+ import { displaySuccess } from "components/GlobalSnackbar/utils" ;
15
15
16
16
export const SecurityPage : FC = ( ) => {
17
17
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 ( ) ) ;
27
19
const { data : authMethods } = useQuery ( {
28
20
queryKey : [ "authMethods" ] ,
29
21
queryFn : getAuthMethods ,
@@ -43,13 +35,17 @@ export const SecurityPage: FC = () => {
43
35
security = { {
44
36
form : {
45
37
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 ,
52
44
} ) ;
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 ;
53
49
} ,
54
50
} ,
55
51
} }
0 commit comments