slot on click

 avatar
Satyam
plain_text
2 months ago
2.8 kB
4
Indexable
const onSlotItemClick = (item: SlotsProps, index: number) => {
    let isValid = false;
    let temp: any = [...bookSlotState?.markedDates];
    let tempSlots = [...bookSlotState?.selectedSlot];
    temp?.map((key: MarkedDataProps, keyIndex: number) => {
      if (
        moment(key?.date).format('DD-MM-YYYY') ==
        moment(bookSlotState?.selectedDate).format('DD-MM-YYYYY')
      ) {
        key?.slots?.map((val: SlotsDataProps, i: number) => {
          if (val?.uniqueId === item?.uniqueId) {
            isValid = true;
          }
        });
      }
    });
    if (!(bookSlotState?.slotSizeData?.length === 1)) {
      if (isValid) {
        Toast(`Slot already selected for this date`);
        return;
      }
    }

    if (!tempSlots.some(value => value.uniqueId == item.uniqueId)) {
      tempSlots.push({
        slotId: item?.SlotID,
        uniqueId: item?.uniqueId,
        date: bookSlotState.selectedDate,
      });
      setBookSlotState('selectedSlot', tempSlots);
    } else {
      const index = tempSlots.findIndex(val => val.uniqueId === item.uniqueId);
      console.log('index  satyam::', index);
      tempSlots.splice(index, 1);
      setBookSlotState('selectedSlot', tempSlots);
    }
    let finalSlots: Array<SlotsDataProps> = [];
    bookSlotState?.slotData.map((item: MarkedDataProps) => {
      item.slots.map((val: SlotsDataProps) => {
        if (tempSlots.some(value => value.uniqueId == val.uniqueId)) {
          finalSlots.push(val);
        }
      });
    });
    let finalCategory = bookSlotState?.slotSizeData?.filter(
      (item: ServicesProps) =>
        item.ServiceID == bookSlotState?.categorySelection[0],
    );

    if (tempSlots.length == 0) {
      temp.splice(0, temp.length);
      setBookSlotState('markedDates', temp);
    } else {
      let filter = temp.filter(
        (item: MarkedDataProps) =>
          item.category == finalCategory[0]?.ServiceID &&
          moment(item.date).format('DD-MM-YYYY') ==
            moment(bookSlotState?.selectedDate).format('DD-MM-YYYY'),
      );
      if (filter.length == 0) {
        temp.push({
          date: bookSlotState?.selectedDate,
          dots: [{color: 'white'}],
          slots: finalSlots,
          price: finalCategory[0]?.Price,
          category: finalCategory[0]?.ServiceID,
        });
        setBookSlotState('markedDates', temp);
      } else {
        let index = temp.findIndex(
          (val: MarkedDataProps) =>
            val.category == finalCategory[0]?.ServiceID &&
            moment(val.date).format('DD-MM-YYYY') ==
              moment(bookSlotState?.selectedDate).format('DD-MM-YYYY'),
        );
        temp[index].slots = finalSlots;
        setBookSlotState('markedDates', temp);
      }
    }
  };
Editor is loading...
Leave a Comment