11import React , { useState , useEffect } from "react" ;
22import axios from "axios" ;
33import { usePage , InertiaLink } from "@inertiajs/inertia-react" ;
4- import { useToast } from "@chakra-ui/core" ;
54
65import { NotificationType } from "@/utils/types" ;
76import Notification from "@/components/Notification" ;
7+ import NotifyAlert from "@/components/NotifyAlert" ;
88
99export default ( ) => {
1010 const [ isOpen , setIsOpen ] = useState ( false ) ;
1111 const [ notifications , setNotifications ] = useState < NotificationType [ ] > ( [ ] ) ;
1212 const { auth : { user } } = usePage ( ) ;
13- const toast = useToast ( ) ;
13+ const [ message , setMessage ] = useState ( "" ) ;
14+ const [ notify , setNotify ] = useState ( false ) ;
1415
1516 useEffect ( ( ) => {
1617 setNotifications ( user . notifications ) ;
@@ -19,85 +20,80 @@ export default () => {
1920 function readNotification ( e : React . SyntheticEvent , id : number ) {
2021 e . preventDefault ( ) ;
2122 axios . delete ( `/notifications/${ id } ` ) . then ( ( response ) => {
22- toast ( {
23- position : `bottom-right` ,
24- description : `Notification marquée comme lue.` ,
25- status : `success` ,
26- duration : 2500 ,
27- isClosable : true ,
28- } ) ;
23+ setNotify ( true ) ;
24+ setMessage ( "Notification marquée comme lue." ) ;
25+ setTimeout ( ( ) => setNotify ( false ) , 2500 ) ;
2926 setNotifications ( response . data . notifications ) ;
3027 } ) . catch ( ( error ) => {
31- toast ( {
32- position : `bottom-right` ,
33- description : `Impossible de marquée cette notification comme lue.` ,
34- status : `error` ,
35- duration : 2500 ,
36- isClosable : true ,
37- } ) ;
28+ setNotify ( true ) ;
29+ setMessage ( "Impossible de marquée cette notification comme lue." ) ;
30+ setTimeout ( ( ) => setNotify ( false ) , 2500 ) ;
3831 console . error ( error ) ;
3932 } ) ;
4033 }
4134
4235 return (
43- < div className = "relative" >
44- < InertiaLink
45- // onClick={() => setIsOpen(!isOpen)}
46- href = "/notifications"
47- className = "relative inline-flex p-1 border-2 border-transparent text-gray-600 rounded-full hover:text-gray-700 focus:outline-none focus:text-gray-700 focus:bg-gray-100 transition duration-150 ease-in-out z-10"
48- >
49- < svg className = "h-7 w-7" stroke = "currentColor" fill = "none" viewBox = "0 0 24 24" >
50- < path strokeLinecap = "round" strokeLinejoin = "round" strokeWidth = "2" d = "M15 17h5l-1.405-1.405A2.032 2.032 0 0118 14.158V11a6.002 6.002 0 00-4-5.659V5a2 2 0 10-4 0v.341C7.67 6.165 6 8.388 6 11v3.159c0 .538-.214 1.055-.595 1.436L4 17h5m6 0v1a3 3 0 11-6 0v-1m6 0H9" />
51- </ svg >
52- { notifications . length > 0 && (
53- < span
54- className = "absolute h-5 w-5 flex items-center justify-center bg-brand-primary rounded-full text-white text-xs"
55- style = { { top : "-5px" , right : "-4px" } }
56- >
57- { notifications . length }
58- </ span >
59- ) }
60- </ InertiaLink >
61- < button
62- onClick = { ( ) => setIsOpen ( false ) }
63- className = { `fixed w-full h-full inset-0 bg-transparent cursor-default ${ isOpen ? "" : "hidden" } ` }
64- tabIndex = { - 1 }
65- aria-label = "Menu"
66- />
67- < div
68- className = { `absolute right-0 rounded bg-white w-96 h-100 shadow-lg mt-2 ${ isOpen ? "" : "hidden" } ` }
69- >
70- < div className = "p-3 bg-gray-50 text-gray-700 border-b border-gray-100 flex items-center" >
71- < svg fill = "none" viewBox = "0 0 24 24" stroke = "currentColor" className = "w-6 h-6 mr-2" >
36+ < >
37+ < div className = "relative" >
38+ < InertiaLink
39+ // onClick={() => setIsOpen(!isOpen)}
40+ href = "/notifications"
41+ className = "relative inline-flex p-1 border-2 border-transparent text-gray-600 rounded-full hover:text-gray-700 focus:outline-none focus:text-gray-700 focus:bg-gray-100 transition duration-150 ease-in-out z-10"
42+ >
43+ < svg className = "h-7 w-7" stroke = "currentColor" fill = "none" viewBox = "0 0 24 24" >
7244 < path strokeLinecap = "round" strokeLinejoin = "round" strokeWidth = "2" d = "M15 17h5l-1.405-1.405A2.032 2.032 0 0118 14.158V11a6.002 6.002 0 00-4-5.659V5a2 2 0 10-4 0v.341C7.67 6.165 6 8.388 6 11v3.159c0 .538-.214 1.055-.595 1.436L4 17h5m6 0v1a3 3 0 11-6 0v-1m6 0H9" />
7345 </ svg >
74- < span className = "font-medium" > Notifications</ span >
75- </ div >
76- < div id = "notifications" className = "overflow-y-auto h-80" >
77- {
78- notifications . length > 0
79- && notifications . map ( ( notification : NotificationType ) => (
80- < Notification
81- key = { notification . id }
82- notification = { notification }
83- readNotification = { readNotification }
84- />
85- ) )
86- }
87-
88- { notifications . length === 0 && (
89- < div className = "h-80 flex flex-col items-center justify-center" >
90- < svg fill = "none" viewBox = "0 0 24 24" stroke = "currentColor" className = "w-10 h-10 mb-4" >
91- < path strokeLinecap = "round" strokeLinejoin = "round" strokeWidth = "2" d = "M15 17h5l-1.405-1.405A2.032 2.032 0 0118 14.158V11a6.002 6.002 0 00-4-5.659V5a2 2 0 10-4 0v.341C7.67 6.165 6 8.388 6 11v3.159c0 .538-.214 1.055-.595 1.436L4 17h5m6 0v1a3 3 0 11-6 0v-1m6 0H9" />
92- </ svg >
93- < small className = "text-gray-500 text-lg" > Aucune notification</ small >
94- </ div >
46+ { notifications . length > 0 && (
47+ < span
48+ className = "absolute h-5 w-5 flex items-center justify-center bg-brand-primary rounded-full text-white text-xs"
49+ style = { { top : "-5px" , right : "-4px" } }
50+ >
51+ { notifications . length }
52+ </ span >
9553 ) }
96- </ div >
97- < div className = "p-3 bg-gray-50 text-gray-700 border-b border-gray-100 text-center" >
98- < a href = "/notifications" className = "font-medium" > Toutes les notifications</ a >
54+ </ InertiaLink >
55+ < button
56+ onClick = { ( ) => setIsOpen ( false ) }
57+ className = { `fixed w-full h-full inset-0 bg-transparent cursor-default ${ isOpen ? "" : "hidden" } ` }
58+ tabIndex = { - 1 }
59+ aria-label = "Menu"
60+ />
61+ < div
62+ className = { `absolute right-0 rounded bg-white w-96 h-100 shadow-lg mt-2 ${ isOpen ? "" : "hidden" } ` }
63+ >
64+ < div className = "p-3 bg-gray-50 text-gray-700 border-b border-gray-100 flex items-center" >
65+ < svg fill = "none" viewBox = "0 0 24 24" stroke = "currentColor" className = "w-6 h-6 mr-2" >
66+ < path strokeLinecap = "round" strokeLinejoin = "round" strokeWidth = "2" d = "M15 17h5l-1.405-1.405A2.032 2.032 0 0118 14.158V11a6.002 6.002 0 00-4-5.659V5a2 2 0 10-4 0v.341C7.67 6.165 6 8.388 6 11v3.159c0 .538-.214 1.055-.595 1.436L4 17h5m6 0v1a3 3 0 11-6 0v-1m6 0H9" />
67+ </ svg >
68+ < span className = "font-medium" > Notifications</ span >
69+ </ div >
70+ < div id = "notifications" className = "overflow-y-auto h-80" >
71+ {
72+ notifications . length > 0
73+ && notifications . map ( ( notification : NotificationType ) => (
74+ < Notification
75+ key = { notification . id }
76+ notification = { notification }
77+ readNotification = { readNotification }
78+ />
79+ ) )
80+ }
81+
82+ { notifications . length === 0 && (
83+ < div className = "h-80 flex flex-col items-center justify-center" >
84+ < svg fill = "none" viewBox = "0 0 24 24" stroke = "currentColor" className = "w-10 h-10 mb-4" >
85+ < path strokeLinecap = "round" strokeLinejoin = "round" strokeWidth = "2" d = "M15 17h5l-1.405-1.405A2.032 2.032 0 0118 14.158V11a6.002 6.002 0 00-4-5.659V5a2 2 0 10-4 0v.341C7.67 6.165 6 8.388 6 11v3.159c0 .538-.214 1.055-.595 1.436L4 17h5m6 0v1a3 3 0 11-6 0v-1m6 0H9" />
86+ </ svg >
87+ < small className = "text-gray-500 text-lg" > Aucune notification</ small >
88+ </ div >
89+ ) }
90+ </ div >
91+ < div className = "p-3 bg-gray-50 text-gray-700 border-b border-gray-100 text-center" >
92+ < a href = "/notifications" className = "font-medium" > Toutes les notifications</ a >
93+ </ div >
9994 </ div >
10095 </ div >
101- </ div >
96+ < NotifyAlert show = { notify } onClose = { ( ) => setNotify ( false ) } message = { message } />
97+ </ >
10298 ) ;
10399} ;
0 commit comments