3577 |
stevensc |
1 |
import React, { useEffect, useState } from 'react'
|
|
|
2 |
import { useDispatch } from 'react-redux'
|
|
|
3 |
import { Autocomplete, TextField } from '@mui/material'
|
|
|
4 |
|
|
|
5 |
import { axios } from '@app/utils'
|
|
|
6 |
import { addNotification } from '@app/redux/notification/notification.actions'
|
|
|
7 |
|
|
|
8 |
export default function SearchInput({ onChange }) {
|
|
|
9 |
const [search, setSearch] = useState('')
|
|
|
10 |
const [value, setValue] = useState(null)
|
|
|
11 |
const [loading, setLoading] = useState(false)
|
|
|
12 |
const [options, setOptions] = useState([])
|
|
|
13 |
const dispatch = useDispatch()
|
|
|
14 |
|
|
|
15 |
useEffect(() => {
|
|
|
16 |
const SEARCH_URL = '/helpers/search-people?search='
|
|
|
17 |
|
|
|
18 |
const searchMember = (search = '') => {
|
|
|
19 |
setLoading(true)
|
|
|
20 |
axios
|
|
|
21 |
.get(SEARCH_URL + search + '&page=1')
|
|
|
22 |
.then((response) => {
|
|
|
23 |
const { data } = response.data
|
|
|
24 |
const users = data.current ? data.current.items : data
|
|
|
25 |
setOptions(users)
|
|
|
26 |
})
|
|
|
27 |
.catch((err) => {
|
|
|
28 |
dispatch(addNotification({ style: 'danger', msg: err.message }))
|
|
|
29 |
})
|
|
|
30 |
.finally(() => setLoading(false))
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
searchMember(search)
|
|
|
34 |
}, [search])
|
|
|
35 |
|
|
|
36 |
return (
|
|
|
37 |
<>
|
|
|
38 |
<Autocomplete
|
|
|
39 |
value={value}
|
|
|
40 |
getOptionLabel={(option) => option.contact}
|
|
|
41 |
noOptionsText='Sin resultados'
|
|
|
42 |
loadingText='Buscando...'
|
|
|
43 |
onInputChange={(event, value) => setSearch(value)}
|
|
|
44 |
onChange={(event, newValue) => {
|
|
|
45 |
setValue(newValue)
|
|
|
46 |
onChange(newValue)
|
|
|
47 |
}}
|
|
|
48 |
options={options}
|
|
|
49 |
loading={loading}
|
|
|
50 |
renderInput={(params) => <TextField label='Para' {...params} />}
|
|
|
51 |
/>
|
|
|
52 |
</>
|
|
|
53 |
)
|
|
|
54 |
}
|