1
- import React , { Component } from "react" ;
1
+ import React , { Component , useContext , useEffect , useState } from "react" ;
2
2
import Table from '@mui/material/Table' ;
3
3
import TableBody from '@mui/material/TableBody' ;
4
4
import TableCell from '@mui/material/TableCell' ;
@@ -7,75 +7,83 @@ import TableHead from '@mui/material/TableHead';
7
7
import TableRow from '@mui/material/TableRow' ;
8
8
import Paper from '@mui/material/Paper' ;
9
9
import Alert from "@mui/material/Alert" ;
10
+ import Grid from "@mui/material/Grid" ;
11
+ import { AppContext } from "./AppContext" ;
10
12
11
13
12
- class LeadList extends Component {
13
- constructor ( props ) {
14
- super ( props ) ;
15
- this . state = {
16
- data : [ ] ,
17
- error : ''
18
- } ;
19
- }
14
+ function LeadList ( props ) {
15
+ const [ data , setData ] = useState ( [ ] ) ;
16
+ const [ error , setError ] = useState ( [ ] ) ;
17
+
18
+ const appContext = useContext ( AppContext ) ;
20
19
21
- componentDidMount ( ) {
20
+ useEffect ( ( ) => {
22
21
const requestOptions = {
23
22
headers : { 'Accept' : 'application/json' } ,
24
23
} ;
25
24
26
- fetch ( "api/lead/" , requestOptions )
27
- . then ( response => {
28
- if ( response . status > 400 ) {
29
- return this . setState ( ( ) => {
30
- return { error : 'Request status: ' + response . status + ' - ' + response . statusText } ;
31
- } ) ;
32
- }
33
- return response . json ( ) ;
34
- } )
35
- . then ( data => {
36
- this . setState ( ( ) => {
37
- return {
38
- data
39
- } ;
25
+ if ( appContext . user . isAuthenticated ) {
26
+ fetch ( "api/lead/" , requestOptions )
27
+ . then ( response => {
28
+ if ( response . status > 400 ) {
29
+ return setError ( 'Request status: ' + response . status + ' - ' + response . statusText ) ;
30
+ }
31
+ return response . json ( ) ;
32
+ } )
33
+ . then ( data => {
34
+ setData ( data ) ;
40
35
} ) ;
41
- } ) ;
42
- }
36
+ }
37
+ else
38
+ {
39
+ setError ( '' ) ;
40
+ }
41
+
42
+ } , [ appContext . user . isAuthenticated ] )
43
43
44
- render ( ) {
45
- return (
46
- < >
47
- { this . state . error == '' ?
48
- < TableContainer component = { Paper } >
49
- < Table size = "small" aria-label = "Lead List" >
50
- < TableHead >
51
- < TableRow >
52
- < TableCell > Name</ TableCell >
53
- < TableCell > Email</ TableCell >
54
- < TableCell > Message</ TableCell >
55
- </ TableRow >
56
- </ TableHead >
57
- < TableBody >
58
- { this . state . data . map ( ( contact ) => (
59
- < TableRow
60
- key = { contact . name }
61
- sx = { { '&:last-child td, &:last-child th' : { border : 0 } } }
62
- >
63
- < TableCell component = "th" scope = "row" >
64
- { contact . name }
65
- </ TableCell >
66
- < TableCell > { contact . email } </ TableCell >
67
- < TableCell > { contact . message } </ TableCell >
68
- </ TableRow >
69
- ) ) }
70
- </ TableBody >
71
- </ Table >
72
- </ TableContainer >
44
+ return (
45
+ < >
46
+ { ! appContext . user . isAuthenticated ?
47
+ < >
48
+ < h1 > Login</ h1 >
49
+ < Alert severity = "error" > Please login to see the lead list.</ Alert >
50
+ </ >
73
51
:
74
- < Alert severity = "error" > { this . state . error } </ Alert >
52
+ < >
53
+ < h1 > Lead List</ h1 >
54
+ { error == '' ?
55
+ < TableContainer component = { Paper } >
56
+ < Table size = "small" aria-label = "Lead List" >
57
+ < TableHead >
58
+ < TableRow >
59
+ < TableCell > Name</ TableCell >
60
+ < TableCell > Email</ TableCell >
61
+ < TableCell > Message</ TableCell >
62
+ </ TableRow >
63
+ </ TableHead >
64
+ < TableBody >
65
+ { data . map ( ( contact ) => (
66
+ < TableRow
67
+ key = { contact . name }
68
+ sx = { { '&:last-child td, &:last-child th' : { border : 0 } } }
69
+ >
70
+ < TableCell component = "th" scope = "row" >
71
+ { contact . name }
72
+ </ TableCell >
73
+ < TableCell > { contact . email } </ TableCell >
74
+ < TableCell > { contact . message } </ TableCell >
75
+ </ TableRow >
76
+ ) ) }
77
+ </ TableBody >
78
+ </ Table >
79
+ </ TableContainer >
80
+ :
81
+ < Alert severity = "error" > { error } </ Alert >
82
+ }
83
+ </ >
75
84
}
76
- </ >
77
- ) ;
78
- }
85
+ </ >
86
+ ) ;
79
87
}
80
88
81
89
export default LeadList ;
0 commit comments