simplify conditionals

This commit is contained in:
Elvyra 2025-02-05 15:29:55 +01:00
parent 7b50083d20
commit 64a657b936

View file

@ -77,20 +77,18 @@ export function SettingArrayComponent({
return; return;
} }
if (option.type === OptionType.ARRAY) { if (items.includes(text)) {
const isValid = option.isValid?.call(definedSettings, text) ?? true; setError("This item is already added");
if (typeof isValid === "string") setError(isValid);
else if (!isValid) setError("Invalid input provided.");
else setError(null);
return; return;
} }
if (!isNaN(Number(text)) && text !== "") {
if (option.type !== OptionType.ARRAY && !isNaN(Number(text)) && text !== "") {
if (text.length >= 18 && text.length <= 19) { if (text.length >= 18 && text.length <= 19) {
setError(null); setError(null);
} else { } else {
setError("Invalid ID"); setError("Invalid ID");
} }
} else if (text !== "") { } else {
const isValid = option.isValid?.call(definedSettings, text) ?? true; const isValid = option.isValid?.call(definedSettings, text) ?? true;
if (typeof isValid === "string") setError(isValid); if (typeof isValid === "string") setError(isValid);
else if (!isValid) setError("Invalid input provided."); else if (!isValid) setError("Invalid input provided.");
@ -331,19 +329,6 @@ export function SettingArrayComponent({
return elements; return elements;
} }
function handleSubmit() {
if (items.includes(text)) {
setError("This item is already added");
setText("");
return;
}
setItems([...items, text]);
setText("");
setError(null);
}
return ( return (
<Forms.FormSection> <Forms.FormSection>
@ -390,9 +375,9 @@ export function SettingArrayComponent({
<Button <Button
size={Button.Sizes.MIN} size={Button.Sizes.MIN}
id={cl("add-button")} id={cl("add-button")}
onClick={handleSubmit} onClick={() => { setItems([...items, text]); setText(""); }}
style={{ background: "none" }} style={{ background: "none" }}
disabled={((text.length < 18 || text.length > 19) && option.type !== OptionType.ARRAY) || text === "" || error != null} disabled={text === "" || error != null}
> >
<CheckMarkIcon /> <CheckMarkIcon />
</Button> : </Button> :