2021-05-20 22:00:46 +00:00
|
|
|
import React, { useState } from 'react';
|
|
|
|
import { useFormikContext } from 'formik';
|
|
|
|
import { User } from '@/api/admin/users/getUsers';
|
|
|
|
import searchUsers from '@/api/admin/users/searchUsers';
|
|
|
|
import SearchableSelect, { Option } from '@/components/elements/SearchableSelect';
|
|
|
|
|
|
|
|
export default ({ selected }: { selected: User | null }) => {
|
|
|
|
const context = useFormikContext();
|
|
|
|
|
|
|
|
const [ user, setUser ] = useState<User | null>(selected);
|
|
|
|
const [ users, setUsers ] = useState<User[] | null>(null);
|
|
|
|
|
|
|
|
const onSearch = (query: string): Promise<void> => {
|
|
|
|
return new Promise((resolve, reject) => {
|
2021-08-02 16:23:58 +00:00
|
|
|
searchUsers({ username: query, email: query })
|
|
|
|
.then((users) => {
|
|
|
|
setUsers(users);
|
|
|
|
return resolve();
|
|
|
|
})
|
|
|
|
.catch(reject);
|
2021-05-20 22:00:46 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const onSelect = (user: User | null) => {
|
|
|
|
setUser(user);
|
|
|
|
context.setFieldValue('ownerId', user?.id || null);
|
|
|
|
};
|
|
|
|
|
|
|
|
const getSelectedText = (user: User | null): string => {
|
2021-08-05 04:16:52 +00:00
|
|
|
return 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>
|
|
|
|
);
|
|
|
|
};
|