From c194bf1ee2e7dd610d782dcaefb622cb86370fdf Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 24 Jul 2020 17:00:06 +0200 Subject: [PATCH] Refactor --- src/App.tsx | 2 +- src/businesslogic/BusinessLogicProvider.tsx | 2 +- src/businesslogic/LecturesProvider.tsx | 2 +- src/businesslogic/mockData/lectures.ts | 42 +++++++ src/businesslogic/types/group.ts | 7 ++ src/businesslogic/types/lecture.ts | 6 + src/businesslogic/types/lectureInit.ts | 4 + src/businesslogic/{models => types}/user.ts | 2 +- src/components/RightBar/LectureCard/index.tsx | 2 +- src/components/RightBar/index.tsx | 2 +- src/components/TopBar/Results/index.tsx | 111 ++++++++---------- src/lectures.ts | 53 --------- 12 files changed, 117 insertions(+), 118 deletions(-) create mode 100644 src/businesslogic/mockData/lectures.ts create mode 100644 src/businesslogic/types/group.ts create mode 100644 src/businesslogic/types/lecture.ts create mode 100644 src/businesslogic/types/lectureInit.ts rename src/businesslogic/{models => types}/user.ts (72%) delete mode 100644 src/lectures.ts diff --git a/src/App.tsx b/src/App.tsx index 8a8318e..00bc599 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -4,7 +4,7 @@ import Transfer from "./components/Transfer/"; import "./App.scss"; import {Scheduler} from "./components/Scheduler"; import RightBar from "./components/RightBar"; -import { lectures } from "./lectures"; +import { lectures } from "./businesslogic/mockData/lectures"; import BusinessLogicContext from "./businesslogic/BusinessLogicContext"; import { BuisnessProvided } from "./businesslogic/BusinessLogicProvider"; diff --git a/src/businesslogic/BusinessLogicProvider.tsx b/src/businesslogic/BusinessLogicProvider.tsx index 4cdb84a..9e90320 100644 --- a/src/businesslogic/BusinessLogicProvider.tsx +++ b/src/businesslogic/BusinessLogicProvider.tsx @@ -1,6 +1,6 @@ import BusinessLogicContext from "./BusinessLogicContext"; import React, { Component } from "react"; -import { User } from "./models/user"; +import { User } from "./types/user"; export interface BuisnessProvided { states: BusinessState; diff --git a/src/businesslogic/LecturesProvider.tsx b/src/businesslogic/LecturesProvider.tsx index c5625de..3b4309a 100644 --- a/src/businesslogic/LecturesProvider.tsx +++ b/src/businesslogic/LecturesProvider.tsx @@ -1,5 +1,5 @@ import React, { useState } from "react"; -import { Lecture } from "../lectures"; +import { Lecture } from "./mockData/lectures"; interface ILectureContext { lectures: Array diff --git a/src/businesslogic/mockData/lectures.ts b/src/businesslogic/mockData/lectures.ts new file mode 100644 index 0000000..1b6b73d --- /dev/null +++ b/src/businesslogic/mockData/lectures.ts @@ -0,0 +1,42 @@ +import { Lecture } from "../types/lecture"; + +export const lectures: Array = [ + { + name: "E-gospodarka - narzędzia i bezpieczeństwo", + groups: [ + { + id: "1CB", + day: "Pn", + time: "10:00", + lecturer: "dr inż. Michał Ren", + room: "A2-01", + }, + { + id: "1XD", + day: "Wt", + time: "12:00", + lecturer: "dr inż. Michał Ren", + room: "A3-01", + }, + ], + }, + { + name: "Statystyka", + groups: [ + { + id: "2CB", + day: "Pn", + time: "10:00", + lecturer: "dr inż. Michał Ren", + room: "A2-01", + }, + { + id: "2XD", + day: "Wt", + time: "12:00", + lecturer: "dr inż. Michał Ren", + room: "A3-01", + }, + ], + }, +]; diff --git a/src/businesslogic/types/group.ts b/src/businesslogic/types/group.ts new file mode 100644 index 0000000..af6e89e --- /dev/null +++ b/src/businesslogic/types/group.ts @@ -0,0 +1,7 @@ +export interface Group { + id: string; + day: string; + time: string; + lecturer: string; + room: string; +} \ No newline at end of file diff --git a/src/businesslogic/types/lecture.ts b/src/businesslogic/types/lecture.ts new file mode 100644 index 0000000..ef86640 --- /dev/null +++ b/src/businesslogic/types/lecture.ts @@ -0,0 +1,6 @@ +import { Group } from "./group"; + +export interface Lecture { + name: string; + groups: Array; +} diff --git a/src/businesslogic/types/lectureInit.ts b/src/businesslogic/types/lectureInit.ts new file mode 100644 index 0000000..8d2869d --- /dev/null +++ b/src/businesslogic/types/lectureInit.ts @@ -0,0 +1,4 @@ +export interface LectureInit { + name: string; + id: number; +} diff --git a/src/businesslogic/models/user.ts b/src/businesslogic/types/user.ts similarity index 72% rename from src/businesslogic/models/user.ts rename to src/businesslogic/types/user.ts index dee0426..3f125a3 100644 --- a/src/businesslogic/models/user.ts +++ b/src/businesslogic/types/user.ts @@ -1,4 +1,4 @@ -export type User = { +export interface User { name?: string; surname?: string; ticket: string | null; diff --git a/src/components/RightBar/LectureCard/index.tsx b/src/components/RightBar/LectureCard/index.tsx index 59edc95..a221a15 100644 --- a/src/components/RightBar/LectureCard/index.tsx +++ b/src/components/RightBar/LectureCard/index.tsx @@ -2,7 +2,7 @@ import React from "react"; import "./index.scss"; import Collapse from "@material-ui/core/Collapse"; import ExpandIcon from "./expand.png"; -import { Lecture } from "../../../lectures"; +import { Lecture } from "../../../businesslogic/mockData/lectures"; interface LectureCardProps { onGroupMouseOver: (id: string, name: string) => void; diff --git a/src/components/RightBar/index.tsx b/src/components/RightBar/index.tsx index aa7ee93..29a9fee 100644 --- a/src/components/RightBar/index.tsx +++ b/src/components/RightBar/index.tsx @@ -1,6 +1,6 @@ import React, { useState, useContext } from "react"; import "./index.scss"; -import { Lecture } from "../../lectures"; +import { Lecture } from "../../businesslogic/mockData/lectures"; import LectureCard from "./LectureCard"; import { LecturesContext } from "../../businesslogic/LecturesProvider"; diff --git a/src/components/TopBar/Results/index.tsx b/src/components/TopBar/Results/index.tsx index 9440b32..b123790 100644 --- a/src/components/TopBar/Results/index.tsx +++ b/src/components/TopBar/Results/index.tsx @@ -1,86 +1,75 @@ -import React, { useState, useContext } from "react"; +import React, { useState, useContext, useEffect } from "react"; import axios from "axios"; import { Input } from "@material-ui/core"; import "./index.scss"; import ClickAwayListener from "@material-ui/core/ClickAwayListener"; import { LecturesContext } from "../../../businesslogic/LecturesProvider"; -import { Lecture, Group } from "../../../lectures"; -import { EDEADLK } from "constants"; - -interface data { - id: number; - name: string; - sym: string; -} - - - +import {LectureInit} from "../../../businesslogic/types/lectureInit"; export const Results: React.FC = () => { const [input, setInput] = useState(""); - const [data, setData] = useState>([]); + const [lecturesData, setLecturesData] = useState>([]); const [open, setOpen] = React.useState(false); const lecturesContext = useContext(LecturesContext); + //fetch lectures ids and lectures names + // useEffect(() => { + // const fetchData = async () => { + // const {lecturesData } = await axios.get( + // `http://localhost:1287/getCourses?name=""` + // ); + // setLecturesData(lecturesData); + // }; + // fetchData(); + // }, []); + + useEffect(() => { + const fetchData = async () => { + const { data } = await axios.get( + `http://localhost:1287/getCourses?name=""` + ); + const lecturesData = data.map((data: { name: any; id: any; })=> {name: data.name, id: data.id}) + + setLecturesData(lecturesData); + }; + fetchData(); + }, []); + + useEffect(() => { + const filterLectures = (value: string) => { + return lecturesData.filter((lecture) => lecture.name.includes(value)); + }; + filterLectures(input); + }, [input]); + + const getLecturesById = async (id: string): Promise => { + const { data } = await axios.get( + `http://localhost:1287/getClassesByCourseId?id=${id}` + ); + return data; + }; const handleChange = (event: React.ChangeEvent) => { setInput(event.target.value); - //query should be updated on backend to not accept empty string - if (event.target.value !== "") { - console.log("Jeste w okej") - search(event.target.value); - } else { - console.log("Jeste w dupuie") - search("xxxxxxxxxxxxxxx"); - - } - }; - const search = (text: string) => { - axios - .get(`http://localhost:1287/getCourses?name=${text}`) - .then((response) => { - const names = lecturesContext.lectures.map(lecture => lecture.name); - console.log(`Names: ${names}`) - const filteredData = response.data.filter((el : any) => !names.includes(el.name)); - console.log("D") - setData(filteredData) - }); - }; + }; const handleClick = () => { setOpen(true); - console.log("OPWENEE") }; const handleClickAway = () => { setOpen(false); }; - const onLectureClick = (e: React.MouseEvent) => { - const target = e.currentTarget as HTMLElement; + const onLectureClick = async (e: React.MouseEvent) => { + const target = e.currentTarget as HTMLElement; const id = target.id; - - const lecture = {name: "", groups: []} as Lecture; - - axios - .get(`http://localhost:1287/getClassesByCourseId?id=${id}`) - .then((response) => { - for(let data of response.data){ - let group = {} as Group; - lecture.name = data.course.name; - group.id = data.id; - group.day = data.day; - group.time = data.time; - group.lecturer = `${data.lecturer.title} ${data.lecturer.name} ${data.lecturer.surname}`; - group.room = data.room.trim(); - lecture.groups.push(group); - lecturesContext.updateLectures(lecture); - } + const result = await getLecturesById(id); + lecturesContext.updateLectures(result); setOpen(false); - }).then(()=>{search(input)}); - } + }; return ( @@ -95,8 +84,13 @@ export const Results: React.FC = () => { /> {open ? (
- {data.map((lecture, index) => ( -
+ {lecturesData.map((lecture, index) => ( +

{lecture.name}

))} @@ -106,4 +100,3 @@ export const Results: React.FC = () => { ); }; - diff --git a/src/lectures.ts b/src/lectures.ts deleted file mode 100644 index a1e714d..0000000 --- a/src/lectures.ts +++ /dev/null @@ -1,53 +0,0 @@ -export interface Lecture { - name: string; - groups: Array; -} - -export interface Group { - id: string; - day: string; - time: string; - lecturer: string; - room: string; -} - -export const lectures: Array = [ - { - name: "E-gospodarka - narzędzia i bezpieczeństwo", - groups: [ - { - id: "1CB", - day: "Pn", - time: "10:00", - lecturer: "dr inż. Michał Ren", - room: "A2-01", - }, - { - id: "1XD", - day: "Wt", - time: "12:00", - lecturer: "dr inż. Michał Ren", - room: "A3-01", - }, - ], - }, - { - name: "Statystyka", - groups: [ - { - id: "2CB", - day: "Pn", - time: "10:00", - lecturer: "dr inż. Michał Ren", - room: "A2-01", - }, - { - id: "2XD", - day: "Wt", - time: "12:00", - lecturer: "dr inż. Michał Ren", - room: "A3-01", - }, - ], - }, -];