mirror of
https://github.com/AmanTahiliani/PeerNotes.git
synced 2026-08-07 11:55:12 -04:00
add polling
This commit is contained in:
@@ -3,11 +3,12 @@ import { Link, useNavigate } from 'react-router-dom';
|
||||
import { Outlet } from "react-router-dom";
|
||||
import { SessionContext, destroySessionCookie, getSessionCookie } from "./contexts/session";
|
||||
import { useState, useEffect, useContext } from "react";
|
||||
import { usePoll } from "./hooks/usePoll";
|
||||
|
||||
export default function App() {
|
||||
const [session, setSession] = useState(getSessionCookie());
|
||||
const navigate = useNavigate();
|
||||
|
||||
usePoll();
|
||||
// Redirect to login if session is undefined
|
||||
useEffect(
|
||||
() => {
|
||||
|
||||
33
frontend/src/hooks/usePoll.ts
Normal file
33
frontend/src/hooks/usePoll.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { useEffect } from "react"
|
||||
import { getAuthHeaders } from "../utils/getAuthHeaders"
|
||||
export const usePoll = () => {
|
||||
useEffect(() => {
|
||||
const interval = setInterval(() => {
|
||||
pollServer()
|
||||
.catch((error) => {
|
||||
console.error('Error:', error);
|
||||
});
|
||||
}, 30 * 60000) // poll every 30 minutes
|
||||
return () => {
|
||||
// clean up
|
||||
clearInterval(interval);
|
||||
};
|
||||
}, [])
|
||||
|
||||
const pollServer = async () => {
|
||||
// request private ip from local api
|
||||
return fetch('http://127.0.0.1:8080/ip')
|
||||
.then(response => response.text())
|
||||
.then((data) => {
|
||||
// send private ip to central server
|
||||
return fetch('http://localhost:8000/api/poll/', {
|
||||
method: 'POST',
|
||||
headers: getAuthHeaders(),
|
||||
body: JSON.stringify({ip: data}),
|
||||
})
|
||||
}).then(response => response.json())
|
||||
.then(data => {
|
||||
console.log("Poll response data:", data);
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user