This commit is contained in:
1 parent
e85e4d74ea
commit
2e4ed76121
378 files changed
+22404
-20686
No files matched your search
@@ -1,63 +1,57 @@
|
||||
'use client'
|
||||
"use client";
|
||||
|
||||
import { AlertTriangle, CheckCircle2, Loader2, Upload } from 'lucide-react'
|
||||
import { useMemo, useState } from 'react'
|
||||
import { toast } from 'sonner'
|
||||
import { createCatalogItem } from '@/actions/catalog-items'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogTrigger,
|
||||
} from '@/components/ui/dialog'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import { Textarea } from '@/components/ui/textarea'
|
||||
import { AlertTriangle, CheckCircle2, Loader2, Upload } from "lucide-react";
|
||||
import { useMemo, useState } from "react";
|
||||
import { toast } from "sonner";
|
||||
import { createCatalogItem } from "@/actions/catalog-items";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { Textarea } from "@/components/ui/textarea";
|
||||
|
||||
interface ParsedRow {
|
||||
lineNumber: number
|
||||
raw: string
|
||||
ok: boolean
|
||||
error?: string
|
||||
baseId?: number
|
||||
credits?: number
|
||||
points?: number
|
||||
pointsType?: number
|
||||
lineNumber: number;
|
||||
raw: string;
|
||||
ok: boolean;
|
||||
error?: string;
|
||||
baseId?: number;
|
||||
credits?: number;
|
||||
points?: number;
|
||||
pointsType?: number;
|
||||
}
|
||||
|
||||
interface BulkImportItemsProps {
|
||||
pageId: number
|
||||
onImported?: () => void
|
||||
pageId: number;
|
||||
onImported?: () => void;
|
||||
}
|
||||
|
||||
export function BulkImportItems({ pageId, onImported }: BulkImportItemsProps) {
|
||||
const [open, setOpen] = useState(false)
|
||||
const [input, setInput] = useState('')
|
||||
const [importing, setImporting] = useState(false)
|
||||
const [open, setOpen] = useState(false);
|
||||
const [input, setInput] = useState("");
|
||||
const [importing, setImporting] = useState(false);
|
||||
|
||||
const parsed = useMemo<ParsedRow[]>(() => {
|
||||
const lines = input.split('\n')
|
||||
const out: ParsedRow[] = []
|
||||
const lines = input.split("\n");
|
||||
const out: ParsedRow[] = [];
|
||||
lines.forEach((raw, idx) => {
|
||||
const trimmed = raw.trim()
|
||||
if (!trimmed || trimmed.startsWith('#')) return
|
||||
const parts = trimmed.split(/[,\s;]+/).filter(Boolean)
|
||||
const baseId = parseInt(parts[0], 10)
|
||||
const trimmed = raw.trim();
|
||||
if (!trimmed || trimmed.startsWith("#")) return;
|
||||
const parts = trimmed.split(/[,\s;]+/).filter(Boolean);
|
||||
const baseId = parseInt(parts[0], 10);
|
||||
if (Number.isNaN(baseId) || baseId <= 0) {
|
||||
out.push({ lineNumber: idx + 1, raw: trimmed, ok: false, error: 'Invalid base ID' })
|
||||
return
|
||||
out.push({ lineNumber: idx + 1, raw: trimmed, ok: false, error: "Invalid base ID" });
|
||||
return;
|
||||
}
|
||||
const credits = parts[1] !== undefined ? parseInt(parts[1], 10) : 0
|
||||
const points = parts[2] !== undefined ? parseInt(parts[2], 10) : 0
|
||||
const pointsType = parts[3] !== undefined ? parseInt(parts[3], 10) : 0
|
||||
const credits = parts[1] !== undefined ? parseInt(parts[1], 10) : 0;
|
||||
const points = parts[2] !== undefined ? parseInt(parts[2], 10) : 0;
|
||||
const pointsType = parts[3] !== undefined ? parseInt(parts[3], 10) : 0;
|
||||
if (
|
||||
(parts[1] !== undefined && Number.isNaN(credits)) ||
|
||||
(parts[2] !== undefined && Number.isNaN(points))
|
||||
) {
|
||||
out.push({ lineNumber: idx + 1, raw: trimmed, ok: false, error: 'Invalid number' })
|
||||
return
|
||||
out.push({ lineNumber: idx + 1, raw: trimmed, ok: false, error: "Invalid number" });
|
||||
return;
|
||||
}
|
||||
out.push({
|
||||
lineNumber: idx + 1,
|
||||
@@ -67,29 +61,29 @@ export function BulkImportItems({ pageId, onImported }: BulkImportItemsProps) {
|
||||
credits,
|
||||
points,
|
||||
pointsType,
|
||||
})
|
||||
})
|
||||
return out
|
||||
}, [input])
|
||||
});
|
||||
});
|
||||
return out;
|
||||
}, [input]);
|
||||
|
||||
const validCount = parsed.filter((r) => r.ok).length
|
||||
const invalidCount = parsed.length - validCount
|
||||
const validCount = parsed.filter((r) => r.ok).length;
|
||||
const invalidCount = parsed.length - validCount;
|
||||
|
||||
async function handleImport() {
|
||||
const valid = parsed.filter((r) => r.ok)
|
||||
const valid = parsed.filter((r) => r.ok);
|
||||
if (valid.length === 0) {
|
||||
toast.error('No valid rows to import')
|
||||
return
|
||||
toast.error("No valid rows to import");
|
||||
return;
|
||||
}
|
||||
setImporting(true)
|
||||
let success = 0
|
||||
let failed = 0
|
||||
setImporting(true);
|
||||
let success = 0;
|
||||
let failed = 0;
|
||||
for (const row of valid) {
|
||||
try {
|
||||
const res = await createCatalogItem({
|
||||
pageId,
|
||||
itemIds: String(row.baseId!),
|
||||
catalogName: '',
|
||||
catalogName: "",
|
||||
costCredits: row.credits ?? 0,
|
||||
costPoints: row.points ?? 0,
|
||||
pointsType: row.pointsType ?? 0,
|
||||
@@ -99,24 +93,24 @@ export function BulkImportItems({ pageId, onImported }: BulkImportItemsProps) {
|
||||
orderNumber: 1,
|
||||
offerId: -1,
|
||||
songId: 0,
|
||||
haveOffer: '1',
|
||||
clubOnly: '0',
|
||||
extradata: '',
|
||||
})
|
||||
if (res.ok) success++
|
||||
else failed++
|
||||
haveOffer: "1",
|
||||
clubOnly: "0",
|
||||
extradata: "",
|
||||
});
|
||||
if (res.ok) success++;
|
||||
else failed++;
|
||||
} catch {
|
||||
failed++
|
||||
failed++;
|
||||
}
|
||||
}
|
||||
setImporting(false)
|
||||
setImporting(false);
|
||||
if (success > 0) {
|
||||
toast.success(`Imported ${success} item(s)${failed > 0 ? `, ${failed} failed` : ''}.`)
|
||||
setInput('')
|
||||
setOpen(false)
|
||||
onImported?.()
|
||||
toast.success(`Imported ${success} item(s)${failed > 0 ? `, ${failed} failed` : ""}.`);
|
||||
setInput("");
|
||||
setOpen(false);
|
||||
onImported?.();
|
||||
} else {
|
||||
toast.error(`All ${failed} imports failed.`)
|
||||
toast.error(`All ${failed} imports failed.`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -144,10 +138,9 @@ export function BulkImportItems({ pageId, onImported }: BulkImportItemsProps) {
|
||||
className="font-mono text-xs"
|
||||
/>
|
||||
<p className="text-[11px] text-muted-foreground">
|
||||
Format:{' '}
|
||||
<code className="rounded bg-muted px-1">base_id, credits, points, points_type</code>.
|
||||
Only <code>base_id</code> is required; other fields default to 0. Separators: comma,
|
||||
space, or semicolon.
|
||||
Format: <code className="rounded bg-muted px-1">base_id, credits, points, points_type</code>.
|
||||
Only <code>base_id</code> is required; other fields default to 0. Separators: comma, space, or
|
||||
semicolon.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
@@ -179,14 +172,12 @@ export function BulkImportItems({ pageId, onImported }: BulkImportItemsProps) {
|
||||
</thead>
|
||||
<tbody>
|
||||
{parsed.map((row) => (
|
||||
<tr key={row.lineNumber} className={row.ok ? '' : 'bg-destructive/10'}>
|
||||
<td className="px-2 py-0.5 font-mono text-muted-foreground">
|
||||
{row.lineNumber}
|
||||
</td>
|
||||
<td className="px-2 py-0.5 font-mono">{row.baseId ?? '—'}</td>
|
||||
<td className="px-2 py-0.5 font-mono">{row.credits ?? '—'}</td>
|
||||
<td className="px-2 py-0.5 font-mono">{row.points ?? '—'}</td>
|
||||
<td className="px-2 py-0.5 font-mono">{row.pointsType ?? '—'}</td>
|
||||
<tr key={row.lineNumber} className={row.ok ? "" : "bg-destructive/10"}>
|
||||
<td className="px-2 py-0.5 font-mono text-muted-foreground">{row.lineNumber}</td>
|
||||
<td className="px-2 py-0.5 font-mono">{row.baseId ?? "—"}</td>
|
||||
<td className="px-2 py-0.5 font-mono">{row.credits ?? "—"}</td>
|
||||
<td className="px-2 py-0.5 font-mono">{row.points ?? "—"}</td>
|
||||
<td className="px-2 py-0.5 font-mono">{row.pointsType ?? "—"}</td>
|
||||
<td className="px-2 py-0.5">
|
||||
{row.ok ? (
|
||||
<span className="text-[var(--admin-success)]">OK</span>
|
||||
@@ -212,11 +203,11 @@ export function BulkImportItems({ pageId, onImported }: BulkImportItemsProps) {
|
||||
) : (
|
||||
<Upload className="mr-2 h-4 w-4" />
|
||||
)}
|
||||
Import {validCount} item{validCount !== 1 ? 's' : ''}
|
||||
Import {validCount} item{validCount !== 1 ? "s" : ""}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
)
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user