Skip to content

Commit

Permalink
Temporary remove search (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
sashachabin authored Dec 20, 2023
1 parent d2ae7e7 commit cf1064d
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 344 deletions.
14 changes: 0 additions & 14 deletions components/SidePage/SidePage.jsx

This file was deleted.

20 changes: 0 additions & 20 deletions components/SidePage/SidePage.module.css

This file was deleted.

90 changes: 0 additions & 90 deletions components/SidePage/Suggestions/Suggestions.jsx

This file was deleted.

116 changes: 0 additions & 116 deletions components/SidePage/Suggestions/Suggestions.module.css

This file was deleted.

118 changes: 18 additions & 100 deletions components/Toolbar/Toolbar.jsx
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}
/>
</>
)
}
4 changes: 0 additions & 4 deletions public/Search.svg

This file was deleted.

0 comments on commit cf1064d

Please sign in to comment.