|
1 | | -import React, { useEffect } from "react" |
2 | | -import styled from "styled-components" |
3 | | -import { FontAwesomeIcon } from "@fortawesome/react-fontawesome" |
4 | | - |
5 | | -import Title from "./title" |
| 1 | +import React from "react" |
6 | 2 | import prettyCategory from "../util/pretty-category" |
7 | | -import { getQueryParams, useQueryParamString } from "react-use-query-param-string" |
8 | | - |
9 | | -const Element = styled.div` |
10 | | - padding-top: 36px; |
11 | | - display: flex; |
12 | | - flex-direction: column; |
13 | | - justify-content: flex-start; |
14 | | - align-items: flex-start; |
15 | | -` |
16 | | - |
17 | | -const Category = styled.li` |
18 | | - font-size: var(--font-size-16); |
19 | | - color: var(--main-text-color); |
20 | | - display: flex; |
21 | | - padding: 0; |
22 | | - gap: 8px; |
23 | | -` |
24 | | - |
25 | | -const Categories = styled.ul` |
26 | | - list-style: none; |
27 | | - padding: 0; |
28 | | - margin: 10px; |
29 | | - line-height: 23px; |
30 | | -` |
31 | | - |
32 | | -const TickyBox = styled(props => <FontAwesomeIcon {...props} />)` |
33 | | - font-size: 16px; |
34 | | - color: var(--main-text-color); |
35 | | -` |
36 | | - |
37 | | -const separator = "," |
| 3 | +import TickyFilter from "./ticky-filter" |
38 | 4 |
|
39 | | -const toggleCategory = ( |
40 | | - category, |
41 | | - tickedCategories, |
42 | | - setTickedCategories, |
43 | | - filterer |
44 | | -) => { |
45 | | - if (tickedCategories.includes(category)) { |
46 | | - tickedCategories = tickedCategories.filter(item => item !== category) |
47 | | - } else { |
48 | | - tickedCategories = [...tickedCategories, category] // It's important to make a new array or nothing will be re-rendered |
49 | | - } |
50 | | - if (tickedCategories.length > 0) { |
51 | | - setTickedCategories(tickedCategories?.join(separator)) |
52 | | - } else { |
53 | | - // Clear this filter from the URL bar if there's nothing in it |
54 | | - setTickedCategories(undefined) |
55 | | - } |
56 | | - filterer && filterer(tickedCategories) |
57 | | -} |
58 | | - |
59 | | -const key = "categories" |
60 | 5 | const CategoryFilter = ({ categories, filterer }) => { |
61 | | - const [stringedTickedCategories, setTickedCategories, initialized] = useQueryParamString(key, undefined, true) |
62 | | - const realStringedTickedCategories = initialized ? stringedTickedCategories : getQueryParams() ? getQueryParams()[key] : undefined |
63 | | - |
64 | | - const tickedCategories = stringedTickedCategories ? stringedTickedCategories.split(separator) : [] |
65 | | - |
66 | | - const onClick = category => () => |
67 | | - toggleCategory( |
68 | | - category, |
69 | | - tickedCategories, |
70 | | - setTickedCategories, |
71 | | - filterer |
72 | | - ) |
73 | | - |
74 | | - useEffect(() => { // Make sure that even if the url is pasted in a browser, the list updates with the right value |
75 | | - if (realStringedTickedCategories && realStringedTickedCategories.length > 0) { |
76 | | - filterer(realStringedTickedCategories.split(separator)) |
77 | | - } |
78 | | - }, [realStringedTickedCategories], filterer) |
79 | | - |
80 | 6 | return ( |
81 | | - categories && <Element> |
82 | | - <Title>Category</Title> |
83 | | - <Categories> |
84 | | - {categories && |
85 | | - categories.map(category => ( |
86 | | - <Category |
87 | | - key={category} |
88 | | - onClick={onClick(category) |
89 | | - } |
90 | | - > |
91 | | - <div> |
92 | | - {tickedCategories.includes(category) ? ( |
93 | | - <TickyBox icon="square-check" title="ticked" /> |
94 | | - ) : ( |
95 | | - <TickyBox icon={["far", "square"]} title="unticked" /> |
96 | | - )} |
97 | | - </div> |
98 | | - <div>{prettyCategory(category)}</div> |
99 | | - </Category> |
100 | | - ))} |
101 | | - </Categories> |
102 | | - </Element> |
| 7 | + categories && <TickyFilter label="Category" queryKey="categories" entries={categories} filterer={filterer} |
| 8 | + prettify={prettyCategory} /> |
103 | 9 | ) |
104 | 10 | } |
105 | 11 |
|
|
0 commit comments