Move input validation into lib

This commit is contained in:
Rūdolfs Ošiņš
2026-10-01 18:20:25 +02:00
parent 71c31e234c
commit a1c3622f05
5 changed files with 103 additions and 66 deletions
+7 -33
View File
@@ -1,12 +1,9 @@
<script lang="ts">
import type { Author } from "@bindings/cob/Author";
import { parseAssignee } from "@app/lib/inputValidation";
import { invoke } from "@app/lib/invoke";
import {
authorForNodeId,
parseNodeId,
publicKeyFromDid,
} from "@app/lib/utils";
import { authorForNodeId, publicKeyFromDid } from "@app/lib/utils";
import Button from "@app/components/Button.svelte";
import Icon from "@app/components/Icon.svelte";
@@ -32,9 +29,9 @@
let updatedAssignees: Author[] = $state([]);
let showInput: boolean = $state(false);
let inputValue = $state("");
let validationMessage: string | undefined = $state(undefined);
let valid: boolean = $state(false);
let assignee: string | undefined = undefined;
const parsed = $derived(parseAssignee(inputValue, updatedAssignees));
const validationMessage = $derived(parsed.error);
const valid = $derived(parsed.error === undefined);
let removeToggles: Record<string, boolean> = $state({});
@@ -45,35 +42,12 @@
updatedAssignees = assignees;
showInput = false;
validationMessage = undefined;
valid = true;
removeToggles = {};
});
$effect(() => {
if (inputValue === "") {
validationMessage = "";
valid = true;
} else {
const parsedNodeId = parseNodeId(inputValue);
if (parsedNodeId) {
assignee = `${parsedNodeId.prefix}${parsedNodeId.pubkey}`;
if (updatedAssignees.find(({ did }) => did === assignee)) {
validationMessage = "This assignee is already added";
valid = false;
} else {
validationMessage = undefined;
valid = true;
}
} else {
validationMessage = "This is not a valid DID";
valid = false;
}
}
});
async function addAssignee() {
if (valid && assignee) {
const assignee = parsed.did;
if (assignee) {
const alias = await invoke<string | null>("alias", {
nid: publicKeyFromDid(assignee),
});
+4 -22
View File
@@ -1,4 +1,6 @@
<script lang="ts">
import { labelError } from "@app/lib/inputValidation";
import Button from "@app/components/Button.svelte";
import Icon from "@app/components/Icon.svelte";
import Label from "@app/components/Label.svelte";
@@ -23,10 +25,9 @@
let updatedLabels: string[] = $state([]);
let showInput: boolean = $state(false);
let inputValue = $state("");
let validationMessage: string | undefined = $state();
let valid: boolean = $state(false);
const sanitizedValue = $derived(inputValue.trim());
const validationMessage = $derived(labelError(inputValue, updatedLabels));
const valid = $derived(validationMessage === undefined);
let removeToggles: Record<string, boolean> = $state({});
@@ -37,28 +38,9 @@
updatedLabels = labels;
showInput = false;
validationMessage = undefined;
valid = true;
removeToggles = {};
});
$effect(() => {
if (inputValue !== "") {
if (sanitizedValue.length > 0) {
if (updatedLabels.includes(sanitizedValue)) {
valid = false;
validationMessage = "This label is already assigned";
} else {
valid = true;
validationMessage = undefined;
}
}
} else {
valid = true;
validationMessage = "";
}
});
function addLabel() {
if (valid && sanitizedValue) {
updatedLabels = [...updatedLabels, sanitizedValue].sort();