Something went wrong. Try again.
A blockchain-powered certificate verification platform for @inovuslabs — enabling instant, tamper-proof validation of credentials through s… certificate.inovuslabs.org
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181"use client"
import { useEffect, useState } from "react"import * as XLSX from "xlsx"import { Label } from "@/components/ui/label"import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"import { Loader2 } from "lucide-react"
interface ExcelMapperProps { file: File onColumnMap: (mapping: Record<string, string>) => void}
// Required certificate fieldsconst requiredFields = [ { id: "recipientName", label: "Recipient Name" }, { id: "recipientEmail", label: "Recipient Email" }, { id: "certificateTitle", label: "Certificate Title" }, { id: "issueDate", label: "Issue Date" }, { id: "certificateId", label: "Certificate ID" },]
// Optional certificate fieldsconst optionalFields = [ { id: "expiryDate", label: "Expiry Date" }, { id: "description", label: "Description" }, { id: "issuerName", label: "Issuer Name" }, { id: "assetFilename", label: "Asset Filename" },]
export function ExcelMapper({ file, onColumnMap }: ExcelMapperProps) { const [columns, setColumns] = useState<string[]>([]) const [mapping, setMapping] = useState<Record<string, string>>({}) const [loading, setLoading] = useState(true) const [previewData, setPreviewData] = useState<any[]>([])
useEffect(() => { const readExcel = async () => { try { setLoading(true)
const reader = new FileReader() reader.onload = (e) => { const data = new Uint8Array(e.target?.result as ArrayBuffer) const workbook = XLSX.read(data, { type: "array" })
// Get the first worksheet const worksheetName = workbook.SheetNames[0] const worksheet = workbook.Sheets[worksheetName]
// Convert to JSON const jsonData = XLSX.utils.sheet_to_json(worksheet)
if (jsonData.length > 0) { // Get column headers const headers = Object.keys(jsonData[0]) setColumns(headers)
// Set preview data (first 5 rows) setPreviewData(jsonData.slice(0, 5)) }
setLoading(false) }
reader.readAsArrayBuffer(file) } catch (error) { console.error("Error reading Excel file:", error) setLoading(false) } }
readExcel() }, [file])
const handleMappingChange = (fieldId: string, columnName: string) => { const newMapping = { ...mapping, [fieldId]: columnName } setMapping(newMapping) onColumnMap(newMapping) }
if (loading) { return ( <div className="flex justify-center items-center py-8"> <Loader2 className="h-8 w-8 animate-spin text-primary" /> <span className="ml-2">Reading Excel file...</span> </div> ) }
if (columns.length === 0) { return ( <div className="text-center py-4"> <p className="text-muted-foreground">No columns found in the Excel file. Please check the file format.</p> </div> ) }
return ( <div className="space-y-6"> <div className="space-y-4"> <h3 className="text-lg font-medium">Required Fields</h3> <div className="grid grid-cols-1 md:grid-cols-2 gap-4"> {requiredFields.map((field) => ( <div key={field.id} className="space-y-2"> <Label htmlFor={field.id}>{field.label}</Label> <Select value={mapping[field.id] || ""} onValueChange={(value) => handleMappingChange(field.id, value)}> <SelectTrigger id={field.id}> <SelectValue placeholder="Select a column" /> </SelectTrigger> <SelectContent> {columns.map((column) => ( <SelectItem key={column} value={column}> {column} </SelectItem> ))} </SelectContent> </Select> </div> ))} </div> </div>
<div className="space-y-4"> <h3 className="text-lg font-medium">Optional Fields</h3> <div className="grid grid-cols-1 md:grid-cols-2 gap-4"> {optionalFields.map((field) => ( <div key={field.id} className="space-y-2"> <Label htmlFor={field.id}>{field.label}</Label> <Select value={mapping[field.id] || ""} onValueChange={(value) => handleMappingChange(field.id, value)}> <SelectTrigger id={field.id}> <SelectValue placeholder="Select a column" /> </SelectTrigger> <SelectContent> <SelectItem value="">None</SelectItem> {columns.map((column) => ( <SelectItem key={column} value={column}> {column} </SelectItem> ))} </SelectContent> </Select> </div> ))} </div> </div>
{previewData.length > 0 && ( <div className="space-y-4"> <h3 className="text-lg font-medium">Data Preview</h3> <div className="border rounded-lg overflow-x-auto"> <table className="w-full text-sm"> <thead> <tr className="bg-muted"> {columns.map((column) => ( <th key={column} className="px-4 py-2 text-left font-medium"> {column} </th> ))} </tr> </thead> <tbody> {previewData.map((row, index) => ( <tr key={index} className="border-t"> {columns.map((column) => ( <td key={column} className="px-4 py-2"> {row[column]?.toString() || ""} </td> ))} </tr> ))} </tbody> </table> </div> <p className="text-xs text-muted-foreground">Showing first 5 rows of data</p> </div> )} </div> )}