Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CNV-54239: selected VMs is not getting updates #2334

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/views/virtualmachines/list/VirtualMachinesList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import { OBJECTS_FETCHING_LIMIT } from '@virtualmachines/utils';

import { useVMListFilters } from '../utils';

import VirtualMachineActionButton from './components/VirtualMachineActionButton';
import VirtualMachineBulkActionButton from './components/VirtualMachineBulkActionButton';
import VirtualMachineEmptyState from './components/VirtualMachineEmptyState/VirtualMachineEmptyState';
import VirtualMachineListSummary from './components/VirtualMachineListSummary/VirtualMachineListSummary';
import VirtualMachineRow from './components/VirtualMachineRow/VirtualMachineRow';
Expand Down Expand Up @@ -186,7 +186,7 @@ const VirtualMachinesList: FC<VirtualMachinesListProps> = ({ kind, namespace })
<ListPageHeader title={t('VirtualMachines')}>
<Flex>
<FlexItem>
<VirtualMachineActionButton />
<VirtualMachineBulkActionButton vms={data} />
</FlexItem>
<FlexItem>
<VirtualMachinesCreateButton namespace={namespace} />
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React, { FC, useMemo } from 'react';

import { V1VirtualMachine } from '@kubevirt-ui/kubevirt-api/kubevirt';
import ActionsDropdown from '@kubevirt-utils/components/ActionsDropdown/ActionsDropdown';
import { useKubevirtTranslation } from '@kubevirt-utils/hooks/useKubevirtTranslation';
import { convertResourceArrayToMap } from '@kubevirt-utils/resources/shared';
import { isEmpty } from '@kubevirt-utils/utils/utils';
import useMultipleVirtualMachineActions from '@virtualmachines/actions/hooks/useMultipleVirtualMachineActions';

import { selectedVMs } from '../selectedVMs';

type VirtualMachineBulkActionButtonProps = {
vms: V1VirtualMachine[];
};
const VirtualMachineBulkActionButton: FC<VirtualMachineBulkActionButtonProps> = ({ vms }) => {
const { t } = useKubevirtTranslation();

const selectedVirtualMachines = useMemo(() => {
const vmsMapper = convertResourceArrayToMap(vms, true);
return selectedVMs.value.map(
(selectedVM) => vmsMapper?.[selectedVM.namespace]?.[selectedVM.name],
);
}, [vms]);

const actions = useMultipleVirtualMachineActions(selectedVirtualMachines);

return (
<ActionsDropdown
actions={actions}
disabledTooltip={t('Select multiple VirtualMachines to perform an action for all of them')}
isDisabled={isEmpty(selectedVMs.value)}
variant="secondary"
/>
);
};

export default VirtualMachineBulkActionButton;
25 changes: 17 additions & 8 deletions src/views/virtualmachines/list/selectedVMs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,38 @@ import { getName, getNamespace } from '@kubevirt-utils/resources/shared';
import { isEmpty } from '@kubevirt-utils/utils/utils';
import { signal } from '@preact/signals-core';

export const selectedVMs = signal<V1VirtualMachine[]>([]);
export const selectedVMs = signal<{ name: string; namespace: string }[]>([]);

export const selectVM = (vm: V1VirtualMachine) => {
if (isEmpty(findVM(vm))) selectedVMs.value = [...selectedVMs.value, vm];
const vmIdentifier = { name: getName(vm), namespace: getNamespace(vm) };
if (isEmpty(findVM(vm))) {
selectedVMs.value = [...selectedVMs.value, vmIdentifier];
}
};

export const deselectVM = (vm: V1VirtualMachine) => {
const vmIdentifier = { name: getName(vm), namespace: getNamespace(vm) };
selectedVMs.value = selectedVMs.value.filter(
(selectedVM) =>
getName(selectedVM) !== getName(vm) || getNamespace(selectedVM) !== getNamespace(vm),
selectedVM.name !== vmIdentifier.name || selectedVM.namespace !== vmIdentifier.namespace,
);
};

export const selectAll = (vms: V1VirtualMachine[]) => {
selectedVMs.value = Array.from(new Set([...selectedVMs.value, ...vms]));
const vmIdentifiers = vms.map((vm) => ({ name: getName(vm), namespace: getNamespace(vm) }));
selectedVMs.value = [...vmIdentifiers];
};

export const deselectAll = () => (selectedVMs.value = []);
export const deselectAll = () => {
selectedVMs.value = [];
};

export const findVM = (vm: V1VirtualMachine) =>
selectedVMs.value.find(
export const findVM = (vm: V1VirtualMachine) => {
const vmIdentifier = { name: getName(vm), namespace: getNamespace(vm) };
return selectedVMs.value.find(
(selectedVM) =>
getName(selectedVM) === getName(vm) && getNamespace(selectedVM) === getNamespace(vm),
selectedVM.name === vmIdentifier.name && selectedVM.namespace === vmIdentifier.namespace,
);
};

export const isVMSelected = (vm: V1VirtualMachine): boolean => !isEmpty(findVM(vm));
Loading