|
| 1 | +import React, { useCallback, useEffect } from 'react'; |
| 2 | +import { string, func, bool } from 'prop-types'; |
| 3 | +import { withRouter, useLocation } from 'react-router-dom'; |
| 4 | +import { withI18n } from '@lingui/react'; |
| 5 | +import { t } from '@lingui/macro'; |
| 6 | +import { FormGroup, Tooltip } from '@patternfly/react-core'; |
| 7 | + |
| 8 | +import { ExecutionEnvironmentsAPI } from '../../api'; |
| 9 | +import { ExecutionEnvironment } from '../../types'; |
| 10 | +import { getQSConfig, parseQueryString, mergeParams } from '../../util/qs'; |
| 11 | +import Popover from '../Popover'; |
| 12 | +import OptionsList from '../OptionsList'; |
| 13 | +import useRequest from '../../util/useRequest'; |
| 14 | + |
| 15 | +import Lookup from './Lookup'; |
| 16 | +import LookupErrorMessage from './shared/LookupErrorMessage'; |
| 17 | + |
| 18 | +const QS_CONFIG = getQSConfig('execution_environments', { |
| 19 | + page: 1, |
| 20 | + page_size: 5, |
| 21 | + order_by: 'name', |
| 22 | +}); |
| 23 | + |
| 24 | +function ExecutionEnvironmentLookup({ |
| 25 | + globallyAvailable, |
| 26 | + i18n, |
| 27 | + isDefaultEnvironment, |
| 28 | + isDisabled, |
| 29 | + onChange, |
| 30 | + organizationId, |
| 31 | + popoverContent, |
| 32 | + tooltip, |
| 33 | + value, |
| 34 | + onBlur, |
| 35 | +}) { |
| 36 | + const location = useLocation(); |
| 37 | + |
| 38 | + const { |
| 39 | + result: { |
| 40 | + executionEnvironments, |
| 41 | + count, |
| 42 | + relatedSearchableKeys, |
| 43 | + searchableKeys, |
| 44 | + }, |
| 45 | + request: fetchExecutionEnvironments, |
| 46 | + error, |
| 47 | + isLoading, |
| 48 | + } = useRequest( |
| 49 | + useCallback(async () => { |
| 50 | + const params = parseQueryString(QS_CONFIG, location.search); |
| 51 | + const globallyAvailableParams = globallyAvailable |
| 52 | + ? { or__organization__isnull: 'True' } |
| 53 | + : {}; |
| 54 | + const organizationIdParams = organizationId |
| 55 | + ? { or__organization__id: organizationId } |
| 56 | + : {}; |
| 57 | + const [{ data }, actionsResponse] = await Promise.all([ |
| 58 | + ExecutionEnvironmentsAPI.read( |
| 59 | + mergeParams(params, { |
| 60 | + ...globallyAvailableParams, |
| 61 | + ...organizationIdParams, |
| 62 | + }) |
| 63 | + ), |
| 64 | + ExecutionEnvironmentsAPI.readOptions(), |
| 65 | + ]); |
| 66 | + return { |
| 67 | + executionEnvironments: data.results, |
| 68 | + count: data.count, |
| 69 | + relatedSearchableKeys: ( |
| 70 | + actionsResponse?.data?.related_search_fields || [] |
| 71 | + ).map(val => val.slice(0, -8)), |
| 72 | + searchableKeys: Object.keys( |
| 73 | + actionsResponse.data.actions?.GET || {} |
| 74 | + ).filter(key => actionsResponse.data.actions?.GET[key].filterable), |
| 75 | + }; |
| 76 | + }, [location, globallyAvailable, organizationId]), |
| 77 | + { |
| 78 | + executionEnvironments: [], |
| 79 | + count: 0, |
| 80 | + relatedSearchableKeys: [], |
| 81 | + searchableKeys: [], |
| 82 | + } |
| 83 | + ); |
| 84 | + |
| 85 | + useEffect(() => { |
| 86 | + fetchExecutionEnvironments(); |
| 87 | + }, [fetchExecutionEnvironments]); |
| 88 | + |
| 89 | + const renderLookup = () => ( |
| 90 | + <> |
| 91 | + <Lookup |
| 92 | + id="execution-environments" |
| 93 | + header={i18n._(t`Execution Environments`)} |
| 94 | + value={value} |
| 95 | + onBlur={onBlur} |
| 96 | + onChange={onChange} |
| 97 | + qsConfig={QS_CONFIG} |
| 98 | + isLoading={isLoading} |
| 99 | + isDisabled={isDisabled} |
| 100 | + renderOptionsList={({ state, dispatch, canDelete }) => ( |
| 101 | + <OptionsList |
| 102 | + value={state.selectedItems} |
| 103 | + options={executionEnvironments} |
| 104 | + optionCount={count} |
| 105 | + searchColumns={[ |
| 106 | + { |
| 107 | + name: i18n._(t`Name`), |
| 108 | + key: 'name__icontains', |
| 109 | + isDefault: true, |
| 110 | + }, |
| 111 | + ]} |
| 112 | + sortColumns={[ |
| 113 | + { |
| 114 | + name: i18n._(t`Name`), |
| 115 | + key: 'name', |
| 116 | + }, |
| 117 | + ]} |
| 118 | + searchableKeys={searchableKeys} |
| 119 | + relatedSearchableKeys={relatedSearchableKeys} |
| 120 | + multiple={state.multiple} |
| 121 | + header={i18n._(t`Execution Environment`)} |
| 122 | + name="executionEnvironments" |
| 123 | + qsConfig={QS_CONFIG} |
| 124 | + readOnly={!canDelete} |
| 125 | + selectItem={item => dispatch({ type: 'SELECT_ITEM', item })} |
| 126 | + deselectItem={item => dispatch({ type: 'DESELECT_ITEM', item })} |
| 127 | + /> |
| 128 | + )} |
| 129 | + /> |
| 130 | + </> |
| 131 | + ); |
| 132 | + |
| 133 | + return ( |
| 134 | + <FormGroup |
| 135 | + fieldId="execution-environment-lookup" |
| 136 | + label={ |
| 137 | + isDefaultEnvironment |
| 138 | + ? i18n._(t`Default Execution Environment`) |
| 139 | + : i18n._(t`Execution Environment`) |
| 140 | + } |
| 141 | + labelIcon={popoverContent && <Popover content={popoverContent} />} |
| 142 | + > |
| 143 | + {isDisabled ? ( |
| 144 | + <Tooltip content={tooltip}>{renderLookup()}</Tooltip> |
| 145 | + ) : ( |
| 146 | + renderLookup() |
| 147 | + )} |
| 148 | + |
| 149 | + <LookupErrorMessage error={error} /> |
| 150 | + </FormGroup> |
| 151 | + ); |
| 152 | +} |
| 153 | + |
| 154 | +ExecutionEnvironmentLookup.propTypes = { |
| 155 | + value: ExecutionEnvironment, |
| 156 | + popoverContent: string, |
| 157 | + onChange: func.isRequired, |
| 158 | + isDefaultEnvironment: bool, |
| 159 | +}; |
| 160 | + |
| 161 | +ExecutionEnvironmentLookup.defaultProps = { |
| 162 | + popoverContent: '', |
| 163 | + isDefaultEnvironment: false, |
| 164 | + value: null, |
| 165 | +}; |
| 166 | + |
| 167 | +export default withI18n()(withRouter(ExecutionEnvironmentLookup)); |
0 commit comments