Skip to content

Commit

Permalink
Merge pull request #12 from Shuffled720/main
Browse files Browse the repository at this point in the history
csv import export parser added
  • Loading branch information
harshtalati2410 authored Nov 28, 2023
2 parents a2f618a + 44a4c41 commit df25ce8
Show file tree
Hide file tree
Showing 4 changed files with 508 additions and 3 deletions.
167 changes: 165 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@
"lint": "next lint"
},
"dependencies": {
"csv-parser": "^3.0.0",
"framer-motion": "^10.16.4",
"next": "latest",
"next-auth": "^4.23.2",
"papaparse": "^5.4.1",
"pdf-lib": "^1.17.1",
"react": "latest",
"react-dom": "latest",
"react-google-button": "^0.7.2"
"react-google-button": "^0.7.2",
"xlsx": "^0.18.5"
},
"devDependencies": {
"@types/node": "latest",
"@types/papaparse": "^5.3.10",
"@types/react": "latest",
"@types/react-dom": "latest",
"autoprefixer": "latest",
Expand Down
51 changes: 51 additions & 0 deletions src/components/common/CSVParser.tsx
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;
Loading

1 comment on commit df25ce8

@vercel
Copy link

@vercel vercel bot commented on df25ce8 Nov 28, 2023

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

Please sign in to comment.