Skip to content

Commit

Permalink
chore(ui): review updates
Browse files Browse the repository at this point in the history
Signed-off-by: Mayursinh Sarvaiya <[email protected]>
  • Loading branch information
Marvin9 committed Dec 18, 2024
1 parent c8dad5b commit 67c2047
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 26 deletions.
20 changes: 13 additions & 7 deletions ui/src/features/project/credentials/create-credentials-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const createFormSchema = (editing?: boolean) =>
),
description: z.string().optional(),
type: zodValidators.requiredString,
data: z.record(z.string(), z.string())
data: z.array(z.array(z.string()))
})
)
.refine((data) => ['git', 'helm', 'image', 'generic'].includes(data.type), {
Expand Down Expand Up @@ -105,14 +105,23 @@ export const CreateCredentialsModal = ({ project, onSuccess, editing, init, ...p
}}
okText={editing ? 'Update' : 'Create'}
onOk={handleSubmit((values) => {
const data: Record<string, string> = {};

if (values?.data?.length > 0) {
for (const [k, v] of values.data) {
data[k] = v;
}
}

if (editing) {
return updateCredentialsMutation.mutate({
...values,
project,
name: init?.metadata?.name || ''
name: init?.metadata?.name || '',
data
});
} else {
createCredentialsMutation.mutate({ ...values, project });
createCredentialsMutation.mutate({ ...values, project, data });
}
})}
title={
Expand Down Expand Up @@ -203,10 +212,7 @@ export const CreateCredentialsModal = ({ project, onSuccess, editing, init, ...p
{credentialType === 'generic' && (
<FieldContainer control={control} name='data' label='Secrets'>
{({ field }) => (
<SecretEditor
secret={field.value as Record<string, string>}
onChange={field.onChange}
/>
<SecretEditor secret={field.value as [string, string][]} onChange={field.onChange} />
)}
</FieldContainer>
)}
Expand Down
40 changes: 23 additions & 17 deletions ui/src/features/project/credentials/secret-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import TextArea from 'antd/es/input/TextArea';
import { useEffect, useState } from 'react';

type SecretEditorProps = {
secret: Record<string, string>;
onChange: (newSecret: Record<string, string>) => void;
secret: [string, string][];
onChange: (newSecret: [string, string][]) => void;
};

export const SecretEditor = (props: SecretEditorProps) => {
const secretEntries = Object.entries(props.secret);
const secretEntries = props.secret;

const [lockedSecrets, setLockedSecrets] = useState<string[]>([]);

// if first render of this component has redacted secrets then it is edit mode
// lock the existing secrets
useEffect(() => {
setLockedSecrets(Object.keys(props.secret || {}));
setLockedSecrets(props.secret.map((secret) => secret[0]));
}, []);

return (
Expand All @@ -30,13 +30,15 @@ export const SecretEditor = (props: SecretEditorProps) => {
onChange={(e) => {
const newKey = e.target.value;

const newSecretData: Record<string, string> = { ...(props.secret || {}) };

delete newSecretData[key];

newSecretData[newKey] = value as string;
props.onChange(
secretEntries.map((entry, origIdx) => {
if (idx === origIdx) {
return [newKey, value];
}

props.onChange(newSecretData);
return entry;
})
);
}}
placeholder='key'
/>
Expand Down Expand Up @@ -67,7 +69,15 @@ export const SecretEditor = (props: SecretEditorProps) => {
onChange={(e) => {
const newValue = e.target.value;

props.onChange({ ...(props.secret || {}), [key]: newValue });
props.onChange(
secretEntries.map((entry) => {
if (key === entry[0]) {
return [key, newValue];
}

return entry;
})
);
}}
/>
)}
Expand All @@ -77,16 +87,12 @@ export const SecretEditor = (props: SecretEditorProps) => {
type='text'
danger
onClick={() => {
const newSecretData: Record<string, string> = { ...props.secret };

delete newSecretData[key];

props.onChange(newSecretData);
props.onChange(secretEntries.filter((_, origIdx) => idx !== origIdx));
}}
/>
</Flex>
))}
<Button onClick={() => props.onChange({ ...props.secret, '': '' })} className='mt-2'>
<Button onClick={() => props.onChange(secretEntries.concat([['', '']]))} className='mt-2'>
Add k8s Secret
</Button>
</>
Expand Down
4 changes: 2 additions & 2 deletions ui/src/features/project/credentials/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const constructDefaults = (init?: Secret, type?: string) => {
repoUrlIsRegex: false,
username: '',
password: '',
data: {}
data: []
};
}

Expand All @@ -67,5 +67,5 @@ export const redactSecretStringData = (secret: Secret) => {
data[key] = '';
}

return data;
return Object.entries(data);
};

0 comments on commit 67c2047

Please sign in to comment.