import React, { useState } from "react"; import CardRestaurant from "./CardRestaurant"; import SearchPanel from "../Input/SearchPanel"; import { makeStyles } from '@material-ui/core/styles'; import { useSelector, useDispatch } from "react-redux"; import { IconButton } from "@material-ui/core"; import TuneIcon from '@material-ui/icons/Tune'; import Chip from '@material-ui/core/Chip'; import { restaurantTypes } from '../../config.js'; import { setTags, setTypes } from "../../actions"; import { decodeTags, compareArrays } from "../../Services" const useStyles = makeStyles((theme) => ({ root: { display: 'flex', justifyContent: 'center', flexWrap: 'wrap', '& > *': { margin: theme.spacing(0.5), }, } })) export default function SearchResults() { var results = useSelector((store) => store.searchResults); const filterTags = useSelector((store) => store.data.filters.tags); const filterTypes = useSelector((store) => store.data.filters.types); const dispatch = useDispatch(); const [collapsed, setCollapsed] = useState(true); const array = Array.from(results); const classes = useStyles(); var count = results.length; const filterRestaurants = () => { if(filterTags.length > 0 || filterTypes.length > 0){ return array.filter((restaurant) => { return filterTypes.includes(restaurant.type) || compareArrays(filterTags, decodeTags(restaurant.tags)) }) } else { return array; } } const restaurants = filterRestaurants().map((restaurant) => ( )); const availableTags = [ "Płatność kartą", "Lubimy zwierzaki", "Bezglutenowe", "Wegańskie", "Wegetariańskie", "Podajemy alkohol", "Dowozimy", ]; const handleTagClick = (tag) => { let tags = [...filterTags]; if(tags.includes(tag)){ const filtered = tags.filter((filterTag) => { return tag !== filterTag; }) dispatch(setTags(filtered)) } else { tags.push(tag) dispatch(setTags(tags)) } } const handleTypeClick = (type) => { let types = [...filterTypes]; if(types.includes(type)){ const filtered = types.filter((filterType) => { return type !== filterType; }) dispatch(setTypes(filtered)) } else { types.push(type) dispatch(setTypes(types)) } } const toggleCollapsed = () => { if(collapsed){ document.getElementById("collapsible").style.maxHeight = "400px"; setCollapsed(false); } else { document.getElementById("collapsible").style.maxHeight = "0px"; setCollapsed(true); } } const tags = availableTags.map((tag) => { if(filterTags.includes(tag)){ return ((
handleTagClick(tag)}/>
)) } else { return ((
handleTagClick(tag)}/>
)) } }) const types = restaurantTypes.map((type) => { if(filterTypes.includes(type)){ return (
handleTypeClick(type)}/>
) } else { return (
handleTypeClick(type)}/>
) } }) return (
toggleCollapsed()}>

Tagi

{tags}

Rodzaje kuchni

{types}

Znaleziono: {count}

{restaurants}
); }