2020-09-19 00:17:23 +02:00
|
|
|
import React, { useState, useContext, useEffect, MouseEvent, ChangeEvent } from 'react';
|
2020-08-17 23:56:34 +02:00
|
|
|
import axios from 'axios';
|
|
|
|
import { Input } from '@material-ui/core';
|
|
|
|
import ClickAwayListener from '@material-ui/core/ClickAwayListener';
|
|
|
|
import { coursesContext } from '../contexts/CoursesProvider';
|
2020-08-23 16:10:10 +02:00
|
|
|
import { Course, Basket } from '../types';
|
2020-08-17 23:56:34 +02:00
|
|
|
import styled from 'styled-components';
|
|
|
|
import { makeStyles } from '@material-ui/core/styles';
|
|
|
|
|
2020-10-01 19:51:06 +02:00
|
|
|
const DropdownStyled = styled.div`
|
|
|
|
max-height: 420px;
|
|
|
|
overflow-y: auto;
|
|
|
|
scroll-snap-type: y mandatory;
|
|
|
|
scroll-behavior: smooth;
|
2020-10-08 20:21:52 +02:00
|
|
|
z-index: 100;
|
|
|
|
position: relative;
|
|
|
|
border-radius:0px 0px 0px 15px;
|
|
|
|
::-webkit-scrollbar-track {
|
|
|
|
border-radius: 10px;
|
|
|
|
background-color: #f5f5f5;
|
|
|
|
}
|
|
|
|
::-webkit-scrollbar {
|
|
|
|
width: 12px;
|
|
|
|
background-color: #f5f5f5;
|
|
|
|
}
|
|
|
|
::-webkit-scrollbar-thumb {
|
|
|
|
border-radius: 10px;
|
|
|
|
background-color: #d4b851;
|
|
|
|
border: 1px solid;
|
|
|
|
}
|
2020-10-01 19:51:06 +02:00
|
|
|
`;
|
|
|
|
|
2020-08-17 23:56:34 +02:00
|
|
|
const CourseStyled = styled.div`
|
|
|
|
position: relative;
|
|
|
|
z-index: 10;
|
|
|
|
padding: 5px;
|
|
|
|
padding-left: 20px;
|
|
|
|
background-color: #e6c759;
|
|
|
|
font-size: 18px;
|
|
|
|
font-family: Lato;
|
2020-10-01 19:51:06 +02:00
|
|
|
scroll-snap-align: end;
|
2020-10-08 20:21:52 +02:00
|
|
|
border-bottom:1px solid;
|
2020-08-17 23:56:34 +02:00
|
|
|
:hover {
|
|
|
|
background-color: #d4b851;
|
|
|
|
cursor: pointer;
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
const useStyles = makeStyles({
|
|
|
|
topbarInput: {
|
|
|
|
marginTop: '8px',
|
|
|
|
width: '100%',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-08-20 18:14:28 +02:00
|
|
|
interface DropdownProps {
|
|
|
|
clearInput: boolean;
|
2020-08-23 16:02:52 +02:00
|
|
|
handleClearInput: () => void;
|
2020-08-20 18:14:28 +02:00
|
|
|
}
|
|
|
|
|
2020-08-23 16:02:52 +02:00
|
|
|
export const Dropdown = ({ clearInput, handleClearInput }: DropdownProps) => {
|
2020-08-17 23:56:34 +02:00
|
|
|
const classes = useStyles();
|
|
|
|
|
|
|
|
const [open, setOpen] = React.useState(false);
|
|
|
|
const [input, setInput] = useState<string>('');
|
|
|
|
|
|
|
|
//courses - choosenCourses
|
|
|
|
const [filteredCourses, setFilteredCourses] = useState<Array<Course>>([]);
|
|
|
|
|
2020-08-23 17:22:50 +02:00
|
|
|
const { courses, basket, addToBasket } = useContext(coursesContext)!;
|
2020-08-17 23:56:34 +02:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const filterCourses = (input: string) => {
|
2020-08-23 17:22:50 +02:00
|
|
|
const choosenCoursesNames = basket.map(({ name }) => name.trim());
|
2020-08-17 23:56:34 +02:00
|
|
|
const filteredCourses = courses.filter(
|
|
|
|
({ name }) => name.toLowerCase().includes(input.toLowerCase()) && !choosenCoursesNames.includes(name),
|
|
|
|
);
|
|
|
|
setFilteredCourses(filteredCourses);
|
|
|
|
};
|
|
|
|
filterCourses(input);
|
2020-08-23 17:22:50 +02:00
|
|
|
}, [input, open, basket]);
|
2020-08-17 23:56:34 +02:00
|
|
|
|
2020-08-23 16:02:52 +02:00
|
|
|
useEffect(() => {
|
2020-09-19 00:17:23 +02:00
|
|
|
clearInput && (setInput(''), handleClearInput());
|
2020-08-23 16:02:52 +02:00
|
|
|
}, [clearInput]);
|
2020-08-20 18:14:28 +02:00
|
|
|
|
2020-09-19 00:17:23 +02:00
|
|
|
const handleChange = (event: ChangeEvent<HTMLInputElement>) => setInput(event.target.value);
|
2020-08-17 23:56:34 +02:00
|
|
|
|
|
|
|
const handleClick = () => setOpen(true);
|
|
|
|
|
|
|
|
const handleClickAway = () => setOpen(false);
|
|
|
|
|
|
|
|
const onCourseClick = async (event: MouseEvent) => {
|
|
|
|
const target = event.currentTarget;
|
|
|
|
if (target.id && target.textContent) {
|
2020-10-01 19:51:06 +02:00
|
|
|
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-08-17 23:56:34 +02:00
|
|
|
setOpen(false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ClickAwayListener onClickAway={handleClickAway}>
|
|
|
|
<div>
|
|
|
|
<Input
|
|
|
|
placeholder="Wyszukaj..."
|
|
|
|
inputProps={{ 'aria-label': 'description' }}
|
|
|
|
className={classes.topbarInput}
|
|
|
|
onChange={handleChange}
|
|
|
|
onClick={handleClick}
|
|
|
|
value={input}
|
|
|
|
/>
|
|
|
|
{open && (
|
|
|
|
<DropdownStyled>
|
|
|
|
{filteredCourses.map(({ name, id }, index) => (
|
|
|
|
<CourseStyled key={index} id={id.toString()} onClick={onCourseClick}>
|
|
|
|
<p>{name} </p>
|
|
|
|
</CourseStyled>
|
|
|
|
))}
|
|
|
|
</DropdownStyled>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</ClickAwayListener>
|
|
|
|
);
|
|
|
|
};
|