Skip to content
Open
Show file tree
Hide file tree
Changes from 9 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
4 changes: 3 additions & 1 deletion client/src/Hooks/monitorHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ const useDeleteMonitor = () => {

const useUpdateMonitor = () => {
const [isLoading, setIsLoading] = useState(false);

const navigate = useNavigate();
const updateMonitor = async ({ monitor, redirect }) => {
try {
Expand All @@ -376,10 +377,11 @@ const useUpdateMonitor = () => {
expectedValue: monitor.expectedValue,
ignoreTlsErrors: monitor.ignoreTlsErrors,
jsonPath: monitor.jsonPath,
url: monitor.url,
...((monitor.type === "port" || monitor.type === "game") && {
port: monitor.port,
}),
...(monitor.type == "game" && {
...(monitor.type === "game" && {
gameId: monitor.gameId,
}),
...(monitor.type === "hardware" && {
Expand Down
59 changes: 33 additions & 26 deletions client/src/Pages/Infrastructure/Create/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ const CreateInfrastructureMonitor = () => {
return errorKey ? errors[errorKey] : null;
};

const pageSchema = infrastructureMonitorValidation.fork(["url"], (s) =>
isCreate ? s.required() : s.optional()
);

// Populate form fields if editing
useEffect(() => {
if (isCreate) {
Expand Down Expand Up @@ -196,7 +200,7 @@ const CreateInfrastructureMonitor = () => {
secret: infrastructureMonitor.secret,
};

const { error } = infrastructureMonitorValidation.validate(form, {
const { error } = pageSchema.validate(form, {
abortEarly: false,
});

Expand Down Expand Up @@ -234,6 +238,7 @@ const CreateInfrastructureMonitor = () => {
form = {
...(isCreate ? {} : { _id: monitorId }),
...rest,
url: `http${https ? "s" : ""}://` + infrastructureMonitor.url,
description: form.name,
type: "hardware",
notifications: infrastructureMonitor.notifications,
Expand All @@ -257,8 +262,11 @@ const CreateInfrastructureMonitor = () => {
[name]: value,
});

const adjustedValue =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the logic implemented here can be simplified. So many nested ternary become hard to read in the longer run and maintain.

name === "url" ? (value ? `http${https ? "s" : ""}://${value}` : value) : value;

const { error } = infrastructureMonitorValidation.validate(
{ [name]: value },
{ [name]: adjustedValue },
{ abortEarly: false }
);
setErrors((prev) => ({
Expand Down Expand Up @@ -470,31 +478,30 @@ const CreateInfrastructureMonitor = () => {
onChange={onChange}
error={errors["url"] ? true : false}
helperText={errors["url"]}
disabled={!isCreate}
/>
{isCreate && (
<FieldWrapper
label={t("infrastructureProtocol")}
labelVariant="p"
>
<ButtonGroup>
<Button
variant="group"
filled={https.toString()}
onClick={() => setHttps(true)}
>
{t("https")}
</Button>
<Button
variant="group"
filled={(!https).toString()}
onClick={() => setHttps(false)}
>
{t("http")}
</Button>
</ButtonGroup>
</FieldWrapper>
)}

<FieldWrapper
label={t("infrastructureProtocol")}
labelVariant="p"
>
<ButtonGroup>
<Button
variant="group"
filled={https.toString()}
onClick={() => setHttps(true)}
>
{t("https")}
</Button>
<Button
variant="group"
filled={(!https).toString()}
onClick={() => setHttps(false)}
>
{t("http")}
</Button>
</ButtonGroup>
</FieldWrapper>

<TextInput
type="text"
id="name"
Expand Down
10 changes: 9 additions & 1 deletion server/src/validation/joi.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,11 @@ const createMonitorBodyValidation = joi.object({
name: joi.string().required(),
description: joi.string().required(),
type: joi.string().required(),
url: joi.string().required(),
url: joi
.string()
.trim()
.uri({ scheme: ["http", "https"] })
.required(),
ignoreTlsErrors: joi.boolean().default(false),
port: joi.number(),
isActive: joi.boolean(),
Expand All @@ -182,6 +186,10 @@ const createMonitorsBodyValidation = joi.array().items(
);

const editMonitorBodyValidation = joi.object({
url: joi
.string()
.uri({ scheme: ["http", "https"] })
.optional(),
name: joi.string(),
description: joi.string(),
interval: joi.number(),
Expand Down