2021-05-20 22:00:46 +00:00
|
|
|
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';
|
2022-02-27 20:26:26 +00:00
|
|
|
import { searchUserAccounts } from '@/api/admin/users';
|
2021-05-20 22:00:46 +00:00
|
|
|
|
2021-10-23 21:19:41 +00:00
|
|
|
export default ({ selected }: { selected?: User }) => {
|
2021-10-29 06:04:28 +00:00
|
|
|
const { setFieldValue } = useFormikContext();
|
2021-05-20 22:00:46 +00:00
|
|
|
|
2021-10-23 21:19:41 +00:00
|
|
|
const [ user, setUser ] = useState<User | null>(selected || null);
|
2021-05-20 22:00:46 +00:00
|
|
|
const [ users, setUsers ] = useState<User[] | null>(null);
|
|
|
|
|
2021-10-10 20:13:10 +00:00
|
|
|
const onSearch = async (query: string) => {
|
|
|
|
setUsers(
|
|
|
|
await searchUserAccounts({ filters: { username: query, email: query } })
|
|
|
|
);
|
2021-05-20 22:00:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const onSelect = (user: User | null) => {
|
|
|
|
setUser(user);
|
2021-10-29 06:04:28 +00:00
|
|
|
setFieldValue('ownerId', user?.id || null);
|
2021-05-20 22:00:46 +00:00
|
|
|
};
|
|
|
|
|
2021-10-10 20:13:10 +00:00
|
|
|
const getSelectedText = (user: User | null): string => user?.email || '';
|
2021-05-20 22:00:46 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<SearchableSelect
|
2021-07-25 21:51:39 +00:00
|
|
|
id={'ownerId'}
|
|
|
|
name={'ownerId'}
|
|
|
|
label={'Owner'}
|
|
|
|
placeholder={'Select a user...'}
|
2021-05-20 22:00:46 +00:00
|
|
|
items={users}
|
|
|
|
selected={user}
|
|
|
|
setSelected={setUser}
|
|
|
|
setItems={setUsers}
|
|
|
|
onSearch={onSearch}
|
|
|
|
onSelect={onSelect}
|
|
|
|
getSelectedText={getSelectedText}
|
|
|
|
nullable
|
|
|
|
>
|
|
|
|
{users?.map(d => (
|
2021-07-25 21:51:39 +00:00
|
|
|
<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}
|
2021-05-20 22:00:46 +00:00
|
|
|
</Option>
|
|
|
|
))}
|
|
|
|
</SearchableSelect>
|
|
|
|
);
|
|
|
|
};
|