Skip to content

Commit

Permalink
Revert "fix: added correct models"
Browse files Browse the repository at this point in the history
This reverts commit c8b7fbf.

Revert "fix: add existing secret in the components secret form"

This reverts commit 239cee0.
  • Loading branch information
JoaoPedroPP committed Oct 17, 2024
1 parent c8b7fbf commit dbbdf42
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 39 deletions.
8 changes: 7 additions & 1 deletion src/components/ImportForm/SecretSection/SecretSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { useWorkspaceInfo } from '../../../utils/workspace-context-utils';
import { ButtonWithAccessTooltip } from '../../ButtonWithAccessTooltip';
import { useModalLauncher } from '../../modal/ModalProvider';
import { SecretModalLauncher } from '../../Secrets/SecretModalLauncher';
import { getSupportedPartnerTaskSecrets } from '../../Secrets/utils/secret-utils';
import { ImportFormValues } from '../type';

const accessReviewResources: AccessReviewResources = [{ model: SecretModel, verb: 'create' }];
Expand All @@ -23,8 +24,13 @@ const SecretSection = () => {

const [secrets, secretsLoaded] = useSecrets(namespace);

const partnerTaskNames = getSupportedPartnerTaskSecrets().map(({ label }) => label);
const partnerTaskSecrets: string[] =
secrets && secretsLoaded ? secrets?.map((s) => s.metadata.name) || [] : [];
secrets && secretsLoaded
? secrets
?.filter((rs) => partnerTaskNames.includes(rs.metadata.name))
?.map((s) => s.metadata.name) || []
: [];

const onSubmit = React.useCallback(
(secretValue: any) => {
Expand Down
25 changes: 10 additions & 15 deletions src/components/Secrets/SecretForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { SelectVariant } from '@patternfly/react-core/deprecated';
import { useFormikContext } from 'formik';
import { DropdownItemObject, SelectInputField } from '../../shared';
import KeyValueFileInputField from '../../shared/components/formik-fields/key-value-file-input-field/KeyValueFileInputField';
import { SecretFormValues, SecretTypeDropdownLabel, KeyValueEntry } from '../../types';
import { SecretFormValues, SecretTypeDropdownLabel } from '../../types';
import { RawComponentProps } from '../modal/createModalLauncher';
import SecretTypeSelector from './SecretTypeSelector';
import {
Expand All @@ -15,26 +15,22 @@ import {

type SecretFormProps = RawComponentProps & {
existingSecrets: string[];
keyValues: KeyValueEntry;
};

const SecretForm: React.FC<React.PropsWithChildren<SecretFormProps>> = ({
existingSecrets,
keyValues,
}) => {
const SecretForm: React.FC<React.PropsWithChildren<SecretFormProps>> = ({ existingSecrets }) => {
const { values, setFieldValue } = useFormikContext<SecretFormValues>();
const defaultKeyValues = [{ key: '', value: '', readOnlyKey: false }];
const defaultImageKeyValues = [{ key: '.dockerconfigjson', value: '', readOnlyKey: true }];

const partnerTaskSecrets = getSupportedPartnerTaskSecrets().map((secret) => secret.value);
const initialOptions = existingSecrets
.filter((secret) => !partnerTaskSecrets.includes(secret))
.map((secret) => ({ value: secret }));
const initialOptions = getSupportedPartnerTaskSecrets().filter(
(secret) => !existingSecrets.includes(secret.value),
);
const [options, setOptions] = React.useState(initialOptions);
const currentTypeRef = React.useRef(values.type);

const clearKeyValues = () => {
setFieldValue('keyValues', defaultKeyValues);
const newKeyValues = values.keyValues.filter((kv) => !kv.readOnlyKey);
setFieldValue('keyValues', [...(newKeyValues.length ? newKeyValues : defaultKeyValues)]);
};

const resetKeyValues = () => {
Expand Down Expand Up @@ -84,17 +80,16 @@ const SecretForm: React.FC<React.PropsWithChildren<SecretFormProps>> = ({
toggleId="secret-name-toggle"
toggleAriaLabel="secret-name-dropdown"
onClear={() => {
clearKeyValues();
if (currentTypeRef.current !== values.type || isPartnerTask(values.secretName)) {
clearKeyValues();
}
}}
onSelect={(e, value) => {
if (isPartnerTask(value)) {
setFieldValue('keyValues', [
...values.keyValues.filter((kv) => !kv.readOnlyKey && (!!kv.key || !!kv.value)),
...getSupportedPartnerTaskKeyValuePairs(value),
]);
} else {
const secretValueIndex = values.existingSecrets.findIndex((secret) => secret === value);
setFieldValue('keyValues', keyValues[secretValueIndex]);
}
setFieldValue('secretName', value);
}}
Expand Down
25 changes: 3 additions & 22 deletions src/components/Secrets/SecretModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ import {
ModalVariant,
} from '@patternfly/react-core';
import { Formik } from 'formik';
import { useSecrets } from '../../hooks/useSecrets';
import { ImportSecret, SecretTypeDropdownLabel } from '../../types';
import { SecretFromSchema } from '../../utils/validation-utils';
import { useWorkspaceInfo } from '../../utils/workspace-context-utils';
import { RawComponentProps } from '../modal/createModalLauncher';
import SecretForm from './SecretForm';

Expand All @@ -38,31 +36,14 @@ type SecretModalProps = RawComponentProps & {
const SecretModal: React.FC<React.PropsWithChildren<SecretModalProps>> = ({
modalProps,
onSubmit,
existingSecrets,
}) => {
const { namespace } = useWorkspaceInfo();
const [secrets, secretsLoaded] = useSecrets(namespace);
const defaultKeyValues = [{ key: '', value: '', readOnlyKey: false }];
const keyValues =
secrets && secretsLoaded
? secrets
?.filter((secret) => secret.type === 'Opaque')
?.map((secret) => {
return Object.keys(secret.data).map((key) => ({
key,
value: atob(secret.data[key]),
readOnlyKey: true,
}));
})
: defaultKeyValues;
const secretNames =
secrets && secretsLoaded
? secrets?.filter((secret) => secret.type === 'Opaque')?.map((secret) => secret.metadata.name)
: defaultKeyValues;
const initialValues: SecretModalValues = {
secretName: '',
type: SecretTypeDropdownLabel.opaque,
keyValues: defaultKeyValues,
existingSecrets: secretNames,
existingSecrets,
};

return (
Expand Down Expand Up @@ -102,7 +83,7 @@ const SecretModal: React.FC<React.PropsWithChildren<SecretModalProps>> = ({
/>
</ModalBoxHeader>
<ModalBoxBody>
<SecretForm existingSecrets={initialValues.existingSecrets} keyValues={keyValues} />
<SecretForm existingSecrets={existingSecrets} />
</ModalBoxBody>
</Modal>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ const KeyValueFileInputField: React.FC<
label="Value"
name={`${name}.${idx.toString()}.value`}
filenamePlaceholder="Drag a file here or upload one"
isDisabled={v.readOnlyKey}
onDataChange={(ev, data: string) => {
setFieldValue(`${name}.${idx.toString()}.value`, data);
onChange && onChange(data, `${name}.${idx.toString()}.value`);
Expand Down

0 comments on commit dbbdf42

Please sign in to comment.