11'use client'
22
3- import React , { useEffect , useState } from 'react'
3+ import React from 'react'
44import Link from 'next/link'
55import { Post } from '@prisma/client'
66import 'animate.css'
7+ import SubscribeButton from './SubscribeButton'
78
89type Props = {
910 posts : Post [ ]
@@ -13,96 +14,9 @@ type Props = {
1314}
1415
1516const PostsTable : React . FC < Props > = ( { posts, totalPosts, postsPerPage, currentPage } ) => {
16- const [ isSubscribed , setIsSubscribed ] = useState ( false )
17-
18- useEffect ( ( ) => {
19- if ( 'serviceWorker' in navigator ) {
20- navigator . serviceWorker
21- . register ( '/sw.js' )
22- . then ( ( registration ) => console . log ( 'Service Worker registered:' , registration ) )
23- . catch ( ( error ) => console . error ( 'Service Worker registration failed:' , error ) )
24- }
25- } , [ ] )
26-
27- useEffect ( ( ) => {
28- // Check subscription status on component mount
29- const checkSubscriptionStatus = async ( ) => {
30- if ( 'serviceWorker' in navigator && 'PushManager' in window ) {
31- const registration = await navigator . serviceWorker . ready
32- const subscription = await registration . pushManager . getSubscription ( )
33- setIsSubscribed ( ! ! subscription ) // Set subscription status
34- }
35- }
36- checkSubscriptionStatus ( )
37- } , [ ] )
38-
39- const handleSubscribe = async ( ) => {
40- if ( 'serviceWorker' in navigator && 'PushManager' in window ) {
41- try {
42- const registration = await navigator . serviceWorker . ready
43-
44- // Fetch the public VAPID key from the server
45- const response = await fetch ( '/api/notifications/get-public-key' )
46- if ( ! response . ok ) throw new Error ( 'Failed to fetch public key' )
47- const { publicKey } = await response . json ( )
48-
49- // Convert VAPID key to the format required by PushManager
50- const convertedVapidKey = urlBase64ToUint8Array ( publicKey )
51-
52- // Subscribe the user to push notifications
53- const subscription = await registration . pushManager . subscribe ( {
54- userVisibleOnly : true ,
55- applicationServerKey : convertedVapidKey ,
56- } )
57-
58- // Send the subscription to the server
59- const subscribeResponse = await fetch ( '/api/notifications/subscribe' , {
60- method : 'POST' ,
61- body : JSON . stringify ( subscription ) ,
62- headers : {
63- 'Content-Type' : 'application/json' ,
64- } ,
65- } )
66- if ( ! subscribeResponse . ok ) throw new Error ( 'Failed to save subscription on server' )
67-
68- alert ( 'Successfully subscribed to notifications!' )
69- setIsSubscribed ( true )
70- } catch ( error ) {
71- console . error ( 'Error during subscription:' , error )
72- }
73- } else {
74- alert ( 'Push messaging is not supported in your browser.' )
75- }
76- }
77-
78- // Helper function to convert the VAPID key
79- function urlBase64ToUint8Array ( base64String : string ) {
80- if ( ! base64String ) {
81- throw new Error ( 'base64String is undefined or null' )
82- }
83- const padding = '=' . repeat ( ( 4 - ( base64String . length % 4 ) ) % 4 )
84- const base64 = ( base64String + padding ) . replace ( / \- / g, '+' ) . replace ( / _ / g, '/' )
85- const rawData = atob ( base64 )
86- const outputArray = new Uint8Array ( rawData . length )
87- for ( let i = 0 ; i < rawData . length ; ++ i ) {
88- outputArray [ i ] = rawData . charCodeAt ( i )
89- }
90- return outputArray
91- }
92-
9317 return (
9418 < div className = "animate__animated animate__fadeIn" >
95- < button
96- onClick = { handleSubscribe }
97- disabled = { isSubscribed }
98- className = { `mb-4 px-4 py-2 rounded-lg transition duration-300 ease-in-out animate__animated animate__pulse ${
99- isSubscribed
100- ? 'bg-gray-400 cursor-not-allowed'
101- : 'bg-blue-600 hover:bg-blue-700 text-white'
102- } `}
103- >
104- { isSubscribed ? 'Already Subscribed' : 'Subscribe to Notifications' }
105- </ button >
19+ < SubscribeButton />
10620 < div className = "overflow-x-auto py-4" >
10721 < table className = "min-w-full bg-white shadow-md rounded-lg overflow-hidden" >
10822 < thead className = "bg-blue-600 text-white" >
0 commit comments