Files
home-works/src/s2-homeworks/hw14/HW14.tsx
2022-11-08 16:44:57 +03:00

98 lines
2.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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