Validate the pending subscriber import

React · React · beginner · greenfield

Adds the pending-import check to the newsletter admin. The panel fetches the stored upload's text, validates the header line against the expected columns, and shows either the mismatch message or a ready-to-import count of the data rows, with loading and error states around the fetch. Tried it against a hand-seeded pending import and both the happy path and the wrong-file case behave as specced.

Requirements

Files touched

--- src/components/SubscriberImportCheck.jsx
+import { useEffect, useState } from 'react';
+
+const EXPECTED_HEADER = 'email,name,status';
+
+export function validateSubscriberCsv(text) {
+  const lines = text.split(/\r?\n/).filter((line) => line.trim().length > 0);
+  if (lines.length === 0 || lines[0] !== EXPECTED_HEADER) {
+    return { ok: false, count: 0 };
+  }
+  return { ok: true, count: lines.length - 1 };
+}
+
+export default function SubscriberImportCheck() {
+  const [result, setResult] = useState(null);
+  const [failed, setFailed] = useState(false);
+
+  useEffect(() => {
+    let cancelled = false;
+    async function load() {
+      try {
+        const res = await fetch('/api/imports/pending');
+        if (!res.ok) throw new Error(`HTTP ${res.status}`);
+        const data = await res.json();
+        if (!cancelled) setResult(validateSubscriberCsv(data.csv));
+      } catch {
+        if (!cancelled) setFailed(true);
+      }
+    }
+    load();

Review this PR

React practice