/* * This file is part of caronte (https://github.com/eciavatta/caronte). * Copyright (c) 2020 Emiliano Ciavatta. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, version 3. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ import React, {Component} from 'react'; import Typed from 'typed.js'; import './Header.scss'; import {filtersDefinitions, filtersNames} from "./filters/FiltersDefinitions"; import {Link, withRouter} from "react-router-dom"; import ButtonField from "./fields/ButtonField"; class Header extends Component { constructor(props) { super(props); let newState = {}; filtersNames.forEach(elem => newState[`${elem}_active`] = false); this.state = newState; this.fetchStateFromLocalStorage = this.fetchStateFromLocalStorage.bind(this); } componentDidMount() { const options = { strings: ["caronte$ "], typeSpeed: 50, cursorChar: "❚" }; this.typed = new Typed(this.el, options); this.fetchStateFromLocalStorage(); if (typeof window !== "undefined") { window.addEventListener("quick-filters", this.fetchStateFromLocalStorage); } } componentWillUnmount() { this.typed.destroy(); if (typeof window !== "undefined") { window.removeEventListener("quick-filters", this.fetchStateFromLocalStorage); } } fetchStateFromLocalStorage() { let newState = {}; filtersNames.forEach(elem => newState[`${elem}_active`] = localStorage.getItem(`filters.${elem}`) === "true"); this.setState(newState); } render() { let quickFilters = filtersNames.filter(name => this.state[`${name}_active`]) .map(name => {filtersDefinitions[name]}) .slice(0, 5); return (

{ this.el = el; }}/>

{quickFilters}
); } } export default withRouter(Header);