diff --git a/src/tour-operator-updates/screens/TOUpdates.tsx b/src/tour-operator-updates/screens/TOUpdates.tsx index 91c3457..0045f1e 100644 --- a/src/tour-operator-updates/screens/TOUpdates.tsx +++ b/src/tour-operator-updates/screens/TOUpdates.tsx @@ -1,43 +1,95 @@ -import {Paper, Table, TableBody, TableCell, TableContainer, TableHead, TableRow} from "@mui/material"; -import {ConnectingAirports, Hotel} from "@mui/icons-material"; -import React from "react"; +import { Paper, Table, TableBody, TableCell, TableContainer, TableHead, TableRow } from "@mui/material"; +import { ConnectingAirports, Hotel } from "@mui/icons-material"; +import React, { useEffect, useState } from "react"; + +type HotelUpdate = { + updateDateTime: string; + updateType: string; + hotelName: string; + roomName: string; + priceChange: number; + capacityChange: number; +}; + +type TransportUpdate = { + updateDateTime: string; + updateType: string; + departureRegionAndCountry: string; + arrivalRegionAndCountry: string; + transportTypeName: string; + priceChange: number; + capacityChange: number; +}; const TOUpdates = () => { - const transportChanges = [ - { - date: new Date(), - locationFrom: {idLocation: '123', region: 'Gdańsk', country: 'Polska'}, - locationTo: {idLocation: '345', region: 'Warszawa', country: 'Polska'}, - type: 'BUS', - changes: { - capacityDiff: -20, - priceDiff: -48, - } - }, - { - date: new Date(), - locationFrom: {idLocation: '124', region: 'Gdańsk', country: 'Polska'}, - locationTo: {idLocation: '345', region: 'Durres', country: 'Albania'}, - type: 'PLANE', - changes: { - capacityDiff: 34, - priceDiff: 125, + const [hotelUpdates, setHotelUpdates] = useState([]); + const [transportUpdates, setTransportUpdates] = useState([]); + + useEffect(() => { + const hotelWs = new WebSocket(`ws://localhost:8082/data-generator/ws/hotel`); + const transportWs = new WebSocket(`ws://localhost:8082/data-generator/ws/transport`); + + hotelWs.onmessage = (event) => { + console.log("Received hotel message: " + event.data); + + const messageType = event.data.split(':')[0]; + const messageData = event.data.split(': ')[1]; + + switch (messageType) { + case "SingleHotel": + const hotelUpdate = JSON.parse(messageData); + + // Convert the updateDateTime array to a Date object + hotelUpdate.updateDateTime = new Date( + hotelUpdate.updateDateTime[0], + hotelUpdate.updateDateTime[1] - 1, // JavaScript months are 0-based + hotelUpdate.updateDateTime[2], + hotelUpdate.updateDateTime[3], + hotelUpdate.updateDateTime[4] + ).toISOString(); + + setHotelUpdates((prevUpdates) => [hotelUpdate, ...prevUpdates]); + break; + default: + console.log("Unexpected message type"); + break; } - }, - ] - - const hotelChanges = [ - { - date: new Date(), - hotelName: 'Grand Fafa Blue', - roomName: 'Pokój 2 os.', - changes: { - guestCapacityDiff: 1, - priceDiff: 27, + }; + + transportWs.onmessage = (event) => { + console.log("Received transport message: " + event.data); + + const messageType = event.data.split(':')[0]; + const messageData = event.data.split(': ')[1]; + + switch (messageType) { + case "SingleTransport": + const transportUpdate = JSON.parse(messageData); + + // Convert the updateDateTime array to a Date object + transportUpdate.updateDateTime = new Date( + transportUpdate.updateDateTime[0], + transportUpdate.updateDateTime[1] - 1, // JavaScript months are 0-based + transportUpdate.updateDateTime[2], + transportUpdate.updateDateTime[3], + transportUpdate.updateDateTime[4] + ).toISOString(); + + setTransportUpdates((prevUpdates) => [transportUpdate, ...prevUpdates]); + break; + default: + console.log("Unexpected message type"); + break; } - }, - ] + }; + + return () => { + hotelWs.close(); + transportWs.close(); + console.log("WebSocket connection closed"); + }; + }, []); return (
@@ -52,25 +104,33 @@ const TOUpdates = () => { - Data + Data zmiany Skąd Dokąd Typ transportu + Typ zmiany Zmiany - {transportChanges.map((transport, index) => ( + {transportUpdates.map((transport, index) => ( - {transport.date.toDateString()} - {transport.locationFrom.country}, {transport.locationFrom.region} - {transport.locationTo.country}, {transport.locationTo.region} - {transport.type === 'PLANE' ? 'Samolot' : 'Bus'} + {new Date(transport.updateDateTime).toLocaleString()} + {transport.departureRegionAndCountry} + {transport.arrivalRegionAndCountry} + {transport.transportTypeName} + {transport.updateType} -
-

Cena: {transport.changes.priceDiff >= 0 ? '+' : ''}{transport.changes.priceDiff}

-

Miejsca: {transport.changes.capacityDiff >= 0 ? '+' : ''}{transport.changes.capacityDiff}

-
+ {transport.updateType === 'CREATE' ? ( +
+ {} +
+ ) : ( +
+

Cena: {transport.priceChange > 0 ? '+' : ''}{transport.priceChange}

+

Miejsca: {transport.capacityChange > 0 ? '+' : ''}{transport.capacityChange}

+
+ )}
))} @@ -79,9 +139,9 @@ const TOUpdates = () => { - +
- +

Aktualizacje hoteli

@@ -89,23 +149,31 @@ const TOUpdates = () => {
- Data + Data zmiany Hotel Pokój + Typ zmiany Zmiany - {hotelChanges.map((hotel, index) => ( + {hotelUpdates.map((hotel, index) => ( - {hotel.date.toDateString()} + {new Date(hotel.updateDateTime).toLocaleString()} {hotel.hotelName} {hotel.roomName} + {hotel.updateType} -
-

Cena: {hotel.changes.priceDiff >= 0 ? '+' : ''}{hotel.changes.priceDiff}

-

Miejsca: {hotel.changes.guestCapacityDiff >= 0 ? '+' : ''}{hotel.changes.guestCapacityDiff}

-
+ {hotel.updateType === 'CREATE' ? ( +
+ {} +
+ ) : ( +
+

Cena: {hotel.priceChange > 0 ? '+' : ''}{hotel.priceChange}

+

Miejsca: {hotel.capacityChange > 0 ? '+' : ''}{hotel.capacityChange}

+
+ )}
))}