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

move validation into defence mechanism #212

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
35 changes: 23 additions & 12 deletions frontend/src/components/DefenceBox/DefenceMechanism.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useState } from "react";
import { DefenceConfig, DefenceInfo } from "../../models/defence";
import { validateDefence } from "../../service/defenceService";
import "./DefenceMechanism.css";
import "../StrategyBox/StrategyMechanism.css";
import DefenceConfiguration from "./DefenceConfiguration";
Expand All @@ -26,18 +27,28 @@ function DefenceMechanism({
const [configValidated, setConfigValidated] = useState<boolean>(true);

const setConfigurationValue = async (configId: string, value: string) => {
const newConfiguration = defenceDetail.config.map((config) => {
if (config.id === configId) {
config.value = value;
}
return config;
});
setDefenceConfiguration(defenceDetail.id, newConfiguration).then(
(configured) => {
setIsConfigured(true);
setConfigValidated(configured);
}
);
const configName =
defenceDetail.config.find((config) => config.id === configId)?.name || "";

const configIsValid = validateDefence(defenceDetail.id, configName, value);
if (configIsValid) {
const newConfiguration = defenceDetail.config.map((config) => {
if (config.id === configId) {
config.value = value;
}
return config;
});
setDefenceConfiguration(defenceDetail.id, newConfiguration).then(
(configured) => {
setIsConfigured(true);
setConfigValidated(configured);
}
);
} else {
setConfigValidated(false);
setIsConfigured(true);
}

// hide the message after 3 seconds
setTimeout(() => {
setIsConfigured(false);
Expand Down
6 changes: 1 addition & 5 deletions frontend/src/service/defenceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ async function configureDefence(
config: DefenceConfig[],
phase: number
): Promise<boolean> {
const validation = config.map((c) => validateDefence(id, c.name, c.value));
if (validation.includes(false)) {
console.log("Invalid value for defence configuration", id, config);
return false;
}
const response = await sendRequest(
PATH + "configure",
"POST",
Expand Down Expand Up @@ -102,4 +97,5 @@ export {
deactivateDefence,
configureDefence,
resetActiveDefences,
validateDefence,
};