aboutsummaryrefslogtreecommitdiff
path: root/utils/schedulesUtils.js
blob: cb8c4745f4e41d165e8712f3ba183570b0478139 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Function to transform the schedule data into the desired format
export const transformSchedule = (schedule) => {
  const formattedSchedule = {};

  for (const day of Object.keys(schedule)) {
    formattedSchedule[day] = {};

    for (const scheduleItem of schedule[day]) {
      const time = scheduleItem.airingAt;

      if (!formattedSchedule[day][time]) {
        formattedSchedule[day][time] = [];
      }

      formattedSchedule[day][time].push(scheduleItem);
    }
  }

  return formattedSchedule;
};

export const sortScheduleByDay = (schedule) => {
  const daysOfWeek = [
    "Saturday",
    "Sunday",
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
  ];

  // Get the current day of the week (0 = Sunday, 1 = Monday, ...)
  const currentDay = new Date().getDay();

  // Reorder days of the week to start with today
  const orderedDays = [
    ...daysOfWeek.slice(currentDay),
    ...daysOfWeek.slice(0, currentDay),
  ];

  // Create a new object with sorted days
  const sortedSchedule = {};
  orderedDays.forEach((day) => {
    if (schedule[day]) {
      sortedSchedule[day] = schedule[day];
    }
  });

  return sortedSchedule;
};

export const filterScheduleByDay = (sortedSchedule, filterDay) => {
  if (filterDay === "All") return sortedSchedule;
  // Create a new object to store the filtered schedules
  const filteredSchedule = {};

  // Iterate through the keys (days) in sortedSchedule
  for (const day in sortedSchedule) {
    // Check if the current day matches the filterDay
    if (day === filterDay) {
      // If it matches, add the schedules for that day to the filteredSchedule object
      filteredSchedule[day] = sortedSchedule[day];
    }
  }

  // Return the filtered schedule
  return filteredSchedule;
};

export const filterFormattedSchedule = (formattedSchedule, filterDay) => {
  if (filterDay === "All") return formattedSchedule;

  // Check if the selected day exists in the formattedSchedule
  if (formattedSchedule.hasOwnProperty(filterDay)) {
    return {
      [filterDay]: formattedSchedule[filterDay],
    };
  }

  // If the selected day does not exist, return an empty object
  return {};
};