frontend/src/components/CourseCard.tsx

219 lines
6.4 KiB
TypeScript
Raw Normal View History

2020-12-04 15:05:54 +01:00
import React, { useState, useContext } from 'react';
2020-08-12 20:52:53 +02:00
import Collapse from '@material-ui/core/Collapse';
2020-11-08 14:54:54 +01:00
import { ReactComponent as Expand } from '../assets/expand.svg';
2020-11-26 01:53:07 +01:00
import { Course, Group, GroupType } from '../types/index';
import { coursesContext } from '../contexts/CoursesProvider';
2020-11-26 01:53:07 +01:00
import styled, { css } from 'styled-components';
2020-08-17 21:15:26 +02:00
import { makeStyles } from '@material-ui/core/styles';
2020-11-25 04:10:03 +01:00
import DeleteIcon from '@material-ui/icons/Delete';
import { useMemo } from 'react';
2020-12-10 21:26:09 +01:00
import { createClassTime } from '../utils';
2020-08-12 20:52:53 +02:00
2020-11-08 14:54:54 +01:00
const CourseCardWrapper = styled.div`
position: relative;
2020-08-17 21:15:26 +02:00
display: flex;
2020-10-08 20:21:52 +02:00
min-height: 40px;
2020-11-26 01:53:07 +01:00
background-color: #b5d2e0;
2020-08-17 21:15:26 +02:00
align-items: center;
justify-content: center;
flex-direction: column;
margin-top: 10px;
2020-11-21 04:02:38 +01:00
border-radius: 10px;
2020-08-17 21:15:26 +02:00
cursor: pointer;
align-items: stretch;
2020-12-10 21:26:09 +01:00
box-shadow: 3px 3px 3px 0px rgba(0, 0, 0, 0.75);
2020-08-17 21:15:26 +02:00
`;
2020-11-08 14:54:54 +01:00
const TitleWrapper = styled.div`
2020-11-25 04:10:03 +01:00
font-size: 14px;
font-weight: 550;
2020-11-08 14:54:54 +01:00
display: flex;
2020-11-21 04:02:38 +01:00
align-items: center;
2020-11-08 14:54:54 +01:00
justify-content: space-between;
2020-11-25 04:10:03 +01:00
padding: 10px 10px 10px 2px;
2020-11-21 04:02:38 +01:00
`;
2020-11-08 14:54:54 +01:00
2020-11-25 04:10:03 +01:00
const BinIcon = styled(DeleteIcon)`
max-width: 30px;
min-width: 30px;
2020-11-08 14:54:54 +01:00
cursor: pointer;
&:hover {
fill: white;
}
`;
const CourseName = styled.div`
padding-left: 3px;
padding-right: 3px;
2020-11-01 21:41:16 +01:00
font-size: 16px;
2020-11-08 14:54:54 +01:00
user-select: none;
2020-08-17 21:15:26 +02:00
`;
2020-10-08 20:21:52 +02:00
const ClassGroupStyled = styled.div`
2020-11-01 21:41:16 +01:00
position: relative;
2020-08-17 21:15:26 +02:00
padding-top: 1px;
2020-11-26 01:53:07 +01:00
padding-bottom: 5px;
2020-12-10 21:26:09 +01:00
transition: background-color 0.4s ease;
2020-08-17 21:15:26 +02:00
:hover {
cursor: pointer;
2020-11-01 21:41:16 +01:00
background-color: #9ed3ff;
2020-08-17 21:15:26 +02:00
}
2020-11-22 17:22:13 +01:00
:last-child {
border-radius: 0 0 10px 10px;
2020-11-25 04:10:03 +01:00
}
2020-08-17 21:15:26 +02:00
`;
2020-11-01 21:41:16 +01:00
interface ExpandIconProps {
2020-11-21 04:02:38 +01:00
selected: boolean;
2020-11-01 21:41:16 +01:00
}
2020-12-02 09:04:06 +01:00
export const ExpandIcon = styled(Expand)<ExpandIconProps>`
width: 20px;
2020-11-08 14:54:54 +01:00
height: 20px;
max-width: 20px;
min-width: 20px;
transition: 0.2s;
2020-11-21 04:02:38 +01:00
transform: ${({ selected }) => (selected ? 'scaleY(-1);' : 'scaleY(1);')};
`;
2020-08-17 21:15:26 +02:00
2020-11-26 01:53:07 +01:00
type StyledGroupTypeProps = {
groupType: GroupType;
};
const StyledGroupType = styled.div<StyledGroupTypeProps>`
2020-11-01 21:41:16 +01:00
font-size: 12px;
position: absolute;
border-radius: 15px;
2020-11-26 01:53:07 +01:00
background-color: ${({ groupType }) => (groupType === 'CLASS' ? '#FFDC61' : '#9ed3ff')};
2020-11-22 17:22:13 +01:00
border: 2px solid white;
2020-11-01 21:41:16 +01:00
min-width: 45px;
top: 5px;
left: 5px;
2020-11-26 01:53:07 +01:00
color: black;
`;
const FlexboxWrapper = styled.div`
display: flex;
flex-direction: column;
`;
type FlexItemProps = {
justifyContent?: string;
};
const FlexItem = styled.div<FlexItemProps>`
display: flex;
font-size: 14px;
2020-12-10 21:26:09 +01:00
font-weight: 500;
2020-11-26 01:53:07 +01:00
${({ justifyContent }) =>
justifyContent &&
css`
justify-content: ${justifyContent};
`}
2020-10-08 20:21:52 +02:00
`;
2020-11-25 04:10:03 +01:00
2020-08-17 21:15:26 +02:00
const useStyles = makeStyles({
expanded: {
maxHeight: '244px',
overflowY: 'auto',
2020-10-08 20:21:52 +02:00
'&::-webkit-scrollbar': {
2020-11-22 17:22:13 +01:00
width: '0.3em',
2020-11-25 04:10:03 +01:00
borderStyle: 'none',
2020-08-17 21:15:26 +02:00
},
2020-10-08 20:21:52 +02:00
'&::-webkit-scrollbar-track': {
2020-11-22 17:22:13 +01:00
borderRadius: '10px',
2020-08-17 21:15:26 +02:00
},
2020-10-08 20:21:52 +02:00
'&::-webkit-scrollbar-thumb': {
borderRadius: '10px',
2020-11-22 17:22:13 +01:00
backgroundColor: '#4b4b4b',
},
},
2020-08-17 21:15:26 +02:00
});
interface CourseCardProps {
course: Course;
}
2020-09-28 18:36:38 +02:00
export const CourseCard = ({ course }: CourseCardProps) => {
2020-08-17 21:15:26 +02:00
const classes = useStyles();
const {
hoveredGroup,
changeGroupInBasket,
deleteFromBasket,
selectBasketCourseGroups,
changeHoveredGroup,
} = useContext(coursesContext)!;
2020-12-10 21:26:09 +01:00
const [isSelected, setSelected] = useState(true);
2020-11-21 04:02:38 +01:00
const groups = [...course.lectures!, ...course.classes!];
2020-12-02 09:04:06 +01:00
const basketCourseGroups = useMemo(() => selectBasketCourseGroups(course.id), []);
const [previous, setPrevious] = useState(basketCourseGroups);
// console.log('lecture is: ', courseLecture);
// console.log('class is: ', courseClasses);
2020-12-02 09:04:06 +01:00
const onGroupClick = (group: Group, courseId: number) => {
setPrevious((prev) => (group.type === GroupType.CLASS ? { ...prev, classes: group } : { ...prev, lecture: group }));
changeGroupInBasket(group, courseId);
};
2020-08-12 20:52:53 +02:00
return (
2020-11-08 14:54:54 +01:00
<CourseCardWrapper>
2020-11-26 01:53:07 +01:00
<TitleWrapper onClick={() => setSelected(!isSelected)}>
<BinIcon
onClick={(e) => {
e.stopPropagation();
deleteFromBasket(course.id);
setSelected(false);
}}
></BinIcon>
2020-11-08 14:54:54 +01:00
<CourseName onClick={() => setSelected(!isSelected)}>{course.name}</CourseName>
2020-11-21 04:02:38 +01:00
<ExpandIcon onClick={() => setSelected(!isSelected)} selected={isSelected} />
2020-11-08 14:54:54 +01:00
</TitleWrapper>
2020-08-17 21:15:26 +02:00
<Collapse className={classes.expanded} in={isSelected} timeout="auto" unmountOnExit>
{groups.map((group: Group, index) => (
<ClassGroupStyled
key={index}
onClick={() => onGroupClick(group, course.id)}
onMouseEnter={() => {
2020-12-02 09:04:06 +01:00
if (group.type === GroupType.CLASS) {
changeGroupInBasket(group, course.id);
2020-11-26 03:30:27 +01:00
// setTimeout(()=> { changeHoveredGroup(courseClasses)},[500])
}
2020-12-02 09:04:06 +01:00
if (group.type === GroupType.LECTURE) {
changeGroupInBasket(group, course.id);
2020-11-26 03:30:27 +01:00
// setTimeout(()=> { changeHoveredGroup(courseLecture)},[500])
}
}}
onMouseLeave={() => {
if (hoveredGroup) {
2020-12-02 09:04:06 +01:00
if (hoveredGroup.type === GroupType.CLASS && previous.classes !== undefined) {
changeGroupInBasket(previous.classes, course.id);
}
if (hoveredGroup.type === GroupType.LECTURE && previous.lecture !== undefined) {
changeGroupInBasket(previous.lecture, course.id);
}
changeHoveredGroup(null);
}
}}
>
2020-11-26 01:53:07 +01:00
<StyledGroupType groupType={group.type}>{group.type === 'CLASS' ? 'ĆW' : 'WYK'}</StyledGroupType>
<FlexboxWrapper>
{group.lecturer.replace('UAM', '').length >= 32 ? (
<FlexItem style={{ justifyContent: 'center', marginLeft: '40px' }}>
{group.lecturer.replace('UAM', '')}
</FlexItem>
) : (
<FlexItem style={{ justifyContent: 'center', marginLeft: '10px' }}>
{group.lecturer.replace('UAM', '')}
</FlexItem>
)}
2020-12-10 21:26:09 +01:00
{console.log("abisfdibuafsbuiafsbuifasbuibuiafsbuifasbuifsabuiasf",group)}
2020-11-26 01:53:07 +01:00
<FlexItem style={{ justifyContent: 'center', margin: '0 50px' }}>
2020-12-10 21:26:09 +01:00
<span> {createClassTime(group.time)[0] + " - " + createClassTime(group.time)[1]} {/* Sala: {group.room} */}</span>
2020-11-26 01:53:07 +01:00
</FlexItem>
</FlexboxWrapper>
2020-11-21 04:02:38 +01:00
</ClassGroupStyled>
))}
2020-08-12 20:52:53 +02:00
</Collapse>
2020-11-08 14:54:54 +01:00
</CourseCardWrapper>
2020-08-12 20:52:53 +02:00
);
2020-08-17 23:56:34 +02:00
};