All files / pages/PersonalSchedules PersonalSchedulesDetailsPage.js

89.58% Statements 43/48
73.52% Branches 25/34
100% Functions 14/14
100% Lines 43/43

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346                                  1x                   1x                                           38x 38x   37x                     37x                   37x 16x 16x 6x 6x 6x 6x 6x 6x   10x 10x         37x 37x 29x                   29x 12x 8x   8x 8x   8x 8x 8x 8x   8x   38x   38x                                                             12x         37x 37x         37x                                                                     259x           266x   8x                                                                                                                   666x                 259x     4403x                                 1x                                                                                                                
import BasicLayout from "main/layouts/BasicLayout/BasicLayout";
import { useParams } from "react-router-dom";
import PersonalSchedulesTable from "main/components/PersonalSchedules/PersonalSchedulesTable";
import PersonalSectionsTable from "main/components/PersonalSections/PersonalSectionsTable";
import { useBackend, _useBackendMutation } from "main/utils/useBackend";
import {
  Button,
  Card,
  OverlayTrigger,
  Popover,
  Container,
  Row,
  Col,
} from "react-bootstrap";
import { useCurrentUser } from "main/utils/currentUser";
import React, { useEffect, useState } from "react";
 
const daysOfWeek = [
  "Monday",
  "Tuesday",
  "Wednesday",
  "Thursday",
  "Friday",
  "Saturday",
  "Sunday",
];
 
const hours = [
  "",
  "6 AM",
  "7 AM",
  "8 AM",
  "9 AM",
  "10 AM",
  "11 AM",
  "12 PM",
  "1 PM",
  "2 PM",
  "3 PM",
  "4 PM",
  "5 PM",
  "6 PM",
  "7 PM",
  "8 PM",
  "9 PM",
  "10 PM",
];
 
export default function PersonalSchedulesDetailsPage() {
  let { id } = useParams();
  const { data: currentUser } = useCurrentUser();
 
  const { data: personalSchedule } = useBackend(
    // Stryker disable all : hard to test for query caching
    [`/api/personalschedules?id=${id}`],
    {
      // Stryker disable next-line all : GET is the default, so changing this to "" doesn't introduce a bug
      method: "GET",
      url: `/api/personalschedules?id=${id}`,
      params: { id },
    },
  );
 
  const { data: personalSection } = useBackend(
    // Stryker disable all : hard to test for query caching
    [`/api/personalSections/all?psId=${id}`],
    {
      method: "GET",
      url: `/api/personalSections/all?psId=${id}`,
      params: { id },
    },
  );
 
  const convertTimeToMinutes = (time) => {
    Iif (!time) return 0;
    if (time.includes("AM") || time.includes("PM")) {
      const timeStr = time.replace(/\s/g, "");
      const [timePart, modifier] = [timeStr.slice(0, -2), timeStr.slice(-2)];
      let [hours, minutes] = timePart.split(":").map(Number);
      Iif (modifier === "PM" && hours !== 12) hours += 12;
      Iif (modifier === "AM" && hours === 12) hours = 0;
      return hours * 60 + minutes;
    } else {
      const [hours, minutes] = time.split(":").map(Number);
      return hours * 60 + minutes;
    }
  };
 
  // Stryker disable all : hard to test for specific styles
  const [eventStyles, setEventStyles] = useState([]);
  useEffect(() => {
    const dayMap = {
      M: "Monday",
      T: "Tuesday",
      W: "Wednesday",
      R: "Thursday",
      F: "Friday",
      S: "Saturday",
      U: "Sunday",
    };
 
    if (personalSection) {
      const styles = personalSection.flatMap((section, sectionIdx) => {
        Iif (!section.classSections) return [];
 
        return section.classSections.flatMap((cls, clsIdx) => {
          Iif (!cls.timeLocations) return [];
 
          return cls.timeLocations.flatMap((loc, locIdx) => {
            const start = convertTimeToMinutes(loc.beginTime);
            const end = convertTimeToMinutes(loc.endTime);
            const height = end - start;
 
            return (
              loc.days?.split("")?.map((dayAbbrev) => {
                const day = dayMap[dayAbbrev];
 
                return {
                  id: `${sectionIdx}-${clsIdx}-${locIdx}-${dayAbbrev}`,
                  title: section.title || "Untitled",
                  startTime: loc.beginTime,
                  endTime: loc.endTime,
                  day,
                  description: `${section.courseId?.trim()} — ${loc.building} ${loc.room}`,
                  style: {
                    position: "absolute",
                    top: `${start - 205}px`,
                    height: `${height}px`,
                    width: "100%",
                    backgroundColor: "#b3d9ff",
                    border: "2px solid #3399ff",
                    zIndex: 1,
                    padding: "4px",
                  },
                  titleStyle: {
                    fontSize:
                      height < 25 ? "10px" : height < 40 ? "12px" : "14px",
                    overflow: "hidden",
                    whiteSpace: "nowrap",
                    textOverflow: "ellipsis",
                  },
                  height: height,
                };
              }) ?? []
            );
          });
        });
      });
      setEventStyles(styles);
    }
  }, [personalSection]);
  // Stryker restore all
 
  const createButton = () => (
    <Button variant="primary" href="/personalschedules/list">
      Back
    </Button>
  );
 
  return (
    <BasicLayout>
      <div className="pt-2">
        <h1>Personal Schedules Details</h1>
        {personalSchedule && (
          <PersonalSchedulesTable
            personalSchedules={[personalSchedule]}
            showButtons={false}
          />
        )}
        <h2>Sections in Personal Schedule</h2>
        {personalSection && (
          <PersonalSectionsTable
            personalSections={personalSection}
            psId={id}
            currentUser={currentUser}
          />
        )}
 
        {/* Optional visual scheduler block (from SchedulerEvents logic) */}
        <div
          data-testid="calendar-grid"
          style={{
            position: "relative",
            height: "1500px",
            margin: "20px 0",
            border: "1px solid #ddd",
          }}
        >
          <h2 className="mt-4 text-center">Weekly View</h2>
          <div style={styles.weeklyViewWrapper}>
            <Container fluid style={styles.schedulerPanel}>
              <Row style={styles.headerRow}>
                <Col style={styles.timeColumn}></Col>
                {daysOfWeek.map((day) => (
                  <Col key={day} style={styles.dayColumn}>
                    <Card style={styles.dayCard}>
                      <Card.Body>
                        <Card.Title style={styles.dayTitle}>{day}</Card.Title>
                      </Card.Body>
                      {eventStyles
                        .filter((event) => event.day === day)
                        .map((event) => (
                          <OverlayTrigger
                            key={event.id}
                            trigger="click"
                            placement="auto-start"
                            rootClose
                            overlay={
                              <Popover>
                                <Popover.Header as="h3">
                                  {event.title}
                                </Popover.Header>
                                <Popover.Body
                                  data-testid={`PopoverBody-${event.id}`}
                                >
                                  <p>
                                    {event.startTime} - {event.endTime}
                                    <br />
                                    {event.description}
                                  </p>
                                </Popover.Body>
                              </Popover>
                            }
                          >
                            <Card
                              data-testid={`SchedulerEvent-${event.id}`}
                              style={event.style}
                            >
                              <Card.Body style={{ padding: "5px" }}>
                                <Card.Text
                                  data-testid={`SchedulerEventTitle-${event.id}`}
                                  style={event.titleStyle}
                                >
                                  {event.title}
                                </Card.Text>
                                {event.height >= 40 && (
                                  <Card.Text
                                    data-testid={`SchedulerEventTime-${event.id}`}
                                    style={{
                                      fontSize: "12px",
                                      textAlign: "center",
                                    }}
                                  >
                                    {event.startTime} - {event.endTime}
                                  </Card.Text>
                                )}
                              </Card.Body>
                            </Card>
                          </OverlayTrigger>
                        ))}
                    </Card>
                  </Col>
                ))}
              </Row>
              <Row>
                <Col style={styles.timeColumn}>
                  <div
                    style={{ ...styles.timeSlot, height: "30px", border: "0" }}
                  ></div>
                  {hours.map((hour, index) => (
                    <div
                      key={index}
                      style={{ ...styles.timeSlot, border: "0" }}
                    >
                      <span style={styles.hourLabel}>{hour}</span>
                    </div>
                  ))}
                </Col>
                {daysOfWeek.map((day) => (
                  <Col key={day} style={styles.dayColumn}>
                    <div style={{ ...styles.timeSlot, height: "30px" }}></div>
                    {hours.slice(0, hours.length - 1).map((_, index) => (
                      <div key={index} style={styles.timeSlot}>
                        <Card style={styles.eventCard} />
                      </div>
                    ))}
                  </Col>
                ))}
              </Row>
            </Container>
          </div>
        </div>
        {createButton()}
      </div>
    </BasicLayout>
  );
}
 
// Stryker disable all: no need to test styles
const styles = {
  weeklyViewWrapper: {
    display: "flex",
    justifyContent: "center",
    marginBottom: "2rem",
  },
  schedulerPanel: {
    backgroundColor: "#fff",
    padding: "20px",
  },
  headerRow: {
    textAlign: "center",
  },
  timeColumn: {
    textAlign: "right",
    borderRight: "1px solid #ddd",
    position: "relative",
  },
  dayColumn: {
    padding: 0,
    borderRight: "1px solid #ddd",
  },
  dayCard: {
    backgroundColor: "#ddd",
    borderRadius: "0",
  },
  dayTitle: {
    fontSize: "1.2rem",
    fontWeight: "bold",
  },
  timeSlot: {
    height: "60px",
    display: "flex",
    alignItems: "center",
    justifyContent: "center",
    position: "relative",
    borderBottom: "1px solid #ddd",
  },
  eventCard: {
    width: "100%",
    height: "100%",
    border: "0",
    borderRadius: "0",
  },
  hourLabel: {
    height: "30px",
    display: "flex",
    alignItems: "center",
    justifyContent: "right",
    position: "absolute",
    top: 0,
    right: "5px",
    transform: "translateY(-50%)",
  },
};
// Stryker restore all