Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
4.0 kB
0
Indexable
Never
  setBookingEvents = (bookings) => {
    let events = [];
    bookings.forEach((booking) => {
      const title =
        // this.getClassName(booking.level) +
        booking.level +
        " " +
        this.getAmPmEve(booking.start_time);

      let inst;
      let campus = this.state.campuses.find(
        (cam) => cam._id === booking.campus_id
      );

      if (!campus || !campus._id && booking?.campus) {
        campus = booking.campus
      }

      if (campus) {
        inst = this.state.institutions.find(
          (ins) => ins._id === campus.institution_id
        );
      }

      if (booking.multiple_date) {
        const dates =
          booking.dates.filter(function (dates) {
            return (
              dates.date >= booking.booking_date &&
              dates.date <= booking.end_booking_date
            );
          }) || [];
        dates.forEach((date) => {
          // use original_dates instead of dates to find the correct index of date
          const indexItem = booking.original_dates.find(item => item.date === date.date)
          const index = booking.original_dates.indexOf(indexItem)
          if (!date.cancelled_by_inst && !date.teacher_cancel_reason) {
            if (date.teacher_id === this.props.user._id) {
              const booking_sche = {
                id: `${booking._id}_${index}`,
                calendarId: 1,
                title: title,
                category: "allday",
                dueDateClass: "",
                isReadOnly: true,
                start: new Date(date.date),
                end: new Date(date.date),
                location: inst || campus
                  ? `${campus.address_line1 + (campus.address_line2 ? ', ' + campus.address_line2 : '')}, ${inst?.institution_name}`
                  : "",
                state: booking.online ? "Online" : "On Campus",
                body: JSON.stringify({
                  arrival_time: date.arrival_time
                    ? date.arrival_time
                    : booking.arrival_time,
                  start_time: date.start_time
                    ? date.start_time
                    : booking.start_time,
                  end_time: date.end_time ? date.end_time : booking.end_time,
                  isAdmin: this.props.isAdmin,
                }),
              };

              var duplication = false;
              for (let e of events) {
                if (booking._id === e.id) {
                  duplication = true;
                  break;
                }
              }
              if (!duplication) {
                events.push(booking_sche);
              }
            }
          }
        });
      } else {
        events.push({
          id: booking._id,
          calendarId: 1,
          title: title,
          category: "allday", //'milestone', 'task', allday', 'time'
          dueDateClass: "START",
          borderColor: "#fff",
          isReadOnly: true,
          start: new Date(booking.booking_date),
          end: new Date(booking.booking_date),
          location: inst
            ? `${campus.campus_shortname}, ${inst.institution_name}`
            : "",
          state: booking.online ? "Online" : "On Campus",
          body: JSON.stringify({
            arrival_time: booking.arrival_time,
            start_time: booking.start_time,
            end_time: booking.end_time,
            isAdmin: this.props.isAdmin,
          }),
        });
      }
    });

    this.setState({ gcal_events: events, originalBookings: bookings }, () => {
      let prevState = this.state.schedules;
      if (prevState) {
        const itemWithID = [...prevState, ...events].filter(item => item.id)
        const itemWithoutID = [...prevState, ...events].filter(item => !item.id)
        this.setState({ schedules: [...itemWithoutID,  ..._.uniqBy([...itemWithID],'id')] });
      } else {
        this.setState({ schedules: events });
      }
    });
    // console.log(this.state.schedules);
  };
Leave a Comment