diff --git a/rcongui/src/components/form/custom/PlayerAutocompletion.jsx b/rcongui/src/components/form/custom/PlayerAutocompletion.jsx new file mode 100644 index 000000000..5e781176a --- /dev/null +++ b/rcongui/src/components/form/custom/PlayerAutocompletion.jsx @@ -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 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 + + {option.names[0]?.name[0].toUpperCase()} + + {option.names[0]?.name} + + } + } + inputValue={player.name} + onInputChange={(event, value) => setPlayer(prev => ({...prev, name: value}))} + renderInput={(params) => + } + /> +}; diff --git a/rcongui/src/pages/settings/console-admins/index.jsx b/rcongui/src/pages/settings/console-admins/index.jsx index f13e1650c..0b12001d7 100644 --- a/rcongui/src/pages/settings/console-admins/index.jsx +++ b/rcongui/src/pages/settings/console-admins/index.jsx @@ -28,6 +28,7 @@ import debounce from "lodash/debounce"; import { InputFileUpload } from "@/components/shared/InputFileUpload"; import exportFile from "@/utils/exportFile"; import { usePlayerSidebar } from "@/hooks/usePlayerSidebar"; +import PlayerAutocompletion from "@/components/form/custom/PlayerAutocompletion"; const INTENT = { ADD_ADMIN: "add_admin", @@ -65,14 +66,15 @@ export const action = async ({ request }) => { return results; }; -const initialAdmin = { - player_id: "", - name: "", - role: "", -}; - const ConsoleAdminsPage = () => { const { admins: serverAdmins, adminGroups } = useLoaderData(); + + const initialAdmin = { + player_id: "", + name: "", + role: adminGroups.includes("spectator") ? "spectator" : "", + }; + const [admins, setAdmins] = useState(serverAdmins); const [checked, setChecked] = useState(new Set()); const [searched, setSearched] = useState(""); @@ -274,14 +276,7 @@ const ConsoleAdminsPage = () => { }} > - + { }} > - +