uwu rawr :3

This commit is contained in:
g
2025-02-14 20:36:48 +00:00
parent 1db930d9a3
commit 1804e52e83
3 changed files with 174 additions and 23 deletions

View File

@@ -0,0 +1,139 @@
"use client";
import { useState } from "react";
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
import { Upload, AlertCircle } from "lucide-react";
import { toast } from "sonner";
import { Alert, AlertDescription } from "@/components/ui/alert";
interface ImportProductsModalProps {
open: boolean;
setOpen: (open: boolean) => void;
onImportComplete: () => void;
}
export default function ImportProductsModal({ open, setOpen, onImportComplete }: ImportProductsModalProps) {
const [file, setFile] = useState<File | null>(null);
const [isUploading, setIsUploading] = useState(false);
const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const selectedFile = e.target.files?.[0];
if (selectedFile && selectedFile.type === "text/plain") {
setFile(selectedFile);
} else {
toast.error("Please select a valid .txt file");
}
};
const handleImport = async () => {
if (!file) {
toast.error("Please select a file first");
return;
}
try {
setIsUploading(true);
const fileContent = await file.text();
const products = []
const sections = fileContent.split("----------------------------------------------------------")
for (const section of sections) {
if(!section.trim()) continue
const lines = section.trim().split("\n")
const name = lines[0].trim()
const category = lines[1].trim().split("->")[0].trim()
const subcategory = lines[1].trim().split("->")[1].split("•")[0].trim()
console.log(`${name} - ${category} - ${subcategory}`)
const pricing = lines.slice(3).filter(line => line.includes('@')).map(line => {
const price = line.split('@')[0].trim()
const unit = line.split('@')[1].trim()
return { price, unit }
})
console.log(pricing)
}
//toast.success(`Successfully imported ${result.count} products`);
onImportComplete();
setOpen(false);
} catch (error) {
toast.error("Failed to import products");
console.error(error);
} finally {
setIsUploading(false);
setFile(null);
}
};
return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogContent className="sm:max-w-md">
<DialogHeader>
<DialogTitle className="flex items-center gap-2">
<Upload className="h-5 w-5" />
Import Products
</DialogTitle>
</DialogHeader>
<div className="space-y-4">
<Alert>
<AlertCircle className="h-4 w-4" />
<AlertDescription>
File should be a .txt file with product details in the correct format
</AlertDescription>
</Alert>
<div className="grid w-full items-center gap-1.5">
<label
htmlFor="file-upload"
className="border-2 border-dashed rounded-lg p-8 text-center cursor-pointer hover:border-gray-400 transition-colors"
>
{file ? (
<div className="text-sm text-muted-foreground">
Selected: {file.name}
</div>
) : (
<div className="text-sm text-muted-foreground">
Click to select or drag and drop a .txt file
</div>
)}
<input
id="file-upload"
type="file"
accept=".txt"
onChange={handleFileChange}
className="hidden"
/>
</label>
</div>
</div>
<DialogFooter className="sm:justify-start">
<Button
type="button"
variant="secondary"
onClick={() => setOpen(false)}
>
Cancel
</Button>
<Button
type="button"
onClick={handleImport}
disabled={!file || isUploading}
>
{isUploading ? "Importing..." : "Import Products"}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}