2021-10-24 20:14:04 +00:00
|
|
|
import React, { useState } from 'react';
|
|
|
|
import { useFormikContext } from 'formik';
|
|
|
|
import SearchableSelect, { Option } from '@/components/elements/SearchableSelect';
|
|
|
|
import { Node, searchNodes } from '@/api/admin/node';
|
|
|
|
|
2021-10-29 06:04:28 +00:00
|
|
|
export default ({ node, setNode }: { node: Node | null, setNode: (_: Node | null) => void }) => {
|
|
|
|
const { setFieldValue } = useFormikContext();
|
2021-10-24 20:14:04 +00:00
|
|
|
|
|
|
|
const [ nodes, setNodes ] = useState<Node[] | null>(null);
|
|
|
|
|
|
|
|
const onSearch = async (query: string) => {
|
2021-10-29 06:04:28 +00:00
|
|
|
setNodes(await searchNodes({ filters: { name: query } }));
|
2021-10-24 20:14:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const onSelect = (node: Node | null) => {
|
|
|
|
setNode(node);
|
2021-10-29 06:04:28 +00:00
|
|
|
setFieldValue('nodeId', node?.id || null);
|
2021-10-24 20:14:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const getSelectedText = (node: Node | null): string => node?.name || '';
|
|
|
|
|
|
|
|
return (
|
|
|
|
<SearchableSelect
|
|
|
|
id={'nodeId'}
|
|
|
|
name={'nodeId'}
|
|
|
|
label={'Node'}
|
|
|
|
placeholder={'Select a node...'}
|
|
|
|
items={nodes}
|
|
|
|
selected={node}
|
|
|
|
setSelected={setNode}
|
|
|
|
setItems={setNodes}
|
|
|
|
onSearch={onSearch}
|
|
|
|
onSelect={onSelect}
|
|
|
|
getSelectedText={getSelectedText}
|
|
|
|
nullable
|
|
|
|
>
|
|
|
|
{nodes?.map(d => (
|
|
|
|
<Option key={d.id} selectId={'nodeId'} id={d.id} item={d} active={d.id === node?.id}>
|
|
|
|
{d.name}
|
|
|
|
</Option>
|
|
|
|
))}
|
|
|
|
</SearchableSelect>
|
|
|
|
);
|
|
|
|
};
|