2022-09-21 07:10:58 +00:00
|
|
|
import _ from 'lodash';
|
|
|
|
|
2022-11-21 07:51:55 +00:00
|
|
|
import { EdgeGroup } from '@/react/edge/edge-groups/types';
|
2022-09-21 07:10:58 +00:00
|
|
|
|
2022-11-21 07:51:55 +00:00
|
|
|
import { Select } from '@@/form-components/ReactSelect';
|
2023-03-06 20:25:04 +00:00
|
|
|
import { FormSection } from '@@/form-components/FormSection';
|
|
|
|
import { FormError } from '@@/form-components/FormError';
|
|
|
|
import { Link } from '@@/Link';
|
|
|
|
import { FormControl } from '@@/form-components/FormControl';
|
|
|
|
|
|
|
|
import { useEdgeGroups } from '../../edge-groups/queries/useEdgeGroups';
|
2022-09-21 07:10:58 +00:00
|
|
|
|
|
|
|
type SingleValue = EdgeGroup['Id'];
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
value: SingleValue[];
|
|
|
|
onChange: (value: SingleValue[]) => void;
|
2023-03-06 20:25:04 +00:00
|
|
|
error?: string | string[];
|
|
|
|
horizontal?: boolean;
|
|
|
|
isGroupVisible?(group: EdgeGroup): boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function EdgeGroupsSelector({
|
|
|
|
value,
|
|
|
|
onChange,
|
|
|
|
error,
|
|
|
|
horizontal,
|
|
|
|
isGroupVisible = () => true,
|
|
|
|
}: Props) {
|
|
|
|
const selector = (
|
|
|
|
<InnerSelector
|
|
|
|
value={value}
|
|
|
|
onChange={onChange}
|
|
|
|
isGroupVisible={isGroupVisible}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
return horizontal ? (
|
|
|
|
<FormControl errors={error} label="Edge Groups">
|
|
|
|
{selector}
|
|
|
|
</FormControl>
|
|
|
|
) : (
|
|
|
|
<FormSection title="Edge Groups">
|
|
|
|
<div className="form-group">
|
|
|
|
<div className="col-sm-12">{selector} </div>
|
|
|
|
{error && (
|
|
|
|
<div className="col-sm-12">
|
|
|
|
<FormError>{error}</FormError>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</FormSection>
|
|
|
|
);
|
2022-09-21 07:10:58 +00:00
|
|
|
}
|
|
|
|
|
2023-03-06 20:25:04 +00:00
|
|
|
function InnerSelector({
|
|
|
|
value,
|
|
|
|
onChange,
|
|
|
|
isGroupVisible,
|
|
|
|
}: {
|
|
|
|
isGroupVisible(group: EdgeGroup): boolean;
|
|
|
|
value: SingleValue[];
|
|
|
|
onChange: (value: SingleValue[]) => void;
|
|
|
|
}) {
|
|
|
|
const edgeGroupsQuery = useEdgeGroups();
|
|
|
|
|
2023-03-09 21:48:53 +00:00
|
|
|
const items = (edgeGroupsQuery.data || []).filter(isGroupVisible);
|
2023-03-06 20:25:04 +00:00
|
|
|
|
2022-09-21 07:10:58 +00:00
|
|
|
const valueGroups = _.compact(
|
|
|
|
value.map((id) => items.find((item) => item.Id === id))
|
|
|
|
);
|
|
|
|
|
2023-03-06 20:25:04 +00:00
|
|
|
return items.length ? (
|
2022-09-21 07:10:58 +00:00
|
|
|
<Select
|
2022-10-21 05:22:49 +00:00
|
|
|
aria-label="Edge groups"
|
2022-09-21 07:10:58 +00:00
|
|
|
options={items}
|
|
|
|
isMulti
|
|
|
|
getOptionLabel={(item) => item.Name}
|
|
|
|
getOptionValue={(item) => String(item.Id)}
|
|
|
|
value={valueGroups}
|
|
|
|
onChange={(value) => {
|
|
|
|
onChange(value.map((item) => item.Id));
|
|
|
|
}}
|
|
|
|
placeholder="Select one or multiple group(s)"
|
|
|
|
closeMenuOnSelect={false}
|
|
|
|
/>
|
2023-03-06 20:25:04 +00:00
|
|
|
) : (
|
|
|
|
<div className="small text-muted">
|
|
|
|
No Edge groups are available. Head over to the{' '}
|
|
|
|
<Link to="edge.groups">Edge groups view</Link> to create one.
|
|
|
|
</div>
|
2022-09-21 07:10:58 +00:00
|
|
|
);
|
|
|
|
}
|