From d6aa58fd345a3311b2d49401da0f4abd52e0fe57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciek=20G=C5=82owacki?= Date: Thu, 1 Oct 2020 19:51:06 +0200 Subject: [PATCH] Added splitting groups into lectures and classes --- .vscode/launch.json | 15 +++++++++++++++ src/components/CourseCard.tsx | 28 +++++++++++++++------------- src/components/Dropdown.tsx | 25 +++++++++++-------------- src/contexts/CoursesProvider.tsx | 32 ++++++++++++++++++++++++++------ src/types/index.ts | 5 +++-- 5 files changed, 70 insertions(+), 35 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..f6b35a0 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,15 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "chrome", + "request": "launch", + "name": "Launch Chrome against localhost", + "url": "http://localhost:3000", + "webRoot": "${workspaceFolder}" + } + ] +} \ No newline at end of file diff --git a/src/components/CourseCard.tsx b/src/components/CourseCard.tsx index 803f9e3..3652166 100644 --- a/src/components/CourseCard.tsx +++ b/src/components/CourseCard.tsx @@ -32,8 +32,8 @@ const CourseNameStyled = styled.div` padding-bottom: 10px; `; -interface ClassGroupProps{ - groupType:GroupType; +interface ClassGroupProps { + groupType: GroupType; } const ClassGroupStyled = styled.div` @@ -42,8 +42,8 @@ const ClassGroupStyled = styled.div` :hover { cursor: pointer; } - outline-offset: -5px; - outline:${({groupType})=>groupType === "CLASS" ? "2px solid #5642AA" : "2px solid #866DF7"}; + outline-offset: -5px; + outline: ${({ groupType }) => (groupType === 'CLASS' ? '2px solid #5642AA' : '2px solid #866DF7')}; `; const ClassExandIconStyled = styled.img` @@ -89,10 +89,10 @@ interface CourseCardProps { } export const CourseCard = ({ course }: CourseCardProps) => { - const [isSelected, setSelected] = useState(false); const classes = useStyles(); - const { addGroup, deleteFromBasket } = useContext(coursesContext)!; + const [isSelected, setSelected] = useState(false); + const groups = course.lectures === undefined ? course.classes : [...course.lectures, ...course.classes]; const onGroupClick = (group: Group, id: number) => addGroup(group, id); @@ -101,13 +101,15 @@ export const CourseCard = ({ course }: CourseCardProps) => { deleteFromBasket(course.id)}> setSelected(!isSelected)}>{course.name} - {course.groups.sort((a,b)=> b.type.localeCompare(a.type)).map((group, index) => ( - onGroupClick(group, course.id)}> -

- {group.time} {group.room}

{group.lecturer} -

-
- ))} + {groups + .sort((a, b) => b.type.localeCompare(a.type)) + .map((group, index) => ( + onGroupClick(group, course.id)}> +

+ {group.time} {group.room}

{group.lecturer} +

+
+ ))}
setSelected(!isSelected)}> diff --git a/src/components/Dropdown.tsx b/src/components/Dropdown.tsx index 26186b5..c33caf1 100644 --- a/src/components/Dropdown.tsx +++ b/src/components/Dropdown.tsx @@ -7,6 +7,13 @@ import { Course, Basket } from '../types'; import styled from 'styled-components'; import { makeStyles } from '@material-ui/core/styles'; +const DropdownStyled = styled.div` + max-height: 420px; + overflow-y: auto; + scroll-snap-type: y mandatory; + scroll-behavior: smooth; +`; + const CourseStyled = styled.div` position: relative; z-index: 10; @@ -15,20 +22,13 @@ const CourseStyled = styled.div` background-color: #e6c759; font-size: 18px; font-family: Lato; + scroll-snap-align: end; :hover { background-color: #d4b851; cursor: pointer; } `; -const DropdownStyled = styled.div` - max-height: 400px; - overflow-y: auto; - ::-webkit-scrollbar { - display: none; - } -`; - const useStyles = makeStyles({ topbarInput: { marginTop: '8px', @@ -76,12 +76,9 @@ export const Dropdown = ({ clearInput, handleClearInput }: DropdownProps) => { const onCourseClick = async (event: MouseEvent) => { const target = event.currentTarget; if (target.id && target.textContent) { - const id = target.id; - const name = target.textContent; - - //porozmawiać z Filipem, żeby odrobinę przerobił endpoint - const course: Basket = { name: name.trim(), id: parseInt(id) }; - + const course = filteredCourses.find(({ id }) => id.toString() === target.id)!; + console.log('added course is'); + console.log(course); addToBasket(course); setOpen(false); } diff --git a/src/contexts/CoursesProvider.tsx b/src/contexts/CoursesProvider.tsx index 70c7167..4f31d40 100644 --- a/src/contexts/CoursesProvider.tsx +++ b/src/contexts/CoursesProvider.tsx @@ -5,7 +5,7 @@ import axios from 'axios'; interface CourseContext { courses: Array; basket: Array; - addToBasket: (courses: Basket) => void; + addToBasket: (courses: Course) => void; addGroup: (group: Group, id: number) => void; deleteFromBasket: (id: number) => void; } @@ -20,10 +20,17 @@ export const CoursesProvider = ({ children }: CoursesProviderProps) => { const [courses, setCourses] = useState>([]); const [basket, setBasket] = useState>([]); - const addToBasket = (course: Basket) => setBasket([...basket, course]); - - const deleteFromBasket = (id: number) => setBasket(basket.filter(course => course.id !== id)); + const addToBasket = (course: Course) => { + const courseToBasket = { + name: course.name, + id: course.id, + classes: course.classes[0], + lecture: course.lectures !== undefined ? course.lectures[0] : undefined, + } as Basket; + setBasket([...basket, courseToBasket]); + }; + const deleteFromBasket = (id: number) => setBasket(basket.filter((course) => course.id !== id)); useEffect(() => { console.log('BASKET'); @@ -48,14 +55,27 @@ export const CoursesProvider = ({ children }: CoursesProviderProps) => { useEffect(() => { const fetchData = async () => { - const { data: courses } = await axios.get(`${process.env.REACT_APP_API_URL}/getCoursesWithGroups`); + const { data } = await axios.get }>>( + `${process.env.REACT_APP_API_URL}/api/v1/courses/getCoursesWithGroups`, + ); + const courses = data.map(({ id, name, groups }) => ({ + id: parseInt(id), + name, + lectures: groups.filter(({ type }) => type === GroupType.LECTURE), + classes: groups.filter(({ type }) => type === GroupType.CLASS), + })) as Array; + console.log('courses mapped'); + console.log(courses); courses.sort((a: Course, b: Course) => (a.name > b.name ? 1 : -1)); + setCourses(courses); }; fetchData(); }, []); return ( - {children} + + {children} + ); }; diff --git a/src/types/index.ts b/src/types/index.ts index 09c6f40..ee23cf1 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -7,7 +7,7 @@ export interface Basket { id: number; name: string; lecture?: Group; - classes?: Group; + classes: Group; } export interface Group { @@ -23,7 +23,8 @@ export interface Group { export interface Course { id: number; name: string; - groups: Array; + lectures?: Array; + classes: Array; } export interface User {