misc_pterodactyl-panel/resources/scripts/components/admin/servers/OwnerSelect.tsx

49 lines
1.5 KiB
TypeScript
Raw Normal View History

import React, { useState } from 'react';
import { useFormikContext } from 'formik';
import SearchableSelect, { Option } from '@/components/elements/SearchableSelect';
2022-02-27 16:40:51 +00:00
import { User } from '@definitions/admin';
import { searchUserAccounts } from '@/api/admin/user';
export default ({ selected }: { selected?: User }) => {
2021-10-29 06:04:28 +00:00
const { setFieldValue } = useFormikContext();
const [ user, setUser ] = useState<User | null>(selected || null);
const [ users, setUsers ] = useState<User[] | null>(null);
const onSearch = async (query: string) => {
setUsers(
await searchUserAccounts({ filters: { username: query, email: query } })
);
};
const onSelect = (user: User | null) => {
setUser(user);
2021-10-29 06:04:28 +00:00
setFieldValue('ownerId', user?.id || null);
};
const getSelectedText = (user: User | null): string => user?.email || '';
return (
<SearchableSelect
id={'ownerId'}
name={'ownerId'}
label={'Owner'}
placeholder={'Select a user...'}
items={users}
selected={user}
setSelected={setUser}
setItems={setUsers}
onSearch={onSearch}
onSelect={onSelect}
getSelectedText={getSelectedText}
nullable
>
{users?.map(d => (
<Option key={d.id} selectId={'ownerId'} id={d.id} item={d} active={d.id === user?.id}>
2021-08-05 04:16:52 +00:00
{d.email}
</Option>
))}
</SearchableSelect>
);
};