frontend/src/components/Dropdown.tsx

117 lines
3.0 KiB
TypeScript
Raw Normal View History

2020-11-02 00:15:30 +01:00
import React, { useState, useContext, useEffect, MouseEvent, forwardRef } 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-11-02 00:15:30 +01:00
const WrapperIchuj = styled.div`
max-width: 1200px;
`;
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);
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-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;
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-02 00:15:30 +01:00
export const Dropdown = forwardRef(({ open, input, handleCloseDropdown }: DropdownProps, ref: any) => {
2020-08-17 23:56:34 +02:00
//courses - choosenCourses
const [filteredCourses, setFilteredCourses] = useState<Array<Course>>([]);
const { courses, basket, addToBasket } = useContext(coursesContext)!;
2020-08-17 23:56:34 +02:00
2020-11-02 00:15:30 +01:00
useEffect(() => {
console.log('wut');
}, [open, input, handleCloseDropdown]);
useEffect(() => {
console.log('input is: ', input);
}, [input]);
useEffect(() => {
console.log('is open: ', open);
}, [open]);
2020-08-17 23:56:34 +02:00
useEffect(() => {
const filterCourses = (input: string) => {
const choosenCoursesNames = basket.map(({ name }) => name.trim());
2020-08-17 23:56:34 +02: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, ''),
) && !choosenCoursesNames.includes(name),
2020-08-17 23:56:34 +02:00
);
setFilteredCourses(filteredCourses);
};
filterCourses(input);
2020-11-02 00:15:30 +01:00
}, [input, basket]);
2020-08-17 23:56:34 +02:00
const onCourseClick = async (event: MouseEvent) => {
const target = event.currentTarget;
if (target.id && target.textContent) {
const course = filteredCourses.find(({ id }) => id.toString() === target.id)!;
console.log('added course is');
console.log(course);
2020-08-23 16:10:10 +02:00
addToBasket(course);
2020-11-02 00:15:30 +01:00
handleCloseDropdown();
2020-08-17 23:56:34 +02:00
}
};
return (
2020-11-02 00:15:30 +01:00
<WrapperIchuj>
{open && (
<DropdownContainer>
{filteredCourses.map(({ name, id }, index) => (
<CourseContainer key={index} id={id.toString()} onClick={onCourseClick}>
<p>{name} </p>
</CourseContainer>
))}
</DropdownContainer>
)}
</WrapperIchuj>
2020-08-17 23:56:34 +02:00
);
2020-11-02 00:15:30 +01:00
});