Преглед на файлове

refactor(date-range): simplify date filter validation by utilizing centralized validation logic

Code_Uwe преди 1 седмица
родител
ревизия
96bc37a80e
променени са 2 файла, в които са добавени 14 реда и са изтрити 37 реда
  1. 12 36
      lib/frontend/search/dateFilterValidation.js
  2. 2 1
      lib/frontend/search/dateRangePickerUtils.js

+ 12 - 36
lib/frontend/search/dateFilterValidation.js

@@ -1,8 +1,5 @@
 import { ApiClientError } from "@/lib/frontend/apiClient";
-import {
-	isValidIsoDateYmd,
-	isInvalidIsoDateRange,
-} from "@/lib/frontend/search/dateRange";
+import { getSearchDateRangeValidation } from "@/lib/frontend/search/searchDateValidation";
 
 function isNonEmptyString(value) {
 	return typeof value === "string" && value.trim().length > 0;
@@ -12,21 +9,15 @@ function toTrimmedOrNull(value) {
 	return isNonEmptyString(value) ? value.trim() : null;
 }
 
-function buildValidationError(code, message, details) {
-	return new ApiClientError({
-		status: 400,
-		code,
-		message,
-		details,
-	});
-}
-
 /**
  * Build a local validation error for date filters.
  *
  * This is intentionally independent from the search query "q" so the UI can
  * validate immediately while the user edits the date range.
  *
+ * Single Source of Truth:
+ * - Uses getSearchDateRangeValidation(...) as the canonical validator.
+ *
  * @param {{ from?: string|null, to?: string|null }} input
  * @returns {ApiClientError|null}
  */
@@ -37,28 +28,13 @@ export function buildDateFilterValidationError({
 	const f = toTrimmedOrNull(from);
 	const t = toTrimmedOrNull(to);
 
-	if (f && !isValidIsoDateYmd(f)) {
-		return buildValidationError("VALIDATION_SEARCH_DATE", "Invalid from date", {
-			from: f,
-		});
-	}
+	const v = getSearchDateRangeValidation(f, t);
+	if (!v) return null;
 
-	if (t && !isValidIsoDateYmd(t)) {
-		return buildValidationError("VALIDATION_SEARCH_DATE", "Invalid to date", {
-			to: t,
-		});
-	}
-
-	// IMPORTANT:
-	// - from === to is valid and represents a single-day search.
-	// - Only from > to is invalid.
-	if (isInvalidIsoDateRange(f, t)) {
-		return buildValidationError(
-			"VALIDATION_SEARCH_RANGE",
-			"Invalid date range",
-			{ from: f, to: t }
-		);
-	}
-
-	return null;
+	return new ApiClientError({
+		status: 400,
+		code: v.code,
+		message: v.message,
+		details: v.details,
+	});
 }

+ 2 - 1
lib/frontend/search/dateRangePickerUtils.js

@@ -13,7 +13,8 @@ export function normalizeDayClickArgs(args) {
 	// - onDayClick(event, day, modifiers)
 	//
 	// Background:
-	// - There has been historical ambiguity around onDayClick argument order. :contentReference[oaicite:2]{index=2}
+	// - Some react-day-picker versions differ in onDayClick argument order; we support both.
+
 	const a0 = args?.[0];
 	const a1 = args?.[1];
 	const a2 = args?.[2];