-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #779 from marcelhillesheim/feature/player_autocomp…
…lete_v2 Feature/player autocomplete v2
- Loading branch information
Showing
3 changed files
with
98 additions
and
22 deletions.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
rcongui/src/components/form/custom/PlayerAutocompletion.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import React, {useEffect, useMemo, useRef, useState} from "react"; | ||
import {debounce} from "lodash"; | ||
import {handle_http_errors, postData, showResponse} from "@/utils/fetchUtils"; | ||
import Autocomplete from "@mui/material/Autocomplete"; | ||
import Typography from "@mui/material/Typography"; | ||
import {Avatar, Box} from "@mui/material"; | ||
import TextField from "@mui/material/TextField"; | ||
|
||
export default function PlayerAutocompletion({player, setPlayer}) { | ||
|
||
const [suggestions, setSuggestions] = useState([]); | ||
|
||
const nameRef = useRef(player.name); | ||
|
||
useEffect(() => { | ||
debouncedFetchSuggestions(); | ||
nameRef.current = player.name; | ||
}, [player.name]); | ||
|
||
const fetchSuggestions = () => { | ||
setSuggestions([]); | ||
|
||
if (!nameRef.current) { | ||
return; | ||
} | ||
|
||
const params = {page_size: 15, | ||
page: 1, | ||
player_name: nameRef.current, | ||
exact_name_match: false, | ||
ignore_accent: true, | ||
}; | ||
postData( | ||
`${process.env.REACT_APP_API_URL}get_players_history`, | ||
params | ||
) | ||
.then((response) => showResponse(response, "get_players_history")) | ||
.then((data) => { | ||
if (data.failed) { | ||
return; | ||
} | ||
setSuggestions(data.result.players); | ||
}) | ||
.catch(handle_http_errors); | ||
}; | ||
|
||
const debouncedFetchSuggestions = useMemo( | ||
() => debounce(fetchSuggestions, 300), | ||
[] | ||
); | ||
|
||
return <Autocomplete | ||
freeSolo | ||
autoHighlight | ||
fullWidth | ||
options={suggestions} | ||
filterOptions={(options) => options} | ||
onChange={(event, value, reason) => { | ||
setPlayer(prev => ({...prev, | ||
name: value?.names[0]?.name ?? '', | ||
player_id: value?.player_id ?? prev.player_id, | ||
})); | ||
}} | ||
getOptionLabel={(option) => option.names[0]?.name} | ||
renderOption={(props, option) => { | ||
return <Box {...props}> | ||
<Avatar | ||
variant="square" | ||
src={option.steaminfo?.profile?.avatarfull} | ||
> | ||
{option.names[0]?.name[0].toUpperCase()} | ||
</Avatar> | ||
<Typography sx={{marginLeft: 1}}>{option.names[0]?.name}</Typography> | ||
</Box> | ||
} | ||
} | ||
inputValue={player.name} | ||
onInputChange={(event, value) => setPlayer(prev => ({...prev, name: value}))} | ||
renderInput={(params) => | ||
<TextField | ||
{...params} | ||
value={player.name} | ||
label={"Name"} | ||
type="text" | ||
/>} | ||
/> | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters