Files

108 lines
2.9 KiB
JavaScript
Raw Permalink Normal View History

'use client'
2026-04-21 19:33:26 +04:00
import {useEffect, useState, useMemo} from 'react'
import {useRouter} from 'next/navigation'
2022-05-21 17:55:16 +04:00
import TextField from '@mui/material/TextField'
import Autocomplete from '@mui/material/Autocomplete'
2022-05-21 18:43:09 +04:00
import Avatar from '@mui/material/Avatar'
import CircularProgress from '@mui/material/CircularProgress'
import Container from '@mui/material/Container'
2022-05-21 17:55:16 +04:00
import {jwennToutAwtis} from '../../lib/oki-api'
const IMAGE_URL = process.env.NEXT_PUBLIC_API_URL_ROOT || 'http://localhost:1337'
export default function ChecheAwtis() {
const router = useRouter()
const [open, setOpen] = useState(false)
const [options, setOptions] = useState([])
const loading = open && options.length === 0
useEffect(() => {
let active = true
if (!loading) {
return undefined
}
(async () => {
try {
2026-04-21 19:16:11 +04:00
const {data} = await jwennToutAwtis()
2023-07-22 13:47:46 +04:00
const filteredData = data.map(artiste => {
2026-04-21 19:16:11 +04:00
const firstLetter = artiste.alias[0].toUpperCase()
2022-05-21 17:55:16 +04:00
return {
firstLetter: /\d/.test(firstLetter) ? '0-9' : firstLetter,
2026-04-21 19:16:11 +04:00
...artiste
2022-05-21 17:55:16 +04:00
}
})
if (active) {
setOptions([...filteredData])
}
} catch (error) {
console.log('⚠️ Erreur', error.message)
}
})()
return () => {
active = false
}
}, [loading])
2026-04-21 19:33:26 +04:00
const sortedOptions = useMemo(() => {
return [...options].sort((a, b) =>
-b.firstLetter.localeCompare(a.firstLetter)
);
}, [options]);
2022-05-21 17:55:16 +04:00
return (
<Container
sx={{ display: 'flex', justifyContent: 'center', marginBottom: 3 }}
>
2022-05-21 17:55:16 +04:00
<Autocomplete
autoHighlight
disableClearable
disableCloseOnSelect
loading={loading}
open={open}
loadingText='Chargement...'
noOptionsText='Aucun résultat'
id='cheche-awtis'
2026-04-21 19:33:26 +04:00
options={sortedOptions}
groupBy={(option) => option?.firstLetter}
getOptionLabel={(option) => option?.alias}
renderOption={(props, option) => {
const {key, ...rest} = props;
return (
<li key={key} {...rest}>
<Avatar
style={{ marginRight: 8 }}
alt={option?.alias}
src={`${IMAGE_URL}${option?.photo?.formats?.thumbnail?.url}`}
/>
{option?.alias}
</li>
);
}}
sx={{ width: 300 }}
renderInput={(params) => (
2022-05-21 18:43:09 +04:00
<TextField
{...params}
label='Rechercher un artiste'
2026-04-21 19:33:26 +04:00
slotProps={{
...params.slotProps,
htmlInput: { ...params.slotProps.htmlInput },
2022-05-21 18:43:09 +04:00
}}
/>
)}
2022-05-21 17:55:16 +04:00
onChange={(event, newValue) => {
2026-04-21 19:33:26 +04:00
router.push(`/awtis/${newValue.slug}`);
2022-05-21 17:55:16 +04:00
}}
2026-04-21 19:33:26 +04:00
onOpen={() => setOpen(true)}
onClose={() => setOpen(false)}
2022-05-21 17:55:16 +04:00
/>
</Container>
)
}