Skip to content

Commit

Permalink
refactor: props changes + skip queries
Browse files Browse the repository at this point in the history
  • Loading branch information
topliceanurazvan committed Nov 17, 2023
1 parent e4d618c commit 3a886de
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@ const ActionFormItems = () => {
const [actionValue, setActionValue] = useState<string>('');
const [switcherValue, setSwitcherValue] = useState('label');

const nameSelector = Form.useFormInstance().getFieldValue('testNameSelector');
const form = Form.useFormInstance();

const nameSelector = form.getFieldValue('testNameSelector');

useEffect(() => {
setSwitcherValue(nameSelector ? 'name' : 'label');
}, [nameSelector]);

useEffect(() => {
form.resetFields(['testNameSelector']);
}, [actionValue]);

const actionOptions = keyMap?.actions
.map((actionItem: string) =>
keyMap!.executions.map(executionItem => {
Expand Down Expand Up @@ -72,7 +78,10 @@ const ActionFormItems = () => {
name="testNameSelector"
rules={[required]}
>
<ResourceTriggerSelect actionValue={actionValue} />
<ResourceTriggerSelect
$allowTests={actionValue === 'run test'}
$allowTestSuites={actionValue === 'run testsuite'}
/>
</Form.Item>
)}
</Space>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,47 @@ import {StyledResourceOptionWrapper} from './ResourceTriggerSelect.styled';
const {Option, OptGroup} = Select;

interface ResourceTriggerSelectProps {
actionValue: string;
$allowTests: boolean;
$allowTestSuites: boolean;
disabled?: boolean;
value?: string;
onChange?: (value: string) => void;
}

const ResourceTriggerSelect: FC<ResourceTriggerSelectProps> = ({...props}) => {
const ResourceTriggerSelect: FC<ResourceTriggerSelectProps> = props => {
const {$allowTestSuites, $allowTests, disabled, value, onChange, ...rest} = props;

const isClusterAvailable = useSystemAccess(SystemAccess.agent);

const {executors = []} = useExecutorsPick('executors');

const {data: tests = []} = useGetAllTestsQuery(null, {skip: !isClusterAvailable});
const {data: tests = []} = useGetAllTestsQuery(null, {skip: !isClusterAvailable || !$allowTests});
const {data: testSuites = []} = useGetAllTestSuitesQuery(null, {
skip: !isClusterAvailable,
skip: !isClusterAvailable || !$allowTestSuites,
});

const testsData = useMemo(() => {
if (!$allowTests) return [];

return tests.map(item => ({
name: item.test.name,
namespace: item.test.namespace,
type: item.test.type,
}));
}, [tests]);
}, [tests, $allowTests]);

const testSuitesData = useMemo(() => {
if (!$allowTestSuites) return [];

return testSuites.map(item => ({
name: item.testSuite.name,
namespace: item.testSuite.namespace,
}));
}, [testSuites]);
}, [testSuites, $allowTestSuites]);

return (
<Select optionLabelProp="key" placeholder="Your testkube resource" showSearch {...props}>
{testsData.length > 0 && props.actionValue === 'run test' ? (
{testsData.length > 0 ? (
<OptGroup label="Tests">
{testsData.map(item => (
<Option key={item.name} title={item.name}>
Expand All @@ -69,7 +76,7 @@ const ResourceTriggerSelect: FC<ResourceTriggerSelectProps> = ({...props}) => {
))}
</OptGroup>
) : null}
{testSuitesData.length > 0 && props.actionValue === 'run testsuite' ? (
{testSuitesData.length > 0 ? (
<OptGroup label="Test Suites">
{testSuitesData.map(item => (
<Option key={item.name} title={item.name}>
Expand Down

0 comments on commit 3a886de

Please sign in to comment.