source: sources/client/src/hooks/useGetSessions.js

Last change on this file was bc20307, checked in by Tasevski2 <39170279+Tasevski2@…>, 2 years ago

Push before video

  • Property mode set to 100644
File size: 1.1 KB
Line 
1import { useState, useEffect, useContext } from 'react';
2import axios from 'axios';
3import { AccessoriesContext } from '../context/AccessoriesContext';
4
5// THIS HOOK SHOULD LISTEN ON SOME PORT FOR CHANGES
6
7const useGetSessions = (zoneId) => {
8 const [data, setData] = useState([]);
9 const [isLoading, setIsLoading] = useState(true);
10 const { setAlert } = useContext(AccessoriesContext);
11 const fetchData = async () => {
12 await axios
13 .get(`/parkingSession/${zoneId}`)
14 .then((res) => {
15 setData(res.data);
16 })
17 .catch((err) => {
18 // ALERT FOR ERROR WITH ERROR MSG
19 setAlert({
20 type: 'error',
21 msg: 'Проблеми со серверот!', // TODO change msg to err.message
22 });
23 })
24 .finally(() => {
25 setIsLoading(false);
26 });
27 };
28 useEffect(() => {
29 fetchData();
30 const fetchInterval = setInterval(fetchData, 30000);
31 return () => {
32 clearInterval(fetchInterval);
33 };
34 }, []);
35
36 return {
37 data,
38 isLoading,
39 setData
40 };
41};
42
43export default useGetSessions;
Note: See TracBrowser for help on using the repository browser.