mirror of
https://github.com/sergeyyarkov/devthread.ru.git
synced 2026-10-11 10:19:13 +03:00
fixed query state
This commit is contained in:
parent
c3884e8834
commit
7262f77425
@ -1,10 +1,17 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
import { useLocation } from '@reach/router'
|
||||||
import { Link } from 'gatsby'
|
import { Link } from 'gatsby'
|
||||||
|
|
||||||
const Categories = ({ children, pathname, setQuery }) => {
|
const Categories = ({ children, setQuery = () => null }) => {
|
||||||
|
const { search, pathname } = useLocation()
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="articles-categories">
|
<div className="articles-categories">
|
||||||
<div className={pathname === `/articles` ? 'tag-item active' : 'tag-item'}>
|
<div className={
|
||||||
|
pathname === `/articles` || (pathname === `/articles/` && search === '')
|
||||||
|
? 'tag-item active'
|
||||||
|
: 'tag-item'
|
||||||
|
}>
|
||||||
<Link onClick={() => setQuery('')} to='/articles'>Все</Link>
|
<Link onClick={() => setQuery('')} to='/articles'>Все</Link>
|
||||||
</div>
|
</div>
|
||||||
{children}
|
{children}
|
||||||
|
|||||||
@ -7,7 +7,7 @@ import slugify from '../../../helpers/slugify'
|
|||||||
const CategoryItem = ({ title }) => {
|
const CategoryItem = ({ title }) => {
|
||||||
const { pathname } = useLocation()
|
const { pathname } = useLocation()
|
||||||
return (
|
return (
|
||||||
<div className={pathname === `/category/${slugify(title)}` ? 'tag-item active' : 'tag-item'}>
|
<div className={pathname === `/category/${slugify(title)}` || pathname === `/category/${slugify(title)}/` ? 'tag-item active' : 'tag-item'}>
|
||||||
<Link to={`/category/${slugify(title)}`}>{title}</Link>
|
<Link to={`/category/${slugify(title)}`}>{title}</Link>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
|||||||
@ -33,7 +33,7 @@ const ArticlesPage = ({ data: { allMarkdownRemark: edges }, }) => {
|
|||||||
|| frontmatter.tags.join('').toLowerCase().includes(query.toLowerCase())
|
|| frontmatter.tags.join('').toLowerCase().includes(query.toLowerCase())
|
||||||
)
|
)
|
||||||
}, [query])
|
}, [query])
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
if (query !== '') setFiltredArticles(filterArticles(allArticles.edges))
|
if (query !== '') setFiltredArticles(filterArticles(allArticles.edges))
|
||||||
}, [setFiltredArticles, filterArticles, allArticles.edges, query, pathname])
|
}, [setFiltredArticles, filterArticles, allArticles.edges, query, pathname])
|
||||||
@ -49,7 +49,7 @@ const ArticlesPage = ({ data: { allMarkdownRemark: edges }, }) => {
|
|||||||
<div className="articles-heading">
|
<div className="articles-heading">
|
||||||
<h1>Статьи</h1>
|
<h1>Статьи</h1>
|
||||||
</div>
|
</div>
|
||||||
<Categories pathname={pathname} setQuery={setQuery}>
|
<Categories setQuery={setQuery}>
|
||||||
{categories.map(({ node }, i) => <CategoryItem key={i} title={node.frontmatter.title} />)}
|
{categories.map(({ node }, i) => <CategoryItem key={i} title={node.frontmatter.title} />)}
|
||||||
</Categories>
|
</Categories>
|
||||||
<Search allArticles={allArticles.edges} filterArticles={filterArticles} articlesLength={edges.edges.length} resultsLength={filtredArticles.length} query={query} setQuery={setQuery} setFiltredArticles={setFiltredArticles} />
|
<Search allArticles={allArticles.edges} filterArticles={filterArticles} articlesLength={edges.edges.length} resultsLength={filtredArticles.length} query={query} setQuery={setQuery} setFiltredArticles={setFiltredArticles} />
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user