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

Only set config value if it's different #363

Closed
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
8 changes: 3 additions & 5 deletions frontend/src/components/DefenceBox/DefenceConfiguration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function DefenceConfiguration({
}: {
config: DefenceConfig;
isActive: boolean;
setConfigurationValue: (configId: string, value: string) => Promise<void>;
setConfigurationValue: (value: string) => void;
}) {
function inputKeyDown(event: React.KeyboardEvent<HTMLDivElement>) {
if (event.key === "Enter" && !event.shiftKey) {
Expand All @@ -21,15 +21,13 @@ function DefenceConfiguration({
function inputKeyUp(event: React.KeyboardEvent<HTMLDivElement>) {
if (event.key === "Enter" && !event.shiftKey) {
const value = event.currentTarget.innerText.trim();
// asynchronously set the configuration value
void setConfigurationValue(config.id, value);
setConfigurationValue(value);
}
}

function focusLost(event: React.FocusEvent<HTMLDivElement>) {
const value = event.target.innerText.trim();
// asynchronously set the configuration value
void setConfigurationValue(config.id, value);
setConfigurationValue(value);
Copy link
Member

Choose a reason for hiding this comment

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

This onBlur function would be completely unnecessary if we were using a native html input / textarea.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Out of interest, would this not still be required with input and textareas? To set the value on focus loss?

}

function getClassName() {
Expand Down
23 changes: 14 additions & 9 deletions frontend/src/components/DefenceBox/DefenceMechanism.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,19 @@ function DefenceMechanism({
const [isConfigured, setIsConfigured] = useState<boolean>(false);
const [configValidated, setConfigValidated] = useState<boolean>(true);

async function setConfigurationValue(configId: string, value: string) {
const configName =
defenceDetail.config.find((config) => config.id === configId)?.name ?? "";
async function setConfigurationValue(config: DefenceConfig, value: string) {
// don't set if the value is the same
if (config.value === value) {
return;
}

const configIsValid = validateDefence(defenceDetail.id, configName, value);
const configIsValid = validateDefence(defenceDetail.id, config.name, value);
if (configIsValid) {
const newConfiguration = defenceDetail.config.map((config) => {
if (config.id === configId) {
config.value = value;
const newConfiguration = defenceDetail.config.map((oldConfig) => {
if (oldConfig.id === config.id) {
oldConfig.value = value;
}
return config;
return oldConfig;
});

const configured = await setDefenceConfiguration(
Expand Down Expand Up @@ -78,7 +80,10 @@ function DefenceMechanism({
key={config.id}
isActive={defenceDetail.isActive}
config={config}
setConfigurationValue={setConfigurationValue}
setConfigurationValue={(value: string) => {
// asynchronously set the configuration value
void setConfigurationValue(config, value);
}}
/>
);
})}
Expand Down