Untitled

mail@pastecode.io avatar
unknown
plain_text
7 days ago
4.1 kB
2
Indexable
Never
"use client";
import { useEffect, useState } from "react";
import { io } from "socket.io-client";
import { IDatePicker } from "../../../../../types";

let intervalId: NodeJS.Timeout;

const socket = io(process.env.NEXT_PUBLIC_SOCKET_URL!, {
  withCredentials: true
});

function toISOString(date: any) {
  return new Date(new Date(date).setUTCHours(0, 0, 0, 0)).toISOString();
}

export default function useBooking() {
  const [selectedRoomIndex, setSelectedRoomIndex] = useState<number>(0);
  const [tempHoldRooms, setTempHoldRooms] = useState<any>();
  const [availableRoom, setAvailableRoom] = useState<any>();
  const [holdData, setHoldData] = useState<any>();
  const currentDate = new Date(new Date(new Date()).setUTCHours(0, 0, 0, 0)).toISOString();
  const [pickerDate, setPickerDate] = useState<IDatePicker>({
    startDate: new Date().toISOString(),
    endDate: new Date(new Date(currentDate).getTime() + 24 * 60 * 60 * 1000).toISOString()
  });

  useEffect(() => {
    socket.on("connect", () => {});

    socket.on("hold", (data: any) => {
      if (data && toISOString(pickerDate.startDate) === data.date) {
        // Extract own hold IDs
        const ownHoldIds = new Set(data.ownHold.map((hold: any) => hold.id));

        // Get all hold IDs
        const totalHoldIds = new Set(data.totalHolds);

        // Determine holds that are not owned by the current user
        const notOwnHolds = Array.from(totalHoldIds).filter(holdId => !ownHoldIds.has(holdId));

        // Update available rooms
        setAvailableRoom((prevRooms: any) =>
          prevRooms.map((room: any) => ({
            ...room,
            hold: notOwnHolds.includes(room.id)
          }))
        );
      }
    });

    socket.on("room-status", (data: any) => {
      console.log("room-status", data);

      if (data && toISOString(pickerDate.startDate) === data.date) {
        // Extract own hold IDs
        const ownHoldIds = new Set(data.ownHold.map((hold: any) => hold.id));

        // Determine holds that are not owned by the current user
        const notOwnHoldIds = new Set(data.totalHolds.filter((id: any) => !ownHoldIds.has(id)));

        setAvailableRoom((prevRooms: any) =>
          prevRooms?.map((room: any) => {
            const isNotOwnHold = notOwnHoldIds.has(room.id);
            const isOwnHold = ownHoldIds.has(room.id);

            return {
              ...room,
              hold: isNotOwnHold,
              selected: isOwnHold
            };
          })
        );
      }
    });

    socket.on("booking", (data: any) => {
      console.log("bookingEvent", data);
    });

    intervalId = setInterval(() => {
      socket.emit("status", { date: toISOString(pickerDate.startDate) });
    }, 3000);

    return () => {
      socket.off("connect");
      socket.off("hold");
      socket.off("booking");
      socket.off("room-status");
      clearInterval(intervalId);
    };
  }, []);

  // const handleImageClick = (id: number, index: number) => {
  //   setAvailableRoom((prevRooms: any) => prevRooms?.map((room: any) => (room.id === id ? { ...room, selected: !room.selected } : room)));
  //   console.log(id, "id");
  //   setSelectedRoomIndex(id);
  //   socket.emit("hold", {
  //     date: toISOString(pickerDate.startDate),
  //     rooms: [id]
  //   });
  // };

  const handleImageClick = (id: number, index: number) => {
    setAvailableRoom((prevRooms: any) => prevRooms?.map((room: any) => (room.id === id ? { ...room, selected: !room.selected } : room)));
    setSelectedRoomIndex(id);
    socket.emit("hold", {
      date: toISOString(pickerDate.startDate),
      room: id
    });
  };

  console.log(availableRoom, "availableRoom");

  return {
    socket,
    availableRoom,
    setAvailableRoom,
    holdData,
    setHoldData,
    pickerDate,
    setPickerDate,
    toISOString,
    handleImageClick,
    tempHoldRooms,
    setTempHoldRooms,
    setSelectedRoomIndex,
    selectedRoomIndex
  };
}
Leave a Comment