All files / src/components Scheduler.tsx

21.43% Statements 6/28
0% Branches 0/28
0% Functions 0/13
22.22% Lines 6/27

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              1x                           1x               1x         1x                     1x                                             1x                                                                                                                                      
import React, { useLayoutEffect, useRef } from 'react';
import { useState } from 'react';
import { SchedulerEvents } from './SchedulerEvents';
import { days, hours } from '../constants/index';
import styled from 'styled-components/macro';
import { SchedulerEvent } from '../types';
 
const SchedulerWrapper = styled.div`
  border-collapse: collapse;
  flex: 1;
  background-color: white;
  padding: 10px 40px 25px 10px;
  border-radius: 5px;
  margin-right: 20px;
  margin-left: 20px;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  box-shadow: 3px 3px 3px -2px rgba(0, 0, 0, 0.59);
`;
 
const TableBody = styled.div`
  position: relative;
  width: 100%;
  display: flex;
  flex-direction: column;
  height: calc(100% * 25 / 26);
`;
 
const TableRow = styled.div`
  display: flex;
  height: 100%;
`;
 
const TableHead = styled.div`
  display: flex;
  width: 100%;
  height: calc(100% / 26);
`;
 
interface TableCellProps {
  cellHeight?: number;
  isHourColumn?: boolean;
}
 
const TableCell = styled.div<TableCellProps>`
  border-width: ${({ isHourColumn }) => !isHourColumn && '1px'};
  border-style: ${({ isHourColumn }) => !isHourColumn && 'none solid dotted none'};
  border-color: rgb(235, 235, 235);
  display: flex;
  align-items: center;
  justify-content: ${({ isHourColumn }) => (isHourColumn ? 'flex-end' : 'center')};
  flex: ${({ isHourColumn }) => (isHourColumn ? '1' : '5')};
  margin-right: ${({ isHourColumn }) => (isHourColumn ? '10px' : '0px')};
  margin-top: ${({ isHourColumn, cellHeight }) => (isHourColumn ? `-${cellHeight}px` : '0px')};
  font-size: 0.75vw;
  user-select: none;
  border-collapse: collapse;
  :nth-child(2) {
    border-left: 1px solid rgb(235, 235, 235);
  }
  font-weight: bold;
`;
 
interface SchedulerProps {
  schedulerEvents: Array<SchedulerEvent>;
}
 
export const Scheduler = ({ schedulerEvents }: SchedulerProps) => {
  const cellRef = useRef<HTMLDivElement>(null);
  const [cellWidth, setCellWidth] = useState(0);
  const [cellHeight, setCellHeight] = useState(0);
 
  useLayoutEffect(() => {
    const handleResize = () => {
      if (cellRef.current) {
        setCellWidth(cellRef.current.getBoundingClientRect().width);
        setCellHeight(cellRef.current.getBoundingClientRect().height);
      }
    };
    handleResize();
    window.addEventListener('resize', handleResize);
    return () => window.removeEventListener('resize', handleResize);
  }, []);
 
  return (
    <SchedulerWrapper>
      <TableHead>
        {days.map((day, indexCell) =>
          indexCell === 0 ? (
            <TableCell isHourColumn={true} key={indexCell}>
              {day}
            </TableCell>
          ) : (
            <TableCell style={{ borderStyle: 'none none solid none' }} key={indexCell}>
              {day}
            </TableCell>
          ),
        )}
      </TableHead>
      <TableBody>
        {hours.map((hour, indexRow) => (
          <TableRow key={indexRow}>
            {[hour, '', '', '', '', ''].map((value, indexCell) =>
              indexCell === 0 ? (
                <TableCell isHourColumn={true} cellHeight={cellHeight} key={`${indexRow}${indexCell}`}>
                  {value}
                </TableCell>
              ) : indexRow === 0 && indexCell === 1 ? (
                <TableCell ref={cellRef} key={`${indexRow}${indexCell}`}>
                  {value}
                </TableCell>
              ) : indexRow === 23 ? (
                <TableCell style={{ borderBottom: '1px solid   rgb(235, 235, 235)' }} key={`${indexRow}${indexCell}`}>
                  {value}
                </TableCell>
              ) : indexRow === 5 ? (
                <TableCell style={{ borderBottom: '1px solid  rgb(235, 235, 235)' }} key={`${indexRow}${indexCell}`}>
                  {value}
                </TableCell>
              ) : indexRow % 2 !== 0 ? (
                <TableCell style={{ borderBottom: '1px solid  rgb(235, 235, 235)' }} key={`${indexRow}${indexCell}`}>
                  {value}
                </TableCell>
              ) : (
                <TableCell key={`${indexRow}${indexCell}`}>{value}</TableCell>
              ),
            )}
          </TableRow>
        ))}
        <SchedulerEvents cellWidth={cellWidth} cellHeight={cellHeight} schedulerEvents={schedulerEvents} />
      </TableBody>
    </SchedulerWrapper>
  );
};