Sha256: 26d492078da93d260ab98e6a96d6378a0dbc1cc54c01a5e0d311cb18c694ce05

Contents?: true

Size: 1.39 KB

Versions: 13

Compression:

Stored size: 1.39 KB

Contents

import React, { useState } from 'react';
import { useQuery } from '@apollo/client';
import { SelectVariant } from '@patternfly/react-core';
import {
  useForemanOrganization,
  useForemanLocation,
} from 'foremanReact/Root/Context/ForemanContext';
import { HOSTS, HOST_GROUPS, dataName } from '../../JobWizardConstants';
import { SearchSelect } from '../form/SearchSelect';
import hostsQuery from './hosts.gql';
import hostgroupsQuery from './hostgroups.gql';

export const useNameSearchGQL = apiKey => {
  const org = useForemanOrganization();
  const location = useForemanLocation();
  const [search, setSearch] = useState('');
  const queries = {
    [HOSTS]: hostsQuery,
    [HOST_GROUPS]: hostgroupsQuery,
  };
  const { loading, data } = useQuery(queries[apiKey], {
    variables: {
      search: [
        `name~"${search}"`,
        org ? `organization_id=${org.id}` : null,
        location ? `location_id=${location.id}` : null,
      ]
        .filter(i => i)
        .join(' and '),
    },
  });
  return [
    setSearch,
    {
      subtotal: data?.[dataName[apiKey]]?.totalCount,
      results:
        data?.[dataName[apiKey]]?.nodes.map(node => ({
          id: node.name,
          name: node.name,
        })) || [],
    },
    loading,
  ];
};

export const SelectGQL = props => (
  <SearchSelect
    {...props}
    variant={SelectVariant.typeaheadMulti}
    useNameSearch={useNameSearchGQL}
  />
);

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
foreman_remote_execution-5.0.8 webpack/JobWizard/steps/HostsAndInputs/SelectGQL.js
foreman_remote_execution-5.0.7 webpack/JobWizard/steps/HostsAndInputs/SelectGQL.js
foreman_remote_execution-5.0.6 webpack/JobWizard/steps/HostsAndInputs/SelectGQL.js
foreman_remote_execution-6.2.0 webpack/JobWizard/steps/HostsAndInputs/SelectGQL.js
foreman_remote_execution-5.0.5 webpack/JobWizard/steps/HostsAndInputs/SelectGQL.js
foreman_remote_execution-6.1.0 webpack/JobWizard/steps/HostsAndInputs/SelectGQL.js
foreman_remote_execution-5.0.4 webpack/JobWizard/steps/HostsAndInputs/SelectGQL.js
foreman_remote_execution-5.0.3 webpack/JobWizard/steps/HostsAndInputs/SelectGQL.js
foreman_remote_execution-6.0.0 webpack/JobWizard/steps/HostsAndInputs/SelectGQL.js
foreman_remote_execution-5.0.2 webpack/JobWizard/steps/HostsAndInputs/SelectGQL.js
foreman_remote_execution-5.1.0 webpack/JobWizard/steps/HostsAndInputs/SelectGQL.js
foreman_remote_execution-5.0.1 webpack/JobWizard/steps/HostsAndInputs/SelectGQL.js
foreman_remote_execution-5.0.0 webpack/JobWizard/steps/HostsAndInputs/SelectGQL.js