-
Jörn Künsemöller authoredJörn Künsemöller authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
websocket.js 8.57 KiB
import _ from 'lodash';
import ScheduleService from "../services/schedule.service";
import ReservationService from "../services/reservation.service";
import useWebSocket from 'react-use-websocket';
import { getStore } from "../services/store.helper";
import UIConstants from "../utils/ui.constants";
function useWeekViewWebSocket(data, setData, summaryItem, setSummaryItem, setShowSummary) {
/**
* Function to call when websocket is connected
*/
function onConnect() {
try {
console.log("WS Opened");
const userDets = localStorage.getItem("user");
if (userDets) {
sendMessage(JSON.stringify({ "token": JSON.parse(userDets).websocket_token }));
console.log("Auth token submitted");
}
} catch (err) {
console.log('err', err);
}
}
/**
* Function to call when websocket is disconnected
*/
function onDisconnect() {
console.log("WS Closed");
}
function fetchBlueprintAndAddToTimeline(id) {
// The websocket message only contains a subset of the details we need, so fetch the full set
ScheduleService.getTimelineSlimBlueprints(undefined, undefined, id) // todo: check time
.then((response) => {
setData(prevData => ({
...prevData,
schedulingUnits: prevData.schedulingUnits.concat(response)
}));
})
.catch(e => console.error("Couldn't retrieve scheduling unit details for id: ", id, e));
}
function fetchReservationAndAddToTimeline(id) {
const shouldFetchReservations = getStore(UIConstants.STORE_KEY_TIMELINE).reservationsToggle;
if (shouldFetchReservations) {
ReservationService.getTimelineReservations(undefined, undefined, id) // todo: check time
.then((response) => {
setData(prevData => ({
...prevData,
reservations: prevData.reservations.concat(response)
}));
})
.catch(e => console.error("Couldn't retrieve reservation details for id: ", id, e));
}
}
/**
* Handles the message received through websocket
* @param {String} data - String of JSON data
*/
function handleData(event) {
const jsonData = JSON.parse(event?.data) || {};
switch (jsonData.object_type) {
case 'scheduling_unit_blueprint': {
switch (jsonData.action) {
case 'delete': {
const schedulingUnits = data.schedulingUnits