frontend/src/components/CourseCard.tsx

141 lines
3.5 KiB
TypeScript
Raw Normal View History

2020-11-21 04:02:38 +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-10-21 20:42:18 +02:00
import { Course, Group } from '../types/index';
import { coursesContext } from '../contexts/CoursesProvider';
2020-08-17 21:15:26 +02:00
import styled from 'styled-components';
import { makeStyles } from '@material-ui/core/styles';
2020-11-08 14:54:54 +01:00
import { ReactComponent as Bin } from '../assets/bin.svg';
2020-11-25 04:10:03 +01:00
import DeleteIcon from '@material-ui/icons/Delete';
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-25 04:10:03 +01:00
background-color: rgb(166, 226, 208);
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-11-01 21:41:16 +01:00
box-shadow: 9px 9px 8px -2px rgba(0, 0, 0, 0.59);
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;
padding-bottom: 1px;
: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-11-21 04:02:38 +01:00
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-10-21 12:58:24 +02:00
const TypeClass = styled.div`
2020-11-01 21:41:16 +01:00
font-size: 12px;
position: absolute;
border-radius: 15px;
background-color: #00506b;
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;
color: white;
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();
2020-11-21 04:02:38 +01:00
const { changeGroupInBasket, deleteFromBasket } = useContext(coursesContext)!;
const [isSelected, setSelected] = useState(false);
2020-08-17 23:56:34 +02:00
2020-11-21 04:02:38 +01:00
const groups = [...course.lectures!, ...course.classes!];
const onGroupClick = (group: Group, id: number) => changeGroupInBasket(group, id);
2020-08-12 20:52:53 +02:00
return (
2020-11-08 14:54:54 +01:00
<CourseCardWrapper>
<TitleWrapper>
<BinIcon onClick={() => deleteFromBasket(course.id)}></BinIcon>
<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>
2020-11-21 04:02:38 +01:00
{groups.map((group, index) => (
<ClassGroupStyled key={index} onClick={() => onGroupClick(group, course.id)}>
2020-11-25 09:14:25 +01:00
<TypeClass>{group.type === 'CLASS' ? 'ĆW' : 'WYK'}</TypeClass>
2020-11-21 04:02:38 +01:00
<p>
{group.time} {group.room} <br></br> {group.lecturer}
</p>
</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
};