-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from Shuffled720/main
csv import export parser added
- Loading branch information
Showing
4 changed files
with
508 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// CSVParser.tsx | ||
"use client" | ||
import React, { useState } from 'react'; | ||
import csvParser from 'csv-parser'; | ||
|
||
function CSVParser() { | ||
|
||
const [jsonData, setJsonData] = useState<any>(null); | ||
|
||
const handleCSVUpload = async (event: React.ChangeEvent<HTMLInputElement>) => { | ||
const file = event.target.files?.[0]; | ||
|
||
if (file) { | ||
const data = await parseCSV(file); | ||
setJsonData(data); | ||
} | ||
}; | ||
|
||
|
||
const parseCSV = async (file: File) => { | ||
const results: any[] = []; | ||
const reader = new FileReader(); | ||
reader.readAsText(file); | ||
await new Promise(resolve => reader.onload = resolve); | ||
const data = reader.result as string; | ||
const parser = csvParser(); | ||
|
||
const records = data.split('\n'); | ||
const headers = records[0].split(','); | ||
|
||
for (let i = 1; i < records.length; i++) { | ||
const record = records[i].split(','); | ||
const obj: any = {}; | ||
for (let j = 0; j < headers.length; j++) { | ||
obj[headers[j]] = record[j]; | ||
} | ||
results.push(obj); | ||
} | ||
|
||
return results; | ||
}; | ||
|
||
return ( | ||
<div> | ||
<input type="file" accept=".csv" onChange={handleCSVUpload} /> | ||
{jsonData && <pre>{JSON.stringify(jsonData, null, 2)}</pre>} | ||
</div> | ||
); | ||
} | ||
|
||
export default CSVParser; |
Oops, something went wrong.
df25ce8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
tpc-frontend – ./
tpc-frontend-six.vercel.app
tpc-frontend-git-main-rojgar.vercel.app
tpc-frontend-rojgar.vercel.app