-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d2ae7e7
commit cf1064d
Showing
6 changed files
with
18 additions
and
344 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,120 +1,38 @@ | ||
import React, { useEffect, useState, useRef, useCallback } from 'react' | ||
import React from 'react' | ||
import cn from 'classnames' | ||
import { SidePage } from '../SidePage/SidePage' | ||
import { API_HOST } from '../../consts/endpoints' | ||
|
||
import Close from './close.svg' | ||
import Download from './download.svg' | ||
import Menu from './menu.svg' | ||
import Search from './search.svg' | ||
|
||
import styles from './Toolbar.module.css' | ||
|
||
const debounce = (func, timeout = 300) => { | ||
let timer | ||
return (...args) => { | ||
clearTimeout(timer) | ||
timer = setTimeout(() => { | ||
func.apply(this, args) | ||
}, timeout) | ||
} | ||
} | ||
|
||
export const Toolbar = ({ pdf, menuActive, menuOnClick }) => { | ||
const [isOpenSidePage, setIsOpenSidePage] = useState(false) | ||
const [isLoadingSuggestion, setIsLoadingSuggestion] = useState(false) | ||
const [currentQuery, setCurrentQuery] = useState('') | ||
const searchInputRef = useRef(null) | ||
const [guideSuggestions, setGuideSuggestions] = useState([]) | ||
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
const handleOnChange = useCallback( | ||
debounce(async (e) => { | ||
const textInputValue = e.target.value | ||
if (textInputValue.length > 2) { | ||
setIsLoadingSuggestion(true) | ||
setCurrentQuery(textInputValue) | ||
const response = await fetch( | ||
`${API_HOST}/api/content/search?pattern=${e.target.value}` | ||
) | ||
const responseJson = await response.json() | ||
const { guideSuggestions } = responseJson | ||
setGuideSuggestions(guideSuggestions) | ||
} else { | ||
setGuideSuggestions([]) | ||
} | ||
setIsLoadingSuggestion(false) | ||
}), | ||
[guideSuggestions] | ||
) | ||
|
||
useEffect(() => { | ||
if (isOpenSidePage) { | ||
searchInputRef.current.focus() | ||
} | ||
}, [isOpenSidePage]) | ||
|
||
return ( | ||
<> | ||
<div className={styles.Toolbar}> | ||
{!isOpenSidePage ? ( | ||
<> | ||
{pdf && ( | ||
<a | ||
className={styles.Toolbar__item} | ||
href={pdf} | ||
target="_blank" | ||
aria-label="Скачать .pdf" | ||
> | ||
<Download /> | ||
</a> | ||
)} | ||
<> | ||
{pdf && ( | ||
<a | ||
className={styles.Toolbar__item} | ||
href={pdf} | ||
target="_blank" | ||
aria-label="Скачать .pdf" | ||
> | ||
<Download /> | ||
</a> | ||
)} | ||
|
||
{menuActive !== undefined && ( | ||
<button | ||
className={styles.Toolbar__item} | ||
onClick={() => setIsOpenSidePage(true)} | ||
className={cn(styles.Toolbar__item, styles.Toolbar__item_menu)} | ||
onClick={() => menuOnClick(!menuActive)} | ||
> | ||
<Search /> | ||
{menuActive ? <Close /> : <Menu />} | ||
</button> | ||
|
||
{menuActive !== undefined && ( | ||
<button | ||
className={cn(styles.Toolbar__item, styles.Toolbar__item_menu)} | ||
onClick={() => menuOnClick(!menuActive)} | ||
> | ||
{menuActive ? <Close /> : <Menu />} | ||
</button> | ||
)} | ||
</> | ||
) : ( | ||
<> | ||
<Search /> | ||
<div className={styles.customInput}> | ||
<input | ||
type="text" | ||
className={styles.Toolbar__input} | ||
placeholder="Например, скамья" | ||
ref={searchInputRef} | ||
onChange={handleOnChange} | ||
defaultValue={currentQuery} | ||
/> | ||
|
||
<button | ||
onClick={() => setIsOpenSidePage(false)} | ||
className={cn(styles.Toolbar__item)} | ||
> | ||
<Close /> | ||
</button> | ||
</div> | ||
</> | ||
)} | ||
)} | ||
</> | ||
</div> | ||
<SidePage | ||
items={guideSuggestions} | ||
isClose={!isOpenSidePage} | ||
isLoading={isLoadingSuggestion} | ||
query={currentQuery} | ||
/> | ||
</> | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.