Skip to content

Commit

Permalink
feat(app): wrap the config fields with <form>
Browse files Browse the repository at this point in the history
  • Loading branch information
leventebalogh committed Aug 7, 2024
1 parent 345017e commit 297d98d
Showing 1 changed file with 26 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export const AppConfig = ({ plugin }: AppConfigProps) => {
isApiKeySet: Boolean(secureJsonFields?.apiKey),
});

const isSubmitDisabled = Boolean(!state.apiUrl || (!state.isApiKeySet && !state.apiKey));

const onResetApiKey = () =>
setState({
...state,
Expand All @@ -44,8 +46,30 @@ export const AppConfig = ({ plugin }: AppConfigProps) => {
});
};

const onSubmit = () => {
if (isSubmitDisabled) {
return;
}

updatePluginAndReload(plugin.meta.id, {
enabled,
pinned,
jsonData: {
apiUrl: state.apiUrl,
},
// This cannot be queried later by the frontend.
// We don't want to override it in case it was set previously and left untouched now.
secureJsonData: state.isApiKeySet
? undefined
: {
apiKey: state.apiKey,
},
});
};

return (
<div data-testid={testIds.appConfig.container}>
<form onSubmit={onSubmit}>
<FieldSet label="API Settings">
<Field label="API Key" description="A secret key for authenticating to our custom API">
<SecretInput
Expand Down Expand Up @@ -77,28 +101,13 @@ export const AppConfig = ({ plugin }: AppConfigProps) => {
<Button
type="submit"
data-testid={testIds.appConfig.submit}
onClick={() =>
updatePluginAndReload(plugin.meta.id, {
enabled,
pinned,
jsonData: {
apiUrl: state.apiUrl,
},
// This cannot be queried later by the frontend.
// We don't want to override it in case it was set previously and left untouched now.
secureJsonData: state.isApiKeySet
? undefined
: {
apiKey: state.apiKey,
},
})
}
disabled={Boolean(!state.apiUrl || (!state.isApiKeySet && !state.apiKey))}
disabled={isSubmitDisabled}
>
Save API settings
</Button>
</div>
</FieldSet>
</form>
</div>
);
};
Expand Down

0 comments on commit 297d98d

Please sign in to comment.