mirror of
https://github.com/IgnatZakalinsky/home-works.git
synced 2026-01-29 12:32:47 +00:00
98 lines
2.8 KiB
TypeScript
98 lines
2.8 KiB
TypeScript
import React, {useEffect, useState} from 'react'
|
||
import s2 from '../../s1-main/App.module.css'
|
||
import s from './HW14.module.css'
|
||
import axios from 'axios'
|
||
import SuperDebouncedInput from './common/c8-SuperDebouncedInput/SuperDebouncedInput'
|
||
import {useSearchParams} from 'react-router-dom'
|
||
|
||
/*
|
||
* 1 - дописать функцию onChangeTextCallback в SuperPagination
|
||
* 2 - дописать функцию sendQuery в HW14
|
||
* 3 - дописать функцию onChangeText в HW14
|
||
* 4 - сделать стили в соответствии с дизайном
|
||
* 5 - добавить HW14 в HW5/pages/JuniorPlus
|
||
* */
|
||
|
||
const getTechs = (find: string) => {
|
||
return axios
|
||
.get<{ techs: string[] }>(
|
||
'https://incubator-personal-page-back.herokuapp.com/api/3.0/homework/test2',
|
||
{params: {find}}
|
||
)
|
||
.catch((e) => {
|
||
alert(e.response?.data?.errorText || e.message)
|
||
})
|
||
}
|
||
|
||
const HW14 = () => {
|
||
const [find, setFind] = useState('')
|
||
const [isLoading, setLoading] = useState(false)
|
||
const [searchParams, setSearchParams] = useSearchParams()
|
||
const [techs, setTechs] = useState<string[]>([])
|
||
|
||
const sendQuery = (value: string) => {
|
||
setLoading(true)
|
||
getTechs(value)
|
||
.then((res) => {
|
||
// делает студент
|
||
|
||
if (res) {
|
||
setTechs(res.data.techs)
|
||
}
|
||
setLoading(false)
|
||
|
||
//
|
||
})
|
||
}
|
||
|
||
const onChangeText = (value: string) => {
|
||
setFind(value)
|
||
// делает студент
|
||
|
||
const findQuery: { find?: string } = value ? {find: value} : {} // если нет - то не записывать в урл
|
||
const {find, ...lastQueries} = Object.fromEntries(searchParams)
|
||
|
||
setSearchParams({...lastQueries, ...findQuery})
|
||
|
||
//
|
||
}
|
||
|
||
useEffect(() => {
|
||
const params = Object.fromEntries(searchParams)
|
||
sendQuery(params.find || '')
|
||
setFind(params.find || '')
|
||
}, [])
|
||
|
||
const mappedTechs = techs.map(t => (
|
||
<div key={t} id={'hw14-tech-' + t} className={s.tech}>
|
||
{t}
|
||
</div>
|
||
))
|
||
|
||
return (
|
||
<div id={'hw14'}>
|
||
<div className={s2.hwTitle}>Homework #14</div>
|
||
|
||
<div className={s2.hw}>
|
||
<SuperDebouncedInput
|
||
id={'hw14-super-debounced-input'}
|
||
value={find}
|
||
onChangeText={onChangeText}
|
||
onDebouncedChange={sendQuery}
|
||
/>
|
||
{isLoading ? (
|
||
<div id={'hw14-loading'}>
|
||
...ищем
|
||
</div>
|
||
) : (
|
||
<br/>
|
||
)}
|
||
|
||
{mappedTechs}
|
||
</div>
|
||
</div>
|
||
)
|
||
}
|
||
|
||
export default HW14
|