From c7dc37c8270efff51d6328a9e678478b00748c8d Mon Sep 17 00:00:00 2001 From: wrzesinski-hubert Date: Sat, 5 Dec 2020 18:06:26 +0100 Subject: [PATCH] new provider + select changes --- src/components/Dropdown.tsx | 60 +++++++++++++++++++------------- src/components/SelectMenu.tsx | 18 +++++++--- src/components/Topbar.tsx | 16 ++++----- src/contexts/CoursesProvider.tsx | 16 +-------- src/contexts/UsersProvider.tsx | 14 +++----- src/index.tsx | 11 +++--- 6 files changed, 68 insertions(+), 67 deletions(-) diff --git a/src/components/Dropdown.tsx b/src/components/Dropdown.tsx index 35b9fa3..8564272 100644 --- a/src/components/Dropdown.tsx +++ b/src/components/Dropdown.tsx @@ -1,5 +1,6 @@ import React, { useState, useContext, useEffect, MouseEvent, useMemo } from 'react'; import { coursesContext } from '../contexts/CoursesProvider'; +import { usersContext } from '../contexts/UsersProvider'; import { Course, User } from '../types'; import styled from 'styled-components'; @@ -45,10 +46,12 @@ interface DropdownProps { open: boolean; input: string; handleCloseDropdown: () => void; + selectedOption: string; } -export const Dropdown = ({ open, input, handleCloseDropdown }: DropdownProps) => { - const { courses, users, selectBasketNames, addCourseToBasket } = useContext(coursesContext)!; +export const Dropdown = ({ open, input, handleCloseDropdown, selectedOption }: DropdownProps) => { + const { courses, selectBasketNames, addCourseToBasket } = useContext(coursesContext)!; + const { users } = useContext(usersContext)!; const basketNames = useMemo(() => selectBasketNames(), [selectBasketNames]); const [filteredCourses, setFilteredCourses] = useState>([]); const [filteredUsers, setFilteredUsers] = useState>([]); @@ -84,18 +87,17 @@ export const Dropdown = ({ open, input, handleCloseDropdown }: DropdownProps) => useEffect(() => { const filterUsers = (input: string) => { - const filteredUsers = users.filter( - ({ name,surname }) => - (name+surname) - .toLowerCase() - .normalize('NFD') - .replace(/[\u0300-\u036f]/g, '') - .includes( - input - .toLowerCase() - .normalize('NFD') - .replace(/[\u0300-\u036f]/g, ''), - ), + const filteredUsers = users.filter(({ name, surname }) => + (name + surname) + .toLowerCase() + .normalize('NFD') + .replace(/[\u0300-\u036f]/g, '') + .includes( + input + .toLowerCase() + .normalize('NFD') + .replace(/[\u0300-\u036f]/g, ''), + ), ); setFilteredUsers(filteredUsers); }; @@ -106,17 +108,25 @@ export const Dropdown = ({ open, input, handleCloseDropdown }: DropdownProps) => {open && ( <> - {filteredUsers.map(({ name,surname, id }, index) => ( - -

{name} {surname}

-
- ))} -{/* - {filteredCourses.map(({ name, id }, index) => ( - -

{name}

-
- ))} */} + {selectedOption === 'studenci' ? ( +
+ {filteredUsers.map(({ name, surname, id }, index) => ( + +

+ {name} {surname}{' '} +

+
+ ))} +
+ ) : ( +
+ {filteredCourses.map(({ name, id }, index) => ( + +

{name}

+
+ ))} +
+ )} )}
diff --git a/src/components/SelectMenu.tsx b/src/components/SelectMenu.tsx index 6bc04a4..f8a18fa 100644 --- a/src/components/SelectMenu.tsx +++ b/src/components/SelectMenu.tsx @@ -15,6 +15,7 @@ const Wrapper = styled.div` color: black; border-top-left-radius: 6px; border-bottom-left-radius: 6px; + &:focus { outline: none; } @@ -64,16 +65,23 @@ const ExpandIconSelect = styled(ExpandIcon)` width: 10px; height: 10px; `; -export const SelectMenu = () => { + +interface SelectMenuProps { + changeSelectedOption: (option: string) => void; + changeDropdownOpen: (dropdownState: boolean) => void; + selectedOption:string; +} + +export const SelectMenu = ({changeSelectedOption,changeDropdownOpen, selectedOption}:SelectMenuProps) => { const [isOpen, setIsOpen] = useState(false); - const [selectedOption, setSelectedOption] = useState('przedmioty'); + return ( { setIsOpen(false); }} > - + {changeDropdownOpen(false)}}>
{ console.log('clicked'); @@ -87,7 +95,7 @@ export const SelectMenu = () => { { - setSelectedOption('przedmioty'); + changeSelectedOption('przedmioty'); setIsOpen(false); }} > @@ -95,7 +103,7 @@ export const SelectMenu = () => { { - setSelectedOption('studenci'); + changeSelectedOption('studenci'); setIsOpen(false); }} > diff --git a/src/components/Topbar.tsx b/src/components/Topbar.tsx index b783b0c..31cd3ad 100644 --- a/src/components/Topbar.tsx +++ b/src/components/Topbar.tsx @@ -60,7 +60,7 @@ const InputWrapper = styled.div` margin-top: 15px; max-height: 40px; background-color: #f2f4f7; - border-radius: 6px 6px 6px 6px; + border-radius: 0px 6px 6px 0px; padding-left: 6px; &:hover { background-color: #ffffff; @@ -128,6 +128,9 @@ export default function ({ handleTransfer }: TopbarProps) { const [anchorEl, setAnchorEl] = useState(null); const [open, setOpen] = useState(false); const [input, setInput] = useState(''); + const [selectedOption, setSelectedOption] = useState('przedmioty'); + + const changeSelectedOption = (option:string) => setSelectedOption(option); const onLangChange = () => setIsPolish(!isPolish); @@ -159,15 +162,10 @@ export default function ({ handleTransfer }: TopbarProps) { - - + - {/* - Przedmiot - Student - */} { @@ -177,7 +175,7 @@ export default function ({ handleTransfer }: TopbarProps) { - + diff --git a/src/contexts/CoursesProvider.tsx b/src/contexts/CoursesProvider.tsx index c1d0cde..4f1eb54 100644 --- a/src/contexts/CoursesProvider.tsx +++ b/src/contexts/CoursesProvider.tsx @@ -1,5 +1,5 @@ import React, { useState, createContext, useEffect, ReactNode } from 'react'; -import { Course, Group, Basket, GroupType, SchedulerEvent,User } from '../types'; +import { Course, Group, Basket, GroupType, SchedulerEvent } from '../types'; import { useSnackbar } from 'notistack'; import { createClassTime } from '../utils'; import { axiosInstance } from '../utils/axiosInstance'; @@ -15,7 +15,6 @@ const StyledCloseIcon = styled(CloseIcon)` `; interface CourseContext { - users: Array; courses: Array; basket: Array; hoveredGroup: Group | undefined | null; @@ -42,7 +41,6 @@ export const CoursesProvider = ({ children }: CoursesProviderProps) => { //fetch courses with groups const [courses, setCourses] = useState>([]); - const [users, setUsers] = useState>([]); const [basket, setBasket] = useState>([]); const [hoveredGroup, setHoveredGroup] = useState(null); const selectBasketIds = () => { @@ -179,28 +177,16 @@ export const CoursesProvider = ({ children }: CoursesProviderProps) => { } }; - const getUsers = async () => { - try { - const {data}= await axiosInstance.get>(`${process.env.REACT_APP_API_URL}/api/v1/users/student/search?query=a`); - console.log(data); - setUsers(data); - } catch(e){ - console.log(e); - } - }; - useEffect(() => { setTimeout(() => { fetchCourses(); getNewestTimetable(); - getUsers(); }, 200); }, []); return ( { const [users, setUsers] = useState>([]); - + const getUsers = async () => { try { - const { data } = await axiosInstance.get>( - `${process.env.REACT_APP_API_URL}/api/v1/Users/student/search?query=`, - ); - console.log(data); + const {data}= await axiosInstance.get>(`${process.env.REACT_APP_API_URL}/api/v1/users/student/search?query=`); setUsers(data); - } catch (e) { + console.log(data); + } catch(e){ console.log(e); } }; useEffect(() => { - setTimeout(() => { getUsers(); - }, 200); }, []); return ( diff --git a/src/index.tsx b/src/index.tsx index 0e9ee27..b5bd2c5 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -3,6 +3,7 @@ import ReactDOM from 'react-dom'; import { App } from './components/App'; import { CASProvider } from './contexts/CASProvider'; import { CoursesProvider } from './contexts/CoursesProvider'; +import { UsersProvider } from './contexts/UsersProvider'; import { GlobalStyles } from './styles/GlobalStyles'; import { SnackbarProvider } from 'notistack'; @@ -16,10 +17,12 @@ ReactDOM.render( }} > - - - - + + + + + + ,