searching users

This commit is contained in:
wrzesinski-hubert 2020-12-05 00:16:59 +01:00
parent ff9db515bc
commit 61b0a6f464
5 changed files with 95 additions and 7 deletions

View File

@ -1,6 +1,6 @@
import React, { useState, useContext, useEffect, MouseEvent, useMemo } from 'react'; import React, { useState, useContext, useEffect, MouseEvent, useMemo } from 'react';
import { coursesContext } from '../contexts/CoursesProvider'; import { coursesContext } from '../contexts/CoursesProvider';
import { Course } from '../types'; import { Course, User } from '../types';
import styled from 'styled-components'; import styled from 'styled-components';
const DropdownContainer = styled.div` const DropdownContainer = styled.div`
@ -48,9 +48,10 @@ interface DropdownProps {
} }
export const Dropdown = ({ open, input, handleCloseDropdown }: DropdownProps) => { export const Dropdown = ({ open, input, handleCloseDropdown }: DropdownProps) => {
const { courses, selectBasketNames, addCourseToBasket } = useContext(coursesContext)!; const { courses, users, selectBasketNames, addCourseToBasket } = useContext(coursesContext)!;
const basketNames = useMemo(() => selectBasketNames(), [selectBasketNames]); const basketNames = useMemo(() => selectBasketNames(), [selectBasketNames]);
const [filteredCourses, setFilteredCourses] = useState<Array<Course>>([]); const [filteredCourses, setFilteredCourses] = useState<Array<Course>>([]);
const [filteredUsers, setFilteredUsers] = useState<Array<User>>([]);
const onCourseClick = (event: MouseEvent) => { const onCourseClick = (event: MouseEvent) => {
const target = event.currentTarget; const target = event.currentTarget;
@ -81,15 +82,41 @@ export const Dropdown = ({ open, input, handleCloseDropdown }: DropdownProps) =>
filterCourses(input); filterCourses(input);
}, [basketNames, courses, input]); }, [basketNames, courses, input]);
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, ''),
),
);
setFilteredUsers(filteredUsers);
};
filterUsers(input);
}, [users, input]);
return ( return (
<DropdownContainer> <DropdownContainer>
{open && ( {open && (
<> <>
{filteredUsers.map(({ name,surname, id }, index) => (
<CourseContainer key={index} id={id.toString()}>
<p>{name} {surname} </p>
</CourseContainer>
))}
{/*
{filteredCourses.map(({ name, id }, index) => ( {filteredCourses.map(({ name, id }, index) => (
<CourseContainer key={index} id={id.toString()} onClick={onCourseClick}> <CourseContainer key={index} id={id.toString()} onClick={onCourseClick}>
<p>{name} </p> <p>{name} </p>
</CourseContainer> </CourseContainer>
))} ))} */}
</> </>
)} )}
</DropdownContainer> </DropdownContainer>

View File

@ -159,7 +159,7 @@ export default function ({ handleTransfer }: TopbarProps) {
<FlexboxColumn> <FlexboxColumn>
<ClickAwayListener onClickAway={handleCloseDropdown}> <ClickAwayListener onClickAway={handleCloseDropdown}>
<Flexbox> <Flexbox>
{/* <SelectMenu /> */} <SelectMenu />
<InputWrapper> <InputWrapper>
{/* <SelectSearch value={value} onChange={Change}> {/* <SelectSearch value={value} onChange={Change}>

View File

@ -1,5 +1,5 @@
import React, { useState, createContext, useEffect, ReactNode } from 'react'; import React, { useState, createContext, useEffect, ReactNode } from 'react';
import { Course, Group, Basket, GroupType, SchedulerEvent } from '../types'; import { Course, Group, Basket, GroupType, SchedulerEvent,User } from '../types';
import { useSnackbar } from 'notistack'; import { useSnackbar } from 'notistack';
import { createClassTime } from '../utils'; import { createClassTime } from '../utils';
import { axiosInstance } from '../utils/axiosInstance'; import { axiosInstance } from '../utils/axiosInstance';
@ -15,6 +15,7 @@ const StyledCloseIcon = styled(CloseIcon)`
`; `;
interface CourseContext { interface CourseContext {
users: Array<User>;
courses: Array<Course>; courses: Array<Course>;
basket: Array<Basket>; basket: Array<Basket>;
hoveredGroup: Group | undefined | null; hoveredGroup: Group | undefined | null;
@ -41,6 +42,7 @@ export const CoursesProvider = ({ children }: CoursesProviderProps) => {
//fetch courses with groups //fetch courses with groups
const [courses, setCourses] = useState<Array<Course>>([]); const [courses, setCourses] = useState<Array<Course>>([]);
const [users, setUsers] = useState<Array<User>>([]);
const [basket, setBasket] = useState<Array<Basket>>([]); const [basket, setBasket] = useState<Array<Basket>>([]);
const [hoveredGroup, setHoveredGroup] = useState<Group | undefined | null>(null); const [hoveredGroup, setHoveredGroup] = useState<Group | undefined | null>(null);
const selectBasketIds = () => { const selectBasketIds = () => {
@ -177,16 +179,28 @@ export const CoursesProvider = ({ children }: CoursesProviderProps) => {
} }
}; };
const getUsers = async () => {
try {
const {data}= await axiosInstance.get<Array<User>>(`${process.env.REACT_APP_API_URL}/api/v1/users/student/search?query=a`);
console.log(data);
setUsers(data);
} catch(e){
console.log(e);
}
};
useEffect(() => { useEffect(() => {
setTimeout(() => { setTimeout(() => {
fetchCourses(); fetchCourses();
getNewestTimetable(); getNewestTimetable();
getUsers();
}, 200); }, 200);
}, []); }, []);
return ( return (
<coursesContext.Provider <coursesContext.Provider
value={{ value={{
users,
courses, courses,
basket, basket,
hoveredGroup, hoveredGroup,

View File

@ -0,0 +1,45 @@
import React, { useState, createContext, useEffect, ReactNode } from 'react';
import { User } from '../types';
import { axiosInstance } from '../utils/axiosInstance';
interface UserContext {
users: Array<User>;
}
export const usersContext = createContext<UserContext | undefined>(undefined);
interface UsersProviderProps {
children: ReactNode;
}
export const UsersProvider = ({ children }: UsersProviderProps) => {
const [users, setUsers] = useState<Array<User>>([]);
const getUsers = async () => {
try {
const { data } = await axiosInstance.get<Array<User>>(
`${process.env.REACT_APP_API_URL}/api/v1/Users/student/search?query=`,
);
console.log(data);
setUsers(data);
} catch (e) {
console.log(e);
}
};
useEffect(() => {
setTimeout(() => {
getUsers();
}, 200);
}, []);
return (
<usersContext.Provider
value={{
users,
}}
>
{children}
</usersContext.Provider>
);
};

View File

@ -28,8 +28,10 @@ export interface Course {
} }
export interface User { export interface User {
name?: string; email: string;
surname?: string; id: number;
name: string;
surname: string;
} }
export interface SchedulerEvent { export interface SchedulerEvent {