2020-11-21 04:02:38 +01:00
|
|
|
import React, { useState, useContext, useEffect, MouseEvent } from 'react';
|
2020-08-17 23:56:34 +02:00
|
|
|
import { coursesContext } from '../contexts/CoursesProvider';
|
2020-10-21 20:42:18 +02:00
|
|
|
import { Course } from '../types';
|
2020-08-17 23:56:34 +02:00
|
|
|
import styled from 'styled-components';
|
|
|
|
|
2020-10-21 20:42:18 +02:00
|
|
|
const DropdownContainer = styled.div`
|
2020-11-02 00:15:30 +01:00
|
|
|
position: relative;
|
|
|
|
z-index: 99999999;
|
|
|
|
max-height: 420px;
|
|
|
|
border-radius: 3px;
|
|
|
|
overflow-y: auto;
|
|
|
|
box-shadow: 0.05em 0.2em 0.6em rgba(0, 0, 0, 0.2);
|
2020-10-01 19:51:06 +02:00
|
|
|
scroll-snap-type: y mandatory;
|
|
|
|
scroll-behavior: smooth;
|
2020-10-08 20:21:52 +02:00
|
|
|
::-webkit-scrollbar-track {
|
|
|
|
border-radius: 10px;
|
|
|
|
background-color: #f5f5f5;
|
|
|
|
}
|
|
|
|
::-webkit-scrollbar {
|
|
|
|
width: 12px;
|
|
|
|
background-color: #f5f5f5;
|
|
|
|
}
|
|
|
|
::-webkit-scrollbar-thumb {
|
|
|
|
border-radius: 10px;
|
2020-10-30 00:42:40 +01:00
|
|
|
background-color: black;
|
2020-10-08 20:21:52 +02:00
|
|
|
border: 1px solid;
|
|
|
|
}
|
2020-10-01 19:51:06 +02:00
|
|
|
`;
|
|
|
|
|
2020-10-21 20:42:18 +02:00
|
|
|
const CourseContainer = styled.div`
|
2020-08-17 23:56:34 +02:00
|
|
|
padding: 5px;
|
|
|
|
padding-left: 20px;
|
2020-10-30 00:42:40 +01:00
|
|
|
background-color: #f2f4f7;
|
2020-08-17 23:56:34 +02:00
|
|
|
font-size: 18px;
|
2020-10-30 00:42:40 +01:00
|
|
|
font-weight: 500;
|
2020-10-01 19:51:06 +02:00
|
|
|
scroll-snap-align: end;
|
2020-08-17 23:56:34 +02:00
|
|
|
:hover {
|
2020-11-02 00:15:30 +01:00
|
|
|
background-color: #eceef4;
|
2020-08-17 23:56:34 +02:00
|
|
|
cursor: pointer;
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2020-08-20 18:14:28 +02:00
|
|
|
interface DropdownProps {
|
2020-11-02 00:15:30 +01:00
|
|
|
open: boolean;
|
|
|
|
input: string;
|
|
|
|
handleCloseDropdown: () => void;
|
2020-08-20 18:14:28 +02:00
|
|
|
}
|
|
|
|
|
2020-11-21 04:02:38 +01:00
|
|
|
export const Dropdown = ({ open, input, handleCloseDropdown }: DropdownProps) => {
|
|
|
|
|
|
|
|
const { courses, basket, addCourseToBasket } = useContext(coursesContext)!;
|
|
|
|
const basketNames = basket.map(({ name }) => name.trim());
|
2020-11-02 00:15:30 +01:00
|
|
|
|
2020-11-21 04:02:38 +01:00
|
|
|
const [filteredCourses, setFilteredCourses] = useState<Array<Course>>([]);
|
2020-11-02 00:15:30 +01:00
|
|
|
|
2020-11-21 04:02:38 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
};
|
2020-11-02 00:15:30 +01:00
|
|
|
|
2020-08-17 23:56:34 +02:00
|
|
|
useEffect(() => {
|
|
|
|
const filterCourses = (input: string) => {
|
2020-11-21 04:02:38 +01:00
|
|
|
const filteredCourses = courses.filter(
|
2020-10-22 23:04:00 +02:00
|
|
|
({ name }) =>
|
|
|
|
name
|
|
|
|
.toLowerCase()
|
|
|
|
.normalize('NFD')
|
|
|
|
.replace(/[\u0300-\u036f]/g, '')
|
|
|
|
.includes(
|
|
|
|
input
|
|
|
|
.toLowerCase()
|
|
|
|
.normalize('NFD')
|
|
|
|
.replace(/[\u0300-\u036f]/g, ''),
|
2020-11-21 04:02:38 +01:00
|
|
|
) && !basketNames.includes(name),
|
2020-08-17 23:56:34 +02:00
|
|
|
);
|
|
|
|
setFilteredCourses(filteredCourses);
|
|
|
|
};
|
|
|
|
filterCourses(input);
|
2020-11-03 00:59:42 +01:00
|
|
|
}, [open, input, basket]);
|
2020-08-17 23:56:34 +02:00
|
|
|
|
|
|
|
return (
|
2020-11-08 13:40:11 +01:00
|
|
|
<DropdownContainer>
|
2020-11-02 00:15:30 +01:00
|
|
|
{open && (
|
2020-11-08 13:40:11 +01:00
|
|
|
<>
|
2020-11-02 00:15:30 +01:00
|
|
|
{filteredCourses.map(({ name, id }, index) => (
|
|
|
|
<CourseContainer key={index} id={id.toString()} onClick={onCourseClick}>
|
|
|
|
<p>{name} </p>
|
|
|
|
</CourseContainer>
|
|
|
|
))}
|
2020-11-08 13:40:11 +01:00
|
|
|
</>
|
2020-11-02 00:15:30 +01:00
|
|
|
)}
|
2020-11-08 13:40:11 +01:00
|
|
|
</DropdownContainer>
|
2020-08-17 23:56:34 +02:00
|
|
|
);
|
2020-11-21 04:02:38 +01:00
|
|
|
};
|