2022-04-21 18:12:42 +00:00
|
|
|
import React, { useState } from "react";
|
2025-03-27 20:56:38 +00:00
|
|
|
import { size } from "lodash";
|
2022-04-21 18:12:42 +00:00
|
|
|
|
2025-08-05 20:29:55 +00:00
|
|
|
import { IInputFieldParseTarget } from "interfaces/form_field";
|
|
|
|
|
|
2025-09-29 17:10:41 +00:00
|
|
|
import SettingsSection from "pages/admin/components/SettingsSection";
|
2022-04-21 18:12:42 +00:00
|
|
|
import Button from "components/buttons/Button";
|
|
|
|
|
import InputField from "components/forms/fields/InputField";
|
2023-02-22 14:05:38 +00:00
|
|
|
import validUrl from "components/forms/validators/valid_url";
|
2025-02-21 20:22:08 +00:00
|
|
|
import GitOpsModeTooltipWrapper from "components/GitOpsModeTooltipWrapper";
|
2023-02-22 14:05:38 +00:00
|
|
|
|
2025-03-27 20:56:38 +00:00
|
|
|
import INVALID_SERVER_URL_MESSAGE from "utilities/error_messages";
|
|
|
|
|
|
2025-08-05 20:29:55 +00:00
|
|
|
import { IAppConfigFormProps } from "../constants";
|
2024-04-26 19:14:49 +00:00
|
|
|
|
|
|
|
|
interface IWebAddressFormData {
|
|
|
|
|
serverURL: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface IWebAddressFormErrors {
|
|
|
|
|
server_url?: string | null;
|
|
|
|
|
}
|
2022-04-21 18:12:42 +00:00
|
|
|
const baseClass = "app-config-form";
|
|
|
|
|
|
2025-03-27 20:56:38 +00:00
|
|
|
const validateFormData = ({ serverURL }: IWebAddressFormData) => {
|
|
|
|
|
const errors: IWebAddressFormErrors = {};
|
|
|
|
|
if (!serverURL) {
|
|
|
|
|
errors.server_url = "Fleet server URL must be present";
|
|
|
|
|
} else if (
|
|
|
|
|
!validUrl({
|
|
|
|
|
url: serverURL,
|
|
|
|
|
protocols: ["http", "https"],
|
|
|
|
|
allowLocalHost: true,
|
|
|
|
|
})
|
|
|
|
|
) {
|
|
|
|
|
errors.server_url = INVALID_SERVER_URL_MESSAGE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return errors;
|
|
|
|
|
};
|
|
|
|
|
|
2022-04-21 18:12:42 +00:00
|
|
|
const WebAddress = ({
|
|
|
|
|
appConfig,
|
|
|
|
|
handleSubmit,
|
2022-08-29 15:21:37 +00:00
|
|
|
isUpdatingSettings,
|
2022-04-21 18:12:42 +00:00
|
|
|
}: IAppConfigFormProps): JSX.Element => {
|
2025-02-21 20:22:08 +00:00
|
|
|
const gitOpsModeEnabled = appConfig.gitops.gitops_mode_enabled;
|
|
|
|
|
|
2024-04-26 19:14:49 +00:00
|
|
|
const [formData, setFormData] = useState<IWebAddressFormData>({
|
2022-04-21 18:12:42 +00:00
|
|
|
serverURL: appConfig.server_settings.server_url || "",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const { serverURL } = formData;
|
|
|
|
|
|
2024-04-26 19:14:49 +00:00
|
|
|
const [formErrors, setFormErrors] = useState<IWebAddressFormErrors>({});
|
2022-04-21 18:12:42 +00:00
|
|
|
|
2025-08-05 20:29:55 +00:00
|
|
|
const onInputChange = ({ name, value }: IInputFieldParseTarget) => {
|
2025-03-27 20:56:38 +00:00
|
|
|
const newFormData = { ...formData, [name]: value };
|
|
|
|
|
setFormData(newFormData);
|
|
|
|
|
const newErrs = validateFormData(newFormData);
|
|
|
|
|
// only set errors that are updates of existing errors
|
|
|
|
|
// new errors are only set onBlur
|
|
|
|
|
const errsToSet: Record<string, string> = {};
|
|
|
|
|
Object.keys(formErrors).forEach((k) => {
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
if (newErrs[k]) {
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
errsToSet[k] = newErrs[k];
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
setFormErrors(errsToSet);
|
2022-04-21 18:12:42 +00:00
|
|
|
};
|
|
|
|
|
|
2025-03-27 20:56:38 +00:00
|
|
|
const onInputBlur = () => {
|
|
|
|
|
setFormErrors(validateFormData(formData));
|
2022-04-21 18:12:42 +00:00
|
|
|
};
|
|
|
|
|
|
2025-03-27 20:56:38 +00:00
|
|
|
const onFormSubmit = (evt: React.FormEvent<HTMLFormElement>) => {
|
2022-04-21 18:12:42 +00:00
|
|
|
evt.preventDefault();
|
2025-03-27 20:56:38 +00:00
|
|
|
// return null if there are errors
|
|
|
|
|
const errs = validateFormData(formData);
|
|
|
|
|
if (size(errs)) {
|
|
|
|
|
setFormErrors(errs);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-04-21 18:12:42 +00:00
|
|
|
|
|
|
|
|
// Formatting of API not UI
|
|
|
|
|
const formDataToSubmit = {
|
|
|
|
|
server_settings: {
|
|
|
|
|
server_url: serverURL,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
handleSubmit(formDataToSubmit);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
2025-09-29 17:10:41 +00:00
|
|
|
<SettingsSection className={baseClass} title="Fleet web address">
|
|
|
|
|
<form onSubmit={onFormSubmit} autoComplete="off">
|
|
|
|
|
<InputField
|
|
|
|
|
label="URL"
|
|
|
|
|
helpText={
|
|
|
|
|
<>
|
|
|
|
|
Include base path only (eg. no <code>/latest</code>)
|
|
|
|
|
</>
|
|
|
|
|
}
|
|
|
|
|
onChange={onInputChange}
|
|
|
|
|
name="serverURL"
|
|
|
|
|
value={serverURL}
|
|
|
|
|
parseTarget
|
|
|
|
|
onBlur={onInputBlur}
|
|
|
|
|
error={formErrors.server_url}
|
|
|
|
|
tooltip="The base URL of this instance for use in Fleet links."
|
|
|
|
|
disabled={gitOpsModeEnabled}
|
|
|
|
|
/>
|
|
|
|
|
<GitOpsModeTooltipWrapper
|
|
|
|
|
tipOffset={-8}
|
|
|
|
|
renderChildren={(disableChildren) => (
|
|
|
|
|
<Button
|
|
|
|
|
type="submit"
|
|
|
|
|
disabled={!!size(formErrors) || disableChildren}
|
|
|
|
|
className="button-wrap"
|
|
|
|
|
isLoading={isUpdatingSettings}
|
|
|
|
|
>
|
|
|
|
|
Save
|
|
|
|
|
</Button>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
</form>
|
|
|
|
|
</SettingsSection>
|
2022-04-21 18:12:42 +00:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default WebAddress;
|