1
- import React , { useState } from 'react' ;
1
+ import React , { useState , useContext , useEffect } from 'react' ;
2
+ import { useNavigate } from 'react-router-dom' ;
2
3
import { Button , Dialog , DialogTitle , DialogContent , DialogContentText , DialogActions , LinearProgress } from '@mui/material' ;
3
4
import api from '../utils/api' ;
5
+ import UserContext from './contexts/UserContext' ;
6
+ import { handleLogout } from '../utils/auth' ; // Import handleLogout
4
7
5
8
const ProfileSettings = ( ) => {
6
9
const [ open , setOpen ] = useState ( false ) ;
7
- const [ deleteConfirmOpen , setDeleteConfirmOpen ] = useState ( false ) ;
8
10
const [ deleteInProgress , setDeleteInProgress ] = useState ( false ) ;
9
11
const [ deleteSuccess , setDeleteSuccess ] = useState ( false ) ;
12
+ const user = useContext ( UserContext ) ;
13
+ const navigate = useNavigate ( ) ;
14
+
15
+ useEffect ( ( ) => {
16
+ if ( ! user ) {
17
+ navigate ( '/login' ) ;
18
+ }
19
+ } , [ user , navigate ] ) ;
10
20
11
21
const handleClickOpen = ( ) => {
12
22
setOpen ( true ) ;
13
23
} ;
14
24
15
- const handleClose = ( ) => {
25
+ const handleClose = async ( ) => {
16
26
setOpen ( false ) ;
17
- } ;
18
-
19
- const handleDeleteConfirm = ( ) => {
20
- setDeleteConfirmOpen ( true ) ;
21
- } ;
22
-
23
- const handleDeleteConfirmClose = ( ) => {
24
- setDeleteConfirmOpen ( false ) ;
25
- setDeleteSuccess ( false ) ;
27
+ if ( deleteSuccess ) {
28
+ await handleLogout ( ) ; // Log out the user
29
+ navigate ( '/login' ) ; // Redirect to login page
30
+ }
26
31
} ;
27
32
28
33
const handleDeleteProfile = async ( ) => {
@@ -37,6 +42,10 @@ const ProfileSettings = () => {
37
42
setDeleteInProgress ( false ) ;
38
43
} ;
39
44
45
+ if ( ! user ) {
46
+ return null ; // Optionally, you can return a loading spinner or a message here
47
+ }
48
+
40
49
return (
41
50
< div className = "container" >
42
51
< h2 > Profile Settings</ h2 >
0 commit comments