Refactor again

This commit is contained in:
Maciek Głowacki 2020-08-12 20:52:53 +02:00
parent 48992b7e04
commit 3fba8a4a23
14 changed files with 163 additions and 157 deletions

View File

Before

Width:  |  Height:  |  Size: 535 B

After

Width:  |  Height:  |  Size: 535 B

View File

@ -0,0 +1,50 @@
import React, { useContext } from 'react';
import './index.scss';
import Collapse from '@material-ui/core/Collapse';
import ExpandIcon from './expand.png';
import { Course, Group } from '../../../types/index';
import { coursesContext } from '../../../contexts/CoursesProvider';
import { group } from 'console';
interface CourseCardProps {
onGroupMouseOver: (id: number, name: string) => void;
onCardClick: (e: React.MouseEvent) => void;
course: Course;
id: string;
isSelected: boolean;
}
export function CourseCard({ onGroupMouseOver, onCardClick, course, id, isSelected }: CourseCardProps) {
const { addGroup, courses } = useContext(coursesContext)!;
function onGroupClick(group: Group) {
addGroup(group);
}
return (
<div className="class" onClick={onCardClick} id={id}>
<div className="class__name">{course.name}</div>
<Collapse className="expanded" in={isSelected} timeout="auto" unmountOnExit>
{courses.map((course, index) => (
<>
{course.groups.map((group, index) => (
<div
className="class__group"
key={index}
onMouseOver={() => onGroupMouseOver(group.id, course.name)}
onClick={() => onGroupClick(group)}
>
<p>
{group.time} {group.room} <br></br> {group.lecturer}
</p>{' '}
</div>
))}
</>
))}
</Collapse>
<div onClick={onCardClick} id={id}>
<img alt="expand" src={ExpandIcon} className={`class__expandIcon${isSelected ? 'Rotate' : ''}`} />
</div>
</div>
);
}

View File

@ -1,45 +0,0 @@
import React, { useContext } from 'react';
import './index.scss';
import Collapse from '@material-ui/core/Collapse';
import ExpandIcon from './expand.png';
import { Lecture, Group } from '../../../types/index';
import { LecturesContext } from '../../../contexts/LecturesProvider';
interface LectureCardProps {
onGroupMouseOver: (id: string, name: string) => void;
onCardClick: (e: React.MouseEvent) => void;
lecture: Lecture;
id: string;
isSelected: boolean;
}
export default function LectureCard({ onGroupMouseOver, onCardClick, lecture, id, isSelected }: LectureCardProps) {
const { addGroup } = useContext(LecturesContext)!;
function onGroupClick(group: Group) {
addGroup(group);
}
return (
<div className="class" onClick={onCardClick} id={id}>
<div className="class__name">{lecture.name}</div>
<Collapse className="expanded" in={isSelected} timeout="auto" unmountOnExit>
{lecture.groups.map((group, index) => (
<div
className="class__group"
key={index}
onMouseOver={() => onGroupMouseOver(group.id, lecture.name)}
onClick={() => onGroupClick(group)}
>
<p>
{group.time} {group.room} <br></br> {group.lecturer}
</p>{' '}
</div>
))}
</Collapse>
<div onClick={onCardClick} id={id}>
<img alt="expand" src={ExpandIcon} className={`class__expandIcon${isSelected ? 'Rotate' : ''}`} />
</div>
</div>
);
}

View File

@ -1,17 +1,17 @@
import React, { useState, useContext } from 'react'; import React, { useState, useContext } from 'react';
import './index.scss'; import './index.scss';
import { Lecture } from '../../types'; import { Course } from '../../types';
import LectureCard from './LectureCard'; import { CourseCard } from './CourseCard/index';
import { LecturesContext } from '../../contexts/LecturesProvider'; import { coursesContext } from '../../contexts/CoursesProvider';
interface RightBarProps { interface RightBarProps {
onGroupMouseOver: (id: string, name: string) => void; onGroupMouseOver: (id: number, name: string) => void;
} }
export default function RightBar({ onGroupMouseOver }: RightBarProps) { export default function RightBar({ onGroupMouseOver }: RightBarProps) {
const [selectedCardId, setSelectedCardId] = useState<string | null>(null); const [selectedCardId, setSelectedCardId] = useState<string | null>(null);
const lecturesContext = useContext(LecturesContext); const { courses } = useContext(coursesContext)!;
const onCardClick = (e: React.MouseEvent) => { const onCardClick = (e: React.MouseEvent) => {
const target = e.currentTarget as HTMLElement; const target = e.currentTarget as HTMLElement;
@ -24,9 +24,9 @@ export default function RightBar({ onGroupMouseOver }: RightBarProps) {
Hubert Wrzesiński<br></br> Hubert Wrzesiński<br></br>
Semestr zimowy 2020/2021 Semestr zimowy 2020/2021
</div> </div>
{lecturesContext?.lectures.map((lecture, index) => ( {courses.map((course, index) => (
<LectureCard <CourseCard
lecture={lecture} course={course}
key={index} key={index}
id={index.toString()} id={index.toString()}
onGroupMouseOver={onGroupMouseOver} onGroupMouseOver={onGroupMouseOver}

View File

@ -1,6 +1,6 @@
import React, { useContext, useEffect, useState } from 'react'; import React, { useContext, useEffect, useState } from 'react';
import { SchedulerRow } from '../SchedulerRow'; import { SchedulerRow } from '../SchedulerRow';
import { LecturesContext } from '../../../contexts/LecturesProvider'; import { coursesContext } from '../../../contexts/CoursesProvider';
import { Group } from '../../../types'; import { Group } from '../../../types';
interface SchedulerEventsProps { interface SchedulerEventsProps {
@ -9,20 +9,20 @@ interface SchedulerEventsProps {
} }
export const SchedulerEvents = ({ cellTop, cellWidth }: SchedulerEventsProps) => { export const SchedulerEvents = ({ cellTop, cellWidth }: SchedulerEventsProps) => {
const { choosenGroups } = useContext(LecturesContext)!; const { choosenGroups } = useContext(coursesContext)!;
const [groupsMappedToEvents, setGroupsMappedToEvents] = useState<any>([]); const [groupsMappedToEvents, setGroupsMappedToEvents] = useState<any>([]);
// const groups: Array<Group> = [{ id: "5", day: "4", time: "11.45", lecturer: "dr Dorota Blinkiewicz", room: "A2-3" }, // const groups: Array<Group> = [{ id: "5", day: "4", time: "11.45", courser: "dr Dorota Blinkiewicz", room: "A2-3" },
// { id: "28", day: "1", time: "13.45", lecturer: "dr Barbara Kołodziejczak", room: "D-3" }, // { id: "28", day: "1", time: "13.45", courser: "dr Barbara Kołodziejczak", room: "D-3" },
// { id: "69", day: "4", time: "15.30", lecturer: "dr Karol Gierszewski", room: "A2-3" }]; // { id: "69", day: "4", time: "15.30", courser: "dr Karol Gierszewski", room: "A2-3" }];
interface GroupTimeToEventRowMapping { interface GroupTimeToEventRowMapping {
[time: string]: number; [time: string]: number;
} }
const groupTimeToEventRowMapping: GroupTimeToEventRowMapping = { const groupTimeToEventRowMapping: GroupTimeToEventRowMapping = {
'8.15': 0, '08.15': 0,
'10.00': 1, '10.00': 1,
'11.45': 2, '11.45': 2,
'13.45': 3, '13.45': 3,

View File

@ -39,7 +39,7 @@ export const SchedulerRow = ({ groups, indexRow, cellTop, cellWidth }: Scheduler
id={`eventRow${indexRow}eventCol${eventIndex}`} id={`eventRow${indexRow}eventCol${eventIndex}`}
> >
{groups.map((group, index) => {groups.map((group, index) =>
parseInt(group.day) === eventIndex ? <div key={index}>{groups[index]?.lecturer}</div> : null, group.day === eventIndex ? <div key={index}>{groups[index]?.lecturer}</div> : null,
)} )}
</SchedulerEvent> </SchedulerEvent>
))} ))}

View File

@ -1,4 +1,4 @@
.lecture { .course {
position: relative; position: relative;
z-index: 10; z-index: 10;
padding: 5px; padding: 5px;
@ -7,7 +7,7 @@
font-size: 18px; font-size: 18px;
font-family: Lato; font-family: Lato;
} }
.lecture:hover{ .course:hover {
background-color: #d4b851; background-color: #d4b851;
cursor: pointer; cursor: pointer;
} }
@ -21,4 +21,3 @@
.top-bar__input-field { .top-bar__input-field {
width: 100%; width: 100%;
} }

View File

@ -3,59 +3,48 @@ import axios from 'axios';
import { Input } from '@material-ui/core'; import { Input } from '@material-ui/core';
import './index.scss'; import './index.scss';
import ClickAwayListener from '@material-ui/core/ClickAwayListener'; import ClickAwayListener from '@material-ui/core/ClickAwayListener';
import { LecturesContext } from '../../../contexts/LecturesProvider'; import { coursesContext } from '../../../contexts/CoursesProvider';
import { Lecture, Group } from '../../../types'; import { Course, Group } from '../../../types';
interface LectureData { interface courseData {
name: string; name: string;
id: number; id: number;
} }
export const Results: React.FC = () => { export const Results: React.FC = () => {
const [input, setInput] = useState<string>(''); const [input, setInput] = useState<string>('');
const [lecturesData, setLecturesData] = useState<Array<LectureData>>([]); const [coursesData, setcoursesData] = useState<Array<courseData>>([]);
const [filteredLecturesData, setFilteredLecturesData] = useState<Array<LectureData>>([]); const [filteredcoursesData, setFilteredcoursesData] = useState<Array<courseData>>([]);
const [open, setOpen] = React.useState(false); const [open, setOpen] = React.useState(false);
const lecturesContext = useContext(LecturesContext)!; const { courses, addCourse } = useContext(coursesContext)!;
//fetch lectures ids and lectures names
// useEffect(() => {
// const fetchData = async () => {
// const {lecturesData } = await axios.get(
// `http://localhost:1287/getCourses?name=""`
// );
// setLecturesData(lecturesData);
// };
// fetchData();
// }, []);
useEffect(() => { useEffect(() => {
const fetchData = async () => { const fetchData = async () => {
const results = await axios.get(`http://localhost:1287/getCourses?name=`); const results = await axios.get(`http://localhost:1285/getCourses`);
const lecturesData = results.data.map((result: { id: number; name: string }) => ({ const coursesData = results.data.map((result: { id: number; name: string }) => ({
id: result.id, id: result.id,
name: result.name, name: result.name,
})); }));
setLecturesData(lecturesData); setcoursesData(coursesData);
}; };
fetchData(); fetchData();
}, []); }, []);
useEffect(() => { useEffect(() => {
const names = lecturesContext.lectures.map((lecture) => lecture.name); const names = courses.map((course) => course.name);
const filterLectures = (value: string) => { const filtercourses = (value: string) => {
let filteredLectures = lecturesData.filter( let filteredcourses = coursesData.filter(
(lecture) => lecture.name.toLowerCase().includes(value.toLowerCase()) && !names.includes(lecture.name), (course) => course.name.toLowerCase().includes(value.toLowerCase()) && !names.includes(course.name),
); );
setFilteredLecturesData(filteredLectures); setFilteredcoursesData(filteredcourses);
}; };
filterLectures(input); filtercourses(input);
}, [input, open]); }, [input, open]);
const getLecturesById = async (id: string) => { const getGroupsByCourseId = async (id: string) => {
const { data } = await axios.get(`http://localhost:1287/getClassesByCourseId?id=${id}`); const { data } = await axios.get(`http://localhost:1285/getCourseGroups?id=${id}`);
return data; return data;
}; };
@ -71,27 +60,34 @@ export const Results: React.FC = () => {
setOpen(false); setOpen(false);
}; };
const onLectureClick = async (e: React.MouseEvent) => { const onCourseClick = async (e: React.MouseEvent) => {
const target = e.currentTarget as HTMLElement; const target = e.currentTarget as HTMLElement;
const id = target.id; if (target.id && target.textContent) {
const result = await getLecturesById(id); const id = target.id;
const name = target.textContent;
let groups: Array<Group> = []; const groups = await getGroupsByCourseId(id);
let lecture = { groups: groups } as Lecture; const course: Course = {
lecture.id = result[0].course.id; name: name,
lecture.name = result[0].course.name; id: parseInt(id),
for (let i = 0; i < result.length; i++) { groups: groups,
let group = {} as Group; };
group.id = result[i].id; addCourse(course);
group.day = result[i].day; setOpen(false);
group.time = result[i].time;
group.lecturer = result[i].lecturer.title + ' ' + result[i].lecturer.name + ' ' + result[i].lecturer.surname;
group.room = result[i].room.trim();
lecture.groups.push(group);
} }
lecturesContext.addLecture(lecture); // let groups: Array<Group> = [];
setOpen(false); // let course = { groups: groups } as course;
// course.id = result[0].course.id;
// course.name = result[0].course.name;
// for (let i = 0; i < result.length; i++) {
// let group = {} as Group;
// group.id = result[i].id;
// group.day = result[i].day;
// group.time = result[i].time;
// group.courser = result[i].courser.title + ' ' + result[i].courser.name + ' ' + result[i].courser.surname;
// group.room = result[i].room.trim();
// course.groups.push(group);
// }
}; };
return ( return (
@ -107,9 +103,9 @@ export const Results: React.FC = () => {
/> />
{open ? ( {open ? (
<div className="dropdown"> <div className="dropdown">
{filteredLecturesData.map((lecture, index) => ( {filteredcoursesData.map((course, index) => (
<div className="lecture" key={index} id={String(lecture.id)} onClick={onLectureClick}> <div className="course" key={index} id={String(course.id)} onClick={onCourseClick}>
<p>{lecture.name} </p> <p>{course.name} </p>
</div> </div>
))} ))}
</div> </div>

View File

@ -0,0 +1,32 @@
import React, { useState, createContext, useEffect } from 'react';
import { Course, Group } from '../types';
interface courseContext {
courses: Array<Course>;
choosenGroups: Array<Group>;
addCourse: (courses: Course) => void;
addGroup: (group: Group) => void;
}
export const coursesContext = createContext<courseContext | null>(null);
interface CoursesProviderProps {
children: React.ReactNode;
}
export const CoursesProvider = ({ children }: CoursesProviderProps) => {
const [courses, setCourses] = useState<Array<Course>>([]);
const [choosenGroups, setChoosenGroups] = useState<Array<Group>>([]);
const addCourse = (course: Course) => {
setCourses([...courses, course]);
};
const addGroup = (group: Group) => {
setChoosenGroups([...choosenGroups, group]);
};
return (
<coursesContext.Provider value={{ courses, choosenGroups, addCourse, addGroup }}>
{children}
</coursesContext.Provider>
);
};

View File

@ -1,32 +0,0 @@
import React, { useState, createContext } from 'react';
import { Lecture, Group } from '../types';
interface LectureContext {
lectures: Array<Lecture>;
choosenGroups: Array<Group>;
addLecture: (lectures: Lecture) => void;
addGroup: (group: Group) => void;
}
export const LecturesContext = createContext<LectureContext | null>(null);
interface LecturesProviderProps {
children: React.ReactNode;
}
export const LecturesProvider = ({ children }: LecturesProviderProps) => {
const [lectures, setLectures] = useState<Array<Lecture>>([]);
const [choosenGroups, setChoosenGroups] = useState<Array<Group>>([]);
const addLecture = (lecture: Lecture) => {
setLectures([...lectures, lecture]);
};
const addGroup = (group: Group) => {
setChoosenGroups([...choosenGroups, group]);
};
return (
<LecturesContext.Provider value={{ lectures, choosenGroups, addLecture, addGroup }}>
{children}
</LecturesContext.Provider>
);
};

View File

@ -1,8 +1,6 @@
export enum Types { export enum Types {
addLecture = 'ADD_LECTURE',
removeLecture = 'REMOVE_LECTURE',
addGroup = 'ADD_GROUP',
removeGroup = 'ADD_GROUP',
addCourse = 'ADD_COURSE', addCourse = 'ADD_COURSE',
removeCourse = 'REMOVE_COURSE', removecourse = 'REMOVE_COURSE',
addGroup = 'ADD_GROUP',
removeGroup = 'REMOVE_GROUP',
} }

View File

@ -2,17 +2,17 @@ import React from 'react';
import ReactDOM from 'react-dom'; import ReactDOM from 'react-dom';
import { App } from './App'; import { App } from './App';
import { CASProvider } from './contexts/CASProvider'; import { CASProvider } from './contexts/CASProvider';
import { LecturesProvider } from './contexts/LecturesProvider'; import { CoursesProvider } from './contexts/CoursesProvider';
import { GlobalStyles } from './styles/GlobalStyles'; import { GlobalStyles } from './styles/GlobalStyles';
ReactDOM.render( ReactDOM.render(
<> <>
<LecturesProvider> <CoursesProvider>
<CASProvider> <CASProvider>
<GlobalStyles /> <GlobalStyles />
<App /> <App />
</CASProvider> </CASProvider>
</LecturesProvider> </CoursesProvider>
</>, </>,
document.getElementById('root'), document.getElementById('root'),
); );

View File

@ -1,16 +1,24 @@
export enum GroupType {
course = 'course',
CLASS = 'class',
}
export interface Group { export interface Group {
id: string; id: number;
day: string; day: number;
time: string; time: string;
lecturer: string; lecturer: string;
room: string; room: string;
groupType: GroupType;
capacity?: number;
} }
export interface Lecture {
id?: number; export interface Course {
id: number;
name: string; name: string;
groups: Array<Group>; groups: Array<Group>;
} }
export interface LectureInit { export interface courseInit {
name: string; name: string;
id: number; id: number;
} }