frontend/src/components/SchedulerRow.tsx
Maciek Głowacki d0d685ef64 popover styled
2020-11-25 03:14:39 +01:00

226 lines
7.2 KiB
TypeScript

import React, { Fragment, MouseEvent, useState, useEffect, useRef } from 'react';
import { GroupType, SchedulerEvent } from '../types';
import styled from 'styled-components/macro';
import Popover from '@material-ui/core/Popover';
import { makeStyles, createStyles, Theme } from '@material-ui/core/styles';
import { MONDAY_TO_FRIDAY } from '../constants';
const useStyles = makeStyles((theme: Theme) =>
createStyles({
popover: {
pointerEvents: 'none',
fontSize: '14px',
},
paper: {
padding: '15px 15px 15px 15px',
textAlign: 'left',
lineHeight: `1 !important`,
},
}),
);
const PopoverSpan = styled.span`
font-weight: 'bold';
margin-right: 2px;
`;
interface SchedulerEventsWrapperProps {
eventIndex: number;
rowTop: number;
cellWidth: number;
cellHeight: number;
}
const SchedulerEventsWrapper = styled.div<SchedulerEventsWrapperProps>`
position: absolute;
display: flex;
top: ${({ rowTop }) => rowTop}px;
left: ${({ cellWidth, eventIndex }) => (cellWidth * 1) / 5 + 4 + cellWidth * eventIndex}px;
width: ${({ cellWidth }) => cellWidth - 10}px;
height: ${({ cellHeight }) => cellHeight * 3}px;
z-index: 2;
padding-left: 10px;
`;
interface SchedulerEventProps {
cellWidth: number;
cellHeight: number;
groupType: GroupType;
}
const StyledSchedulerEvent = styled.div<SchedulerEventProps>`
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
z-index: 2;
font-size: 0.65vw;
line-height: normal;
border-radius: 10px;
height: ${({ cellHeight }) => cellHeight * 3}px;
width: ${({ cellWidth }) => (cellWidth * 3) / 4}px;
margin-right: 5px;
padding: 5px 5px 0 5px;
text-align: center;
background-color: ${({ groupType }) => (groupType === 'CLASS' ? '#FFDC61' : '#9ed3ff')};
box-shadow: 3px 3px 3px 0px rgba(0, 0, 0, 0.75);
`;
const threeStyles = () => {
return `
white-space: nowrap;
text-overflow: ellipsis;
max-width: 70px;`;
};
type BoldParagraphProps = {
isThree?: boolean;
};
const BoldParagraph = styled.p<BoldParagraphProps>`
overflow: hidden;
flex: 3;
${({ isThree }) => isThree && threeStyles}
`;
const ClassWrap = styled.div`
font-weight: 700;
height: 100%;
width: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
line-height: normal;
`;
const TextWrapper = styled.div`
flex: 1;
width: inherit;
padding: 0 5px 5px 5px;
display: flex;
justify-content: space-between;
`;
interface SchedulerRowProps {
groups: Array<SchedulerEvent>;
indexRow: number;
rowTop: number;
cellWidth: number;
cellHeight: number;
}
const getGroupsPerDay = (groups: Array<SchedulerEvent>) => {
const groupsPerDay: any = { 0: 0, 1: 0, 2: 0, 3: 0, 4: 0 };
for (const group of groups) {
groupsPerDay[group.day]++;
}
return groupsPerDay;
};
export const SchedulerRow = ({ groups, indexRow, rowTop, cellWidth, cellHeight }: SchedulerRowProps) => {
const classes = useStyles();
const groupsPerDay = getGroupsPerDay(groups);
console.log('groups: ', groups);
const [anchorEl, setAnchorEl] = React.useState<HTMLDivElement | null>(null);
const [popoverId, setPopoverId] = useState<string | null>(null);
//looks weird
const handlePopoverOpen = (event: MouseEvent<HTMLDivElement, globalThis.MouseEvent>) => {
setAnchorEl(event.currentTarget);
setPopoverId(event.currentTarget.id);
};
const handlePopoverClose = () => {
setAnchorEl(null);
setPopoverId(null);
};
const open = Boolean(anchorEl);
return (
<div>
{[...Array(MONDAY_TO_FRIDAY)].map((_, eventIndex) => (
<SchedulerEventsWrapper
eventIndex={eventIndex}
rowTop={rowTop}
cellWidth={cellWidth}
cellHeight={cellHeight}
key={eventIndex}
id={`eventRow${indexRow}eventCol${eventIndex}`}
>
{groups.map(
(group, index) =>
group.day === eventIndex && (
<Fragment key={index}>
<StyledSchedulerEvent
groupType={group.type}
cellWidth={cellWidth}
cellHeight={cellHeight}
id={`eventRow${indexRow}eventCol${eventIndex}${index}`}
key={index}
aria-owns={open ? `mouse-over-popover` : undefined}
aria-haspopup="true"
onMouseEnter={(e) => handlePopoverOpen(e)}
onMouseLeave={handlePopoverClose}
>
<ClassWrap>
<BoldParagraph isThree={groupsPerDay[group.day] >= 3}>{groups[index].name}</BoldParagraph>
{groupsPerDay[group.day] < 3 ? (
<TextWrapper>
<div>{`${groups[index].time[0]}-${groups[index].time[1]}`}</div>
<div>3/{groups[index].capacity}</div>
</TextWrapper>
) : (
<TextWrapper style={{ flexDirection: 'column' }}>
<div style={{ alignSelf: 'flex-end' }}>3/{groups[index].capacity}</div>
</TextWrapper>
)}
</ClassWrap>
</StyledSchedulerEvent>
<Popover
id={`mouse-over-popover`}
className={classes.popover}
classes={{
paper: classes.paper,
}}
open={popoverId === `eventRow${indexRow}eventCol${eventIndex}${index}`}
anchorEl={anchorEl}
anchorOrigin={{
vertical: 'top',
horizontal: 'right',
}}
transformOrigin={{
vertical: 'center',
horizontal: 'left',
}}
onClose={handlePopoverClose}
disableRestoreFocus
>
<div style={{ display: 'flex', flexDirection: 'column' }}>
<p style={{ margin: '7px 0 7px 0', fontWeight: 'bold' }}>{groups[index].name}</p>
<p style={{ margin: '2px 0 2px 0' }}>
<PopoverSpan>Prowadzący:</PopoverSpan> {groups[index].lecturer}
</p>
<p style={{ margin: '2px 0 2px 0' }}>
<span style={{ fontWeight: 'bold', marginRight: '2px' }}>Sala zajęć</span>: {groups[index].room}
</p>
<p style={{ margin: '2px 0 2px 0' }}>
<span style={{ fontWeight: 'bold', marginRight: '2px' }}>Kod przedmiotu: </span>ACB129
</p>
<p style={{ margin: '2px 0 2px 0' }}>
<span style={{ fontWeight: 'bold', marginRight: '2px' }}>Kod grupy: </span>FVJ753
</p>
<p style={{ margin: '2px 0 2px 0' }}>
<span style={{ fontWeight: 'bold', marginRight: '2px' }}>Punkty ECTS:</span> 2
</p>
</div>
</Popover>
</Fragment>
),
)}
</SchedulerEventsWrapper>
))}
</div>
);
};