All files / src/components Dropdown.tsx

61.11% Statements 22/36
10% Branches 1/10
46.15% Functions 6/13
61.76% Lines 21/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142            4x                                                 4x                                       4x 12x 12x 12x 12x 12x   12x                 12x                 12x 8x 8x                         8x   8x     12x 4x 4x                       4x   4x     12x                                                    
import React, { useState, useContext, useEffect, MouseEvent, useMemo } from 'react';
import { coursesContext } from '../contexts/CoursesProvider';
import { studentsContext } from '../contexts/StudentsProvider';
import { Course, Student } from '../types';
import styled from 'styled-components';
 
const DropdownContainer = styled.div`
  position: relative;
  z-index: 99999999;
  max-height: 396px;
  border-radius: 3px;
  overflow-y: auto;
  opacity: 0.97;
  box-shadow: 0.05em 0.2em 0.6em rgba(0, 0, 0, 0.2);
  scroll-snap-type: y mandatory;
  scroll-behavior: smooth;
  ::-webkit-scrollbar-track {
    background-color: #f2f4f7;
    border-radius: 10px;
  }
  ::-webkit-scrollbar {
    background-color: #f2f4f7;
    width: 5px;
    border-style: none;
  }
  ::-webkit-scrollbar-thumb {
    border-radius: 10px;
    background-color: #4b4b4b;
  }
`;
 
const CourseContainer = styled.div`
  padding: 5px;
  padding-left: 20px;
  background-color: #f2f4f7;
  font-size: 16px;
  font-weight: 500;
  scroll-snap-align: end;
  :hover {
    background-color: #eceef4;
    cursor: pointer;
  }
`;
 
interface DropdownProps {
  open: boolean;
  input: string;
  handleCloseDropdown: () => void;
  selectedOption: string;
}
 
export const Dropdown = ({ open, input, handleCloseDropdown, selectedOption }: DropdownProps) => {
  const { courses, selectBasketNames, addCourseToBasket, changeStudent, getStudentTimetablesHistory } = useContext(coursesContext)!;
  const { students, changeSelectedStudent } = useContext(studentsContext)!;
  const basketNames = useMemo(() => selectBasketNames(), [selectBasketNames]);
  const [filteredCourses, setFilteredCourses] = useState<Array<Course>>([]);
  const [filteredStudents, setFilteredStudents] = useState<Array<Student>>([]);
 
  const onCourseClick = (event: MouseEvent) => {
    const target = event.currentTarget;
    if (target.id && target.textContent) {
      const course = filteredCourses.find(({ id }) => id.toString() === target.id)!;
      addCourseToBasket(course);
      handleCloseDropdown();
    }
  };
 
  const onUserClick = (event: MouseEvent) => {
    const target = event.currentTarget;
    //to be moved to students provider
    changeStudent(target.id);
    changeSelectedStudent(Number(target.id));
    
    handleCloseDropdown();
  };
 
  useEffect(() => {
    const filterCourses = (input: string) => {
      const filteredCourses = courses.filter(
        ({ name }) =>
          name
            .toLowerCase()
            .normalize('NFD')
            .replace(/[\u0300-\u036f]/g, '')
            .includes(
              input
                .toLowerCase()
                .normalize('NFD')
                .replace(/[\u0300-\u036f]/g, ''),
            ) && !basketNames.includes(name),
      );
      setFilteredCourses(filteredCourses);
    };
    filterCourses(input);
  }, [basketNames, courses, input]);
 
  useEffect(() => {
    const filterUsers = (input: string) => {
      const filteredUsers = students.filter(({ name, surname }) =>
        (name + surname)
          .toLowerCase()
          .normalize('NFD')
          .replace(/[\u0300-\u036f]/g, '')
          .includes(
            input
              .toLowerCase()
              .normalize('NFD')
              .replace(/[\u0300-\u036f]/g, ''),
          ),
      );
      setFilteredStudents(filteredUsers);
    };
    filterUsers(input);
  }, [students, input]);
 
  return (
    <DropdownContainer>
      {open && (
        <>
          {selectedOption === 'studenci' ? (
            <div>
              {filteredStudents.map(({ email, id }, index) => (
                <CourseContainer key={index} id={id.toString()} onClick={onUserClick}>
                  <p>{email}</p>
                </CourseContainer>
              ))}
            </div>
          ) : (
            <div>
              {filteredCourses.map(({ name, id }, index) => (
                <CourseContainer key={index} id={id.toString()} onClick={onCourseClick}>
                  <p>{name} </p>
                </CourseContainer>
              ))}
            </div>
          )}
        </>
      )}
    </DropdownContainer>
  );
};