Skip to content

Commit

Permalink
fix: correct function env settings
Browse files Browse the repository at this point in the history
  • Loading branch information
anbraten committed Nov 8, 2023
1 parent aee172d commit 5f257d8
Showing 1 changed file with 37 additions and 29 deletions.
66 changes: 37 additions & 29 deletions packages/dashboard/lib/pages/function/FunctionSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -390,22 +390,30 @@ const FunctionSettings = ({ func, refetch }: FunctionSettingsProps) => {
initialValues={{
env: func?.env || {},
}}
onSubmit={async ({ env }) => {
onSubmit={async ({ env, envKey, envValue }) => {
if (!func) {
return;
}

const newEnv = Object.entries<string>(env).reduce(
(acc, [key, value]) => [...acc, { key, value }],
[] as { key: string; value: string }[],
);

if (envKey && envValue) {
newEnv.push({ key: envKey, value: envValue });
}

await updateFunction.mutateAsync({
functionId: func.id,
env: Object.entries<string>(env).reduce(
(acc, [key, value]) => [...acc, { key, value }],
[] as { key: string; value: string }[],
),
env: newEnv,
});
}}
onSubmitSuccess={async () => {
onSubmitSuccess={async (_, form) => {
toast.success('Function environment variables updated successfully.');
await refetch();
form.change('envKey', '');
form.change('envValue', '');
}}
>
{({ values, form }) => (
Expand All @@ -421,6 +429,29 @@ const FunctionSettings = ({ func, refetch }: FunctionSettingsProps) => {
}
>
<div className="flex flex-col items-start gap-4">
{Object.entries<string>(values.env).map(([key, value], index) => {
return (
<div
key={`${key}-${value}-${index}`}
className="flex flex-col items-start gap-2 md:flex-row md:items-center"
>
<Input name={`${key}-key`} placeholder={key} disabled />
<Input name={`${key}-value`} placeholder={new Array(value.length).fill('*').join('')} disabled />
<Button
disabled={updateFunction.isLoading}
variant="danger"
onClick={() => {
const newEnv = { ...values.env };
delete newEnv[key];

form.change('env', newEnv);
}}
>
{t('env.remove')}
</Button>
</div>
);
})}
<div className="flex flex-col items-start gap-2 md:flex-row md:items-center">
<Input
name="envKey"
Expand Down Expand Up @@ -473,29 +504,6 @@ const FunctionSettings = ({ func, refetch }: FunctionSettingsProps) => {
{t('env.add')}
</Button>
</div>
{Object.entries<string>(values.env).map(([key, value], index) => {
return (
<div
key={`${key}-${value}-${index}`}
className="flex flex-col items-start gap-2 md:flex-row md:items-center"
>
<Input name={`${key}-key`} placeholder={key} disabled />
<Input name={`${key}-value`} placeholder={new Array(value.length).fill('*').join('')} disabled />
<Button
disabled={updateFunction.isLoading}
variant="danger"
onClick={() => {
const newEnv = { ...values.env };
delete newEnv[key];

form.change('env', newEnv);
}}
>
{t('env.remove')}
</Button>
</div>
);
})}
<Button variant="primary" disabled={updateFunction.isLoading} submit>
{t('env.submit')}
</Button>
Expand Down

0 comments on commit 5f257d8

Please sign in to comment.