From 8c36e67925b9aba190962c798224e98cf253d643 Mon Sep 17 00:00:00 2001 From: "Kommandoposten\\Dunkstormen" Date: Sun, 15 Dec 2024 01:42:47 +0100 Subject: [PATCH 01/15] refactor: New BookingComponent --- src/components/BookingComponent.jsx | 154 +++++++++++++++++++++++++--- 1 file changed, 137 insertions(+), 17 deletions(-) diff --git a/src/components/BookingComponent.jsx b/src/components/BookingComponent.jsx index 23e1a30..14bde50 100644 --- a/src/components/BookingComponent.jsx +++ b/src/components/BookingComponent.jsx @@ -1,57 +1,177 @@ import React, { useEffect, useState } from "react"; +import moment from "moment"; +import { getBookableCallsign, parseControlCenterDate, bookingType, formatAsZulu } from "../utils/BookingHelper"; +import { ExternalLinkIcon } from './icons/ExternalLinkIcon'; const BookingComponent = () => { const [ControlCenterBookings, setControlCenterBookings] = useState([]); - const [ControlCenterPositions, setControlCenterPositions] = useState([]); const [VatsimNetworkSessions, setVatsimNetworkSessions] = useState([]); + const [dateArray, setDateArray] = useState([]); const [loading, setLoading] = useState(true); + const acceptedFIRsRegex = /((EK[A-Z][A-Z]_)|(EF[A-Z][A-Z]_)|(BI[A-Z][A-Z]_)|(EN[A-Z][A-Z]_)|(ES[A-Z][A-Z]_))\w+/i; + const mentorRegex = /((_X_)|(_X\d_)|(_M_))\w+/i; + /** * Fetch data */ const fetchComponentData = async () => { try { - let [ControlCenterBookingsData, ControlCenterPositionsData, VatsimNetworkSessionsData] = await Promise.all([ + let [ControlCenterBookingsData, VatsimNetworkSessionsData] = await Promise.all([ fetch('https://cc.vatsim-scandinavia.org/api/bookings'), - fetch('https://cc.vatsim-scandinavia.org/api/positions'), fetch('https://data.vatsim.net/v3/vatsim-data.json'), ]); ControlCenterBookingsData = await ControlCenterBookingsData.json(); - ControlCenterPositionsData = await ControlCenterPositionsData.json(); VatsimNetworkSessionsData = await VatsimNetworkSessionsData.json(); setControlCenterBookings(ControlCenterBookingsData); - setControlCenterPositions(ControlCenterPositionsData); setVatsimNetworkSessions(VatsimNetworkSessionsData); } catch (error) { console.log('Error while fetching data:', error); } } + + /** + * Process data + */ + const processData = async () => { + const ControlCenterBookingsData = ControlCenterBookings.data; + const VatsimNetworkSessionsData = VatsimNetworkSessions.controllers; + const localDateArray = []; - useEffect(() => { - const fetchData = async () => { - await Promise.all([ - fetchComponentData(), - ]); - setLoading(false); + const startDate = moment(); + const endDate = moment().add(6, 'days'); + + // Create initial localDateArray + for (let d = startDate; d <= endDate; d.add(1, 'days')) { + const dateString = d.format('YYYY-MM-DD'); + + localDateArray.push({ + date: dateString, + data: [] + }) } - fetchData(); + // Insert CC bookings into localDateArray + ControlCenterBookingsData.forEach(booking => { + const bookingDate = moment(booking.time_start).format('YYYY-MM-DD'); + const dateIndex = localDateArray.findIndex(dateObj => dateObj.date === bookingDate); + + if (dateIndex !== -1) { + localDateArray[dateIndex].data.push(booking); + } + }); + + // Match online sessions to bookings + VatsimNetworkSessionsData.forEach(session => { + if (acceptedFIRsRegex.test(session.callsign) && !mentorRegex.test(session.callsign)) { + const normalizeCallsign = (callsign) => callsign.replace(/_+/g, "_"); + const correctedCallsign = getBookableCallsign(normalizeCallsign(session.callsign), session.frequency); + + session.callsign = correctedCallsign; - const interval = setInterval(fetchData, 60000); + const existingBooking = localDateArray[0].data.find(booking => booking.callsign === session.callsign); + // If booking exists for the session merge them. + if(existingBooking && parseControlCenterDate(existingBooking.time_start).valueOf() < moment().valueOf()) { + existingBooking.name = session.name; + existingBooking.logon_time = session.logon_time; + } else { + localDateArray[0].data.push({ + ...session, + name: session.name + }) + } + } + }) + + // Sorting localDateArray by either logon time or booking start time. + localDateArray[0].data.sort((a, b) => { + const aTime = parseControlCenterDate(a.time_start || a.logon_time); + const bTime = parseControlCenterDate(b.time_start || b.logon_time); + + return aTime - bTime; + }); + + setDateArray(localDateArray); + setLoading(false); + } + + useEffect(() => { + // Fetches data and starts intercal of 1 min for data pulling. + const interval = setInterval(fetchComponentData, 60000); + fetchComponentData(); return () => clearInterval(interval); }, []); + useEffect(() => { + // When data is fetched and states for CC Bookings and/or VATSIM updates. + if (!ControlCenterBookings.data) { return } // Make sure there is data to be processed otherwise exit + + processData(); + }, [ControlCenterBookings, VatsimNetworkSessions]) + return ( - - - +
+ + {loading ? ( + +
+