-
Notifications
You must be signed in to change notification settings - Fork 177
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
Node resource table and modals #3565
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import React from 'react'; | ||
import { FormSection, Flex, FlexItem, Button } from '@patternfly/react-core'; | ||
import { Identifier } from '~/types'; | ||
import NodeResourceTable from './nodeResource/NodeResourceTable'; | ||
import ManageNodeResourceModal from './nodeResource/ManageNodeResourceModal'; | ||
|
||
type ManageNodeResourceSectionProps = { | ||
nodeResources: Identifier[]; | ||
setNodeResources: (identifiers: Identifier[]) => void; | ||
}; | ||
|
||
export const ManageNodeResourceSection: React.FC<ManageNodeResourceSectionProps> = ({ | ||
nodeResources, | ||
setNodeResources, | ||
}) => { | ||
const [isNodeResourceModalOpen, setIsNodeResourceModalOpen] = React.useState<boolean>(false); | ||
return ( | ||
<> | ||
<FormSection | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the mockup, there is a description under this section title. Also, the button is a link style below the table but not next to the section title. However, the description is missed here in this PR and the button becomes the secondary button next to the title. I am OK with changing the edit and delete icon to the kebab menu. However, I am not sure about the description and also the add button because it can affect how we show the empty state. Can you check and help confirm that @vconzola? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please implement as shown in the mockup including the description, the action icons in place of a kebab and the link button for Add resource. Thanks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is not the latest mocks. did you verify with the latest There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where is the latest mock? I checked this one https://www.figma.com/design/UFhLtsPxoETwd39pnMsASV/Hardware-profiles?node-id=0-1&p=f&t=52XPdoeFVdZ2Bkl6-0. |
||
title={ | ||
<Flex> | ||
<FlexItem>Node resources</FlexItem> | ||
<FlexItem> | ||
<Button | ||
variant="secondary" | ||
onClick={() => setIsNodeResourceModalOpen(true)} | ||
data-testid="add-node-resource-button" | ||
> | ||
Add resource | ||
</Button> | ||
</FlexItem> | ||
</Flex> | ||
} | ||
> | ||
<NodeResourceTable | ||
nodeResources={nodeResources} | ||
onUpdate={(newIdentifiers) => setNodeResources(newIdentifiers)} | ||
/> | ||
</FormSection> | ||
{isNodeResourceModalOpen ? ( | ||
<ManageNodeResourceModal | ||
onClose={() => setIsNodeResourceModalOpen(false)} | ||
onSave={(identifier) => setNodeResources([...nodeResources, identifier])} | ||
nodeResources={nodeResources} | ||
/> | ||
) : null} | ||
</> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import * as React from 'react'; | ||
import { FormGroup, FormHelperText, HelperText, HelperTextItem } from '@patternfly/react-core'; | ||
import MemoryField from '~/components/MemoryField'; | ||
import CPUField from '~/components/CPUField'; | ||
import NumberInputWrapper from '~/components/NumberInputWrapper'; | ||
|
||
type CountFormFieldProps = { | ||
label: string; | ||
fieldId: string; | ||
size: number | string; | ||
setSize: (value: number | string) => void; | ||
identifier: string; | ||
errorMessage?: string; | ||
isValid?: boolean; | ||
}; | ||
|
||
const CountFormField: React.FC<CountFormFieldProps> = ({ | ||
label, | ||
fieldId, | ||
size, | ||
setSize, | ||
identifier, | ||
errorMessage, | ||
isValid = true, | ||
}) => { | ||
const renderInputField = () => { | ||
switch (identifier) { | ||
case 'cpu': | ||
return <CPUField onChange={(value) => setSize(value)} value={size} />; | ||
case 'memory': | ||
return <MemoryField onChange={(value) => setSize(value)} value={String(size)} />; | ||
default: | ||
return ( | ||
<NumberInputWrapper | ||
min={0} | ||
value={Number(size)} | ||
onChange={(value) => { | ||
if (value) { | ||
setSize(value); | ||
} | ||
}} | ||
/> | ||
); | ||
} | ||
}; | ||
|
||
return ( | ||
<FormGroup label={label} fieldId={fieldId} data-testid={fieldId}> | ||
{renderInputField()} | ||
{!isValid && errorMessage && ( | ||
<FormHelperText> | ||
<HelperText> | ||
<HelperTextItem data-testid={`${fieldId}-error`} variant="error"> | ||
{errorMessage} | ||
</HelperTextItem> | ||
</HelperText> | ||
</FormHelperText> | ||
)} | ||
</FormGroup> | ||
); | ||
}; | ||
|
||
export default CountFormField; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import React from 'react'; | ||
import { Modal } from '@patternfly/react-core/deprecated'; | ||
import DashboardModalFooter from '~/concepts/dashboard/DashboardModalFooter'; | ||
import { Identifier } from '~/types'; | ||
import useGenericObjectState from '~/utilities/useGenericObjectState'; | ||
import { CPU_UNITS, MEMORY_UNITS_FOR_SELECTION, UnitOption } from '~/utilities/valueUnits'; | ||
import { EMPTY_IDENTIFIER } from './const'; | ||
import NodeResourceForm from './NodeResourceForm'; | ||
import { validateDefaultCount, validateMinCount } from './utils'; | ||
|
||
type ManageNodeResourceModalProps = { | ||
onClose: () => void; | ||
existingIdentifier?: Identifier; | ||
onSave: (identifier: Identifier) => void; | ||
nodeResources: Identifier[]; | ||
}; | ||
|
||
const ManageNodeResourceModal: React.FC<ManageNodeResourceModalProps> = ({ | ||
onClose, | ||
existingIdentifier, | ||
onSave, | ||
nodeResources, | ||
}) => { | ||
const [identifier, setIdentifier] = useGenericObjectState<Identifier>( | ||
existingIdentifier || EMPTY_IDENTIFIER, | ||
); | ||
|
||
const [unitOptions, setUnitOptions] = React.useState<UnitOption[]>(); | ||
|
||
const isUniqueIdentifier = React.useMemo(() => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need to use |
||
if (existingIdentifier) { | ||
return true; | ||
} | ||
Comment on lines
+31
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are you not checking the identifier when editing? If I am editing a custom identifier, then it could be duplicated to another one, right? Try to create 2 identifiers, then edit one to change the identifier to the other and see if it throws the error. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In another word, this check only needs to be
|
||
return !nodeResources.some((i) => i.identifier === identifier.identifier); | ||
}, [existingIdentifier, identifier.identifier, nodeResources]); | ||
|
||
React.useEffect(() => { | ||
switch (identifier.identifier) { | ||
case 'cpu': | ||
setUnitOptions(CPU_UNITS); | ||
break; | ||
case 'memory': | ||
setUnitOptions(MEMORY_UNITS_FOR_SELECTION); | ||
break; | ||
default: | ||
setUnitOptions(undefined); | ||
} | ||
}, [identifier]); | ||
|
||
const isButtonDisabled = React.useMemo(() => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, no need to use |
||
const isValidCounts = unitOptions | ||
? validateDefaultCount(identifier, unitOptions) && validateMinCount(identifier, unitOptions) | ||
: true; | ||
|
||
return ( | ||
!identifier.displayName || !identifier.identifier || !isUniqueIdentifier || !isValidCounts | ||
); | ||
}, [identifier, unitOptions, isUniqueIdentifier]); | ||
|
||
const handleSubmit = () => { | ||
onSave(identifier); | ||
onClose(); | ||
}; | ||
|
||
return ( | ||
<Modal | ||
title={existingIdentifier ? 'Edit resource' : 'Add resource'} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be |
||
variant="medium" | ||
isOpen | ||
onClose={onClose} | ||
footer={ | ||
<DashboardModalFooter | ||
submitLabel={existingIdentifier ? 'Update' : 'Add'} | ||
onSubmit={handleSubmit} | ||
onCancel={onClose} | ||
isSubmitDisabled={isButtonDisabled} | ||
/> | ||
} | ||
> | ||
<NodeResourceForm | ||
identifier={identifier} | ||
setIdentifier={setIdentifier} | ||
unitOptions={unitOptions} | ||
existingIdentifier={!!existingIdentifier} | ||
isUniqueIdentifier={isUniqueIdentifier} | ||
/> | ||
</Modal> | ||
); | ||
}; | ||
|
||
export default ManageNodeResourceModal; |
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,81 @@ | ||||||||||||||
import React from 'react'; | ||||||||||||||
import { TextInput, FormGroup, Form } from '@patternfly/react-core'; | ||||||||||||||
import { Identifier } from '~/types'; | ||||||||||||||
import { UpdateObjectAtPropAndValue } from '~/pages/projects/types'; | ||||||||||||||
import { UnitOption } from '~/utilities/valueUnits'; | ||||||||||||||
import { validateDefaultCount, validateMinCount } from './utils'; | ||||||||||||||
import CountFormField from './CountFormField'; | ||||||||||||||
|
||||||||||||||
type NodeResourceFormProps = { | ||||||||||||||
identifier: Identifier; | ||||||||||||||
setIdentifier: UpdateObjectAtPropAndValue<Identifier>; | ||||||||||||||
unitOptions?: UnitOption[]; | ||||||||||||||
existingIdentifier?: boolean; | ||||||||||||||
isUniqueIdentifier?: boolean; | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
const NodeResourceForm: React.FC<NodeResourceFormProps> = ({ | ||||||||||||||
identifier, | ||||||||||||||
setIdentifier, | ||||||||||||||
unitOptions, | ||||||||||||||
existingIdentifier, | ||||||||||||||
isUniqueIdentifier, | ||||||||||||||
}) => { | ||||||||||||||
const [validated, setValidated] = React.useState<'default' | 'error' | 'success'>('default'); | ||||||||||||||
|
||||||||||||||
React.useEffect(() => { | ||||||||||||||
setValidated(isUniqueIdentifier ? 'default' : 'error'); | ||||||||||||||
}, [isUniqueIdentifier]); | ||||||||||||||
Comment on lines
+24
to
+28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need these at all:
Suggested change
|
||||||||||||||
|
||||||||||||||
return ( | ||||||||||||||
<Form> | ||||||||||||||
<FormGroup label="Resource label" fieldId="resource-label"> | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should add |
||||||||||||||
<TextInput | ||||||||||||||
value={identifier.displayName || ''} | ||||||||||||||
onChange={(_, value) => setIdentifier('displayName', value)} | ||||||||||||||
/> | ||||||||||||||
</FormGroup> | ||||||||||||||
|
||||||||||||||
<FormGroup label="Resource identifier" fieldId="resource-identifier"> | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should add |
||||||||||||||
<TextInput | ||||||||||||||
value={identifier.identifier || ''} | ||||||||||||||
onChange={(_, value) => setIdentifier('identifier', value)} | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This field should also be disabled if it's |
||||||||||||||
isDisabled={ | ||||||||||||||
existingIdentifier && | ||||||||||||||
(identifier.identifier === 'cpu' || identifier.identifier === 'memory') | ||||||||||||||
Comment on lines
+44
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be just:
Suggested change
|
||||||||||||||
} | ||||||||||||||
validated={validated} | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missed error message here? |
||||||||||||||
/> | ||||||||||||||
</FormGroup> | ||||||||||||||
|
||||||||||||||
<CountFormField | ||||||||||||||
label="Default" | ||||||||||||||
fieldId="default" | ||||||||||||||
identifier={identifier.identifier} | ||||||||||||||
size={identifier.defaultCount} | ||||||||||||||
setSize={(value) => setIdentifier('defaultCount', value)} | ||||||||||||||
isValid={unitOptions ? validateDefaultCount(identifier, unitOptions) : true} | ||||||||||||||
errorMessage="Default must be equal to or between the minimum and maximum allowed limits." | ||||||||||||||
/> | ||||||||||||||
|
||||||||||||||
<CountFormField | ||||||||||||||
label="Minimum allowed" | ||||||||||||||
fieldId="minimum-allowed" | ||||||||||||||
identifier={identifier.identifier} | ||||||||||||||
size={identifier.minCount} | ||||||||||||||
setSize={(value) => setIdentifier('minCount', value)} | ||||||||||||||
isValid={unitOptions ? validateMinCount(identifier, unitOptions) : true} | ||||||||||||||
errorMessage="Minimum allowed value cannot exceed the maximum allowed value." | ||||||||||||||
/> | ||||||||||||||
|
||||||||||||||
<CountFormField | ||||||||||||||
label="Default" | ||||||||||||||
fieldId="default" | ||||||||||||||
Comment on lines
+72
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are maximum allowed. |
||||||||||||||
identifier={identifier.identifier} | ||||||||||||||
size={identifier.maxCount} | ||||||||||||||
setSize={(value) => setIdentifier('maxCount', value)} | ||||||||||||||
/> | ||||||||||||||
</Form> | ||||||||||||||
); | ||||||||||||||
}; | ||||||||||||||
export default NodeResourceForm; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a single file, maybe consider exporting it as
default
like other components?