@@ -5,12 +5,18 @@ import Grid from "@material-ui/core/Grid";
5
5
import { Card } from "./Card/index.js" ;
6
6
import style from "./contactus.module.scss" ;
7
7
import Loader from "../../../../components/util/Loader" ;
8
+ import { SimpleToast } from "../../../../components/util/Toast/Toast.jsx" ;
8
9
9
10
export function Contact ( ) {
10
11
const [ contactUsData , setContactUsData ] = useState ( [ ] ) ;
11
12
const [ isLoaded , setIsLoaded ] = useState ( false ) ;
13
+ const [ toast , setToast ] = useState ( {
14
+ toastStatus : false ,
15
+ toastType : "" ,
16
+ toastMessage : "" ,
17
+ } ) ;
12
18
const fetchJoinUs = async ( ) => {
13
- const response = await fetch ( `${ END_POINT } /getContactUs ` , {
19
+ const response = await fetch ( `${ END_POINT } /contactus/getcontactus ` , {
14
20
method : "GET" ,
15
21
headers : {
16
22
"Content-Type" : "application/json" ,
@@ -21,24 +27,68 @@ export function Contact() {
21
27
setContactUsData ( data . ContactUs ) ;
22
28
setIsLoaded ( false ) ;
23
29
} ;
30
+ const handleCloseToast = ( event , reason ) => {
31
+ if ( reason === "clickaway" ) {
32
+ return ;
33
+ }
34
+ setToast ( { ...toast , toastStatus : false } ) ;
35
+ } ;
36
+ const handleDelete = async ( id ) => {
37
+ try {
38
+ const url = `${ END_POINT } /contactus/deleteContactUs` ;
39
+ const response = await fetch ( url , {
40
+ method : "DELETE" ,
41
+ headers : {
42
+ "Content-Type" : "application/json" ,
43
+ Authorization : `Bearer ${ localStorage . getItem ( "token" ) } ` ,
44
+ } ,
45
+ body : JSON . stringify ( { contactUsId : id } ) ,
46
+ } ) ;
47
+
48
+ const data = await response . json ( ) ;
49
+ setToast ( {
50
+ ...toast ,
51
+ toastMessage : "Successfully deleted!" ,
52
+ toastStatus : true ,
53
+ toastType : "success" ,
54
+ } ) ;
55
+ fetchJoinUs ( )
56
+ } catch ( error ) {
57
+ console . log ( error ) ;
58
+ setToast ( {
59
+ ...toast ,
60
+ toastMessage : "Unable to delete!" ,
61
+ toastStatus : true ,
62
+ toastType : "error" ,
63
+ } ) ;
64
+ }
65
+ } ;
24
66
useEffect ( ( ) => {
25
67
setIsLoaded ( true ) ;
26
68
fetchJoinUs ( ) ;
27
69
} , [ ] ) ;
28
70
return (
29
71
< div >
30
72
< h1 style = { { textAlign : "center" } } > Contact Us </ h1 >
31
- < div className = { style [ "data-loader" ] } > { isLoaded ? < Loader /> : null } </ div >
73
+ { isLoaded ? < div className = { style [ "data-loader" ] } > < Loader /> </ div > :
32
74
< div className = { style [ "card-container" ] } >
33
75
< Grid container spacing = { 2 } >
34
76
< Grid item >
35
77
{ contactUsData &&
36
78
contactUsData . map ( ( data ) => {
37
- return < Card key = { data . _id } content = { data } /> ;
79
+ return < Card key = { data . _id } id = { data . _id } handleDelete = { handleDelete } content = { data } /> ;
38
80
} ) }
39
81
</ Grid >
40
82
</ Grid >
41
- </ div >
83
+ </ div > }
84
+ { toast . toastStatus && (
85
+ < SimpleToast
86
+ open = { toast . toastStatus }
87
+ message = { toast . toastMessage }
88
+ handleCloseToast = { handleCloseToast }
89
+ severity = { toast . toastType }
90
+ />
91
+ ) }
42
92
</ div >
43
93
) ;
44
94
}
0 commit comments