Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Plugin: Wrap app config fields with <form> #1041

Merged
merged 3 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,29 @@ 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}>
leventebalogh marked this conversation as resolved.
Show resolved Hide resolved
<FieldSet label="API Settings">
<Field label="API Key" description="A secret key for authenticating to our custom API">
<SecretInput
Expand Down Expand Up @@ -77,29 +100,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>
</div>
</form>
);
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export const testIds = {
appConfig: {
container: 'data-testid ac-container',
apiKey: 'data-testid ac-api-key',
apiUrl: 'data-testid ac-api-url',
submit: 'data-testid ac-submit-form',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export const AppConfig = ({ plugin }: Props) => {
isApiKeySet: Boolean(jsonData?.isApiKeySet),
});

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

const onResetApiKey = () =>
setState({
...state,
Expand All @@ -54,51 +56,26 @@ export const AppConfig = ({ plugin }: Props) => {
});
};

return (
<div data-testid={testIds.appConfig.container}>
{/* ENABLE / DISABLE PLUGIN */}
<FieldSet label="Enable / Disable">
{!enabled && (
<>
<div className={s.colorWeak}>The plugin is currently not enabled.</div>
<Button
className={s.marginTop}
variant="primary"
onClick={() =>
updatePluginAndReload(plugin.meta.id, {
enabled: true,
pinned: true,
jsonData,
})
}
>
Enable plugin
</Button>
</>
)}

{/* Disable the plugin */}
{enabled && (
<>
<div className={s.colorWeak}>The plugin is currently enabled.</div>
<Button
className={s.marginTop}
variant="destructive"
onClick={() =>
updatePluginAndReload(plugin.meta.id, {
enabled: false,
pinned: false,
jsonData,
})
}
>
Disable plugin
</Button>
</>
)}
</FieldSet>
const onSubmit = () => {
updatePluginAndReload(plugin.meta.id, {
enabled,
pinned,
jsonData: {
apiUrl: state.apiUrl,
isApiKeySet: true,
},
// 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,
},
});
};

{/* CUSTOM SETTINGS */}
return (
<form onSubmit={onSubmit}>
<FieldSet label="API Settings" className={s.marginTopXl}>
{/* API Key */}
<Field label="API Key" description="A secret key for authenticating to our custom API">
Expand Down Expand Up @@ -128,33 +105,12 @@ export const AppConfig = ({ plugin }: Props) => {
</Field>

<div className={s.marginTop}>
<Button
type="submit"
data-testid={testIds.appConfig.submit}
onClick={() =>
updatePluginAndReload(plugin.meta.id, {
enabled,
pinned,
jsonData: {
apiUrl: state.apiUrl,
isApiKeySet: true,
},
// 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))}
>
<Button type="submit" data-testid={testIds.appConfig.submit} disabled={isSubmitDisabled}>
Save API settings
</Button>
</div>
</FieldSet>
</div>
</form>
);
};

Expand Down
Loading