From 73b615cec91a2830b9e26694e1133f8c6c90d180 Mon Sep 17 00:00:00 2001 From: Phillip Rhodes Date: Mon, 16 Sep 2024 09:01:39 -0400 Subject: [PATCH] Change template details CPU/memory modal to new design --- .../CPUMemoryModal/CPUMemoryModal.tsx | 32 ++--- .../components/CPUInput/CPUInput.tsx | 20 +-- .../CPUHelperText/CPUHelperText.tsx | 10 +- .../tabs/details/components/CPUMemory.tsx | 2 +- .../details/components/CPUMemoryModal.tsx | 125 ---------------- .../CPUMemoryModal/CPUMemoryModal.tsx | 133 ++++++++++++++++++ .../components/CPUMemoryModal/utils.ts | 19 +++ 7 files changed, 188 insertions(+), 153 deletions(-) delete mode 100644 src/views/templates/details/tabs/details/components/CPUMemoryModal.tsx create mode 100644 src/views/templates/details/tabs/details/components/CPUMemoryModal/CPUMemoryModal.tsx create mode 100644 src/views/templates/details/tabs/details/components/CPUMemoryModal/utils.ts diff --git a/src/utils/components/CPUMemoryModal/CPUMemoryModal.tsx b/src/utils/components/CPUMemoryModal/CPUMemoryModal.tsx index 5b7ddc958..61bc872fc 100644 --- a/src/utils/components/CPUMemoryModal/CPUMemoryModal.tsx +++ b/src/utils/components/CPUMemoryModal/CPUMemoryModal.tsx @@ -42,6 +42,15 @@ const CPUMemoryModal: FC = ({ vm, }) => { const { t } = useKubevirtTranslation(); + + const [updateInProcess, setUpdateInProcess] = useState(false); + const [updateError, setUpdateError] = useState(); + + const { size, unit } = getMemorySize(getMemory(vm)); + const [memory, setMemory] = useState(size || undefined); + const [memoryUnit, setMemoryUnit] = useState(unit || undefined); + const [cpu, setCPU] = useState(getCPU(vm)); + const { data: templateDefaultsData, error: defaultLoadError, @@ -50,13 +59,8 @@ const CPUMemoryModal: FC = ({ vm?.metadata?.labels?.['vm.kubevirt.io/template'], vm?.metadata?.labels?.['vm.kubevirt.io/template.namespace'] || templateNamespace, ); - const [updateInProcess, setUpdateInProcess] = useState(false); - const [updateError, setUpdateError] = useState(); - - const { size, unit } = getMemorySize(getMemory(vm)); - const [memory, setMemory] = useState(size || undefined); - const [memoryUnit, setMemoryUnit] = useState(unit || undefined); - const [cpu, setCPU] = useState(getCPU(vm)); + const { defaultCpu, defaultMemory } = templateDefaultsData || {}; + const { size: defaultMemorySize, unit: defaultMemoryUnit } = defaultMemory || {}; const templateName = getLabel(vm, VM_TEMPLATE_ANNOTATION); @@ -99,16 +103,12 @@ const CPUMemoryModal: FC = ({ , , + , + , + ]} + className="cpu-memory-modal" + isOpen={isOpen} + onClose={onClose} + title={t('Edit CPU | Memory')} + variant={ModalVariant.small} + width="650px" + > +
+ + +
+ {updateError && ( + + {updateError} + + )} + + ); +}; + +export default CPUMemoryModal; diff --git a/src/views/templates/details/tabs/details/components/CPUMemoryModal/utils.ts b/src/views/templates/details/tabs/details/components/CPUMemoryModal/utils.ts new file mode 100644 index 000000000..1779d7b6c --- /dev/null +++ b/src/views/templates/details/tabs/details/components/CPUMemoryModal/utils.ts @@ -0,0 +1,19 @@ +import { V1Template } from '@kubevirt-ui/kubevirt-api/console'; +import { V1CPU } from '@kubevirt-ui/kubevirt-api/kubevirt'; +import { getMemorySize } from '@kubevirt-utils/components/CPUMemoryModal/utils/CpuMemoryUtils'; +import { getTemplateVirtualMachineObject } from '@kubevirt-utils/resources/template'; +import { getCPU, getMemory } from '@kubevirt-utils/resources/vm'; + +type CPUMemoryValues = { + defaultCPU: V1CPU; + defaultMemory: { defaultMemorySize: number; defaultMemoryUnit: string }; +}; + +export const getDefaultCPUMemoryValues = (template: V1Template): CPUMemoryValues => { + const vmObject = getTemplateVirtualMachineObject(template); + const defaultCPU = getCPU(vmObject); + const defaultMemory = getMemorySize(getMemory(vmObject)); + const { size, unit } = defaultMemory; + + return { defaultCPU, defaultMemory: { defaultMemorySize: size, defaultMemoryUnit: unit } }; +};