-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [WD-18264] CMS fields for storage pool source
Signed-off-by: Nkeiruka <nkeiruka.whenu@canonical.com>
- Loading branch information
Showing
11 changed files
with
324 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import { FC, Fragment, useEffect, useRef, useState } from "react"; | ||
import { CheckboxInput, Input } from "@canonical/react-components"; | ||
import ResourceLink from "components/ResourceLink"; | ||
import FormEditButton from "components/FormEditButton"; | ||
import { ClusterSpecificValues } from "components/ClusterSpecificSelect"; | ||
|
||
interface Props { | ||
id: string; | ||
isReadOnly: boolean; | ||
onChange: (value: ClusterSpecificValues) => void; | ||
toggleReadOnly: () => void; | ||
memberNames: string[]; | ||
values?: ClusterSpecificValues; | ||
canToggleSpecific?: boolean; | ||
isDefaultSpecific?: boolean; | ||
clusterMemberLinkTarget?: (member: string) => string; | ||
disabledGlobalInput?: boolean; | ||
disabledClusteredInput?: boolean; | ||
} | ||
|
||
const ClusterSpecificInput: FC<Props> = ({ | ||
values, | ||
id, | ||
isReadOnly, | ||
memberNames, | ||
onChange, | ||
toggleReadOnly, | ||
canToggleSpecific = true, | ||
isDefaultSpecific = false, | ||
clusterMemberLinkTarget = () => "/ui/cluster", | ||
disabledGlobalInput = false, | ||
disabledClusteredInput = false, | ||
}) => { | ||
const [isSpecific, setIsSpecific] = useState(isDefaultSpecific); | ||
const firstValue = Object.values(values ?? {})[0]; | ||
const isUserActionRef = useRef(false); | ||
|
||
useEffect(() => { | ||
if (!isUserActionRef.current && values && Object.keys(values).length > 0) { | ||
const newDefaultSpecific = !Object.values(values).every( | ||
(item) => item === Object.values(values)[0], | ||
); | ||
setIsSpecific(newDefaultSpecific); | ||
} | ||
isUserActionRef.current = false; | ||
}, [values]); | ||
|
||
const setValueForAllMembers = (value: string) => { | ||
const update: ClusterSpecificValues = {}; | ||
memberNames.forEach((member) => (update[member] = value)); | ||
onChange(update); | ||
}; | ||
|
||
const setValueForMember = (value: string, member: string) => { | ||
const update = { | ||
...values, | ||
[member]: value, | ||
}; | ||
onChange(update); | ||
}; | ||
|
||
return ( | ||
<div className="u-sv3"> | ||
{canToggleSpecific && !isReadOnly && ( | ||
<CheckboxInput | ||
id={`${id}-same-for-all-toggle`} | ||
label="Same for all cluster members" | ||
checked={!isSpecific} | ||
onChange={() => { | ||
isUserActionRef.current = true; | ||
if (isSpecific) { | ||
setValueForAllMembers(values?.[memberNames[0]] ?? ""); | ||
} | ||
setIsSpecific((val) => !val); | ||
}} | ||
/> | ||
)} | ||
{isSpecific && ( | ||
<div className="cluster-specific-input"> | ||
{memberNames.map((item) => { | ||
const activeValue = values?.[item]; | ||
|
||
return ( | ||
<Fragment key={item}> | ||
<div className="cluster-specific-member"> | ||
<ResourceLink | ||
type="cluster-member" | ||
value={item} | ||
to={clusterMemberLinkTarget(item)} | ||
/> | ||
</div> | ||
<div className="cluster-specific-value"> | ||
{isReadOnly ? ( | ||
<> | ||
{activeValue} | ||
<FormEditButton toggleReadOnly={toggleReadOnly} /> | ||
</> | ||
) : ( | ||
<Input | ||
id={`${id}-${item}`} | ||
type="text" | ||
className="u-no-margin--bottom" | ||
value={activeValue} | ||
onChange={(e) => setValueForMember(e.target.value, item)} | ||
disabled={disabledClusteredInput} | ||
/> | ||
)} | ||
</div> | ||
</Fragment> | ||
); | ||
})} | ||
</div> | ||
)} | ||
{!isSpecific && ( | ||
<div> | ||
{isReadOnly ? ( | ||
<> | ||
{firstValue} | ||
<FormEditButton toggleReadOnly={toggleReadOnly} /> | ||
</> | ||
) : ( | ||
<Input | ||
id={id} | ||
type="text" | ||
className="u-no-margin--bottom" | ||
value={firstValue} | ||
onChange={(e) => setValueForAllMembers(e.target.value)} | ||
disabled={disabledGlobalInput} | ||
/> | ||
)} | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default ClusterSpecificInput; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/pages/storage/forms/StoragePoolClusteredSourceSelector.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Label } from "@canonical/react-components"; | ||
import ClusterSpecificInput from "components/forms/ClusterSpecificInput"; | ||
import { FormikProps } from "formik"; | ||
import { FC } from "react"; | ||
import { ensureEditMode } from "util/instanceEdit"; | ||
import { StoragePoolFormValues } from "./StoragePoolForm"; | ||
import { useClusterMembers } from "context/useClusterMembers"; | ||
import { focusField } from "util/formFields"; | ||
|
||
interface Props { | ||
formik: FormikProps<StoragePoolFormValues>; | ||
} | ||
|
||
const StoragePoolClusteredSourceSelector: FC<Props> = ({ formik }) => { | ||
const { data: clusterMembers = [] } = useClusterMembers(); | ||
const memberNames: string[] = []; | ||
clusterMembers.forEach((member) => memberNames.push(member.server_name)); | ||
|
||
return ( | ||
<> | ||
<Label>Source</Label> | ||
<ClusterSpecificInput | ||
values={formik.values.sourcePerClusterMember} | ||
id="sourcePerClusterMember" | ||
isReadOnly={false} | ||
onChange={(value) => { | ||
void formik.setFieldValue("sourcePerClusterMember", value); | ||
}} | ||
toggleReadOnly={() => { | ||
ensureEditMode(formik); | ||
focusField("sourcePerClusterMember"); | ||
}} | ||
memberNames={memberNames} | ||
disabledGlobalInput={!formik.values.isCreating} | ||
disabledClusteredInput={!formik.values.isCreating} | ||
/> | ||
</> | ||
); | ||
}; | ||
|
||
export default StoragePoolClusteredSourceSelector; |
Oops, something went wrong.