Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bg pdf/csv export button #64

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,11 @@
"@hookform/resolvers": "^3.3.0",
"@mui/material": "^5.10.11",
"@mui/x-date-pickers": "^5.0.6",
"@react-pdf/renderer": "^3.1.12",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@tinymce/tinymce-react": "^4.2.0",
"@types/file-saver": "^2.0.5",
"@types/react-i18next": "^8.1.0",
"@types/react-router": "^5.1.19",
"@types/react-router-dom": "^5.3.3",
Expand All @@ -88,6 +90,7 @@
"browser": "^0.2.6",
"date-fns": "^2.29.3",
"dotenv": "^16.0.3",
"file-saver": "^2.0.5",
"flowbite": "^1.5.3",
"formik": "^2.4.3",
"googleapis": "^126.0.1",
Expand Down
75 changes: 75 additions & 0 deletions src/components/PDFexport.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import React from "react";
import {
PDFViewer,
PDFDownloadLink,
Document,
Page,
View,
Text,
StyleSheet,
} from "@react-pdf/renderer";
import { saveAs } from "file-saver";

const styles = StyleSheet.create({
page: {
flexDirection: "row",
},
section: {
margin: 10,
padding: 2,
flexGrow: 1,
},
group: {
flexDirection: "column",
gap: 10,
},
});

const PDFExport = ({ obj1, obj2 }) => (
<PDFDownloadLink
document={
<Document>
<Page size="A4" style={styles.page}>
<View style={styles.section}>
<View style={styles.group}>
<Text>First name: {obj2.firstName}</Text>
<Text>Gender: {obj1.gender}</Text>
<Text>Address: {obj1.Address}</Text>
<Text>Phone number: {obj1.phone}</Text>
<Text>Field of study: {obj1.field_of_study}</Text>
<Text>Education level: {obj1.education_level}</Text>
<Text>Is employed: {obj1.isEmployed ? "true" : "false"}</Text>
<Text>Email: {obj2.email}</Text>
</View>
</View>
<View style={styles.section}>
<View style={styles.group}>
<Text>Province: {obj1.province}</Text>
<Text>District: {obj1.district}</Text>
<Text>Sector: {obj1.sector}</Text>
<Text>Is student: {obj1.isStudent ? "true" : "false"}</Text>
<Text>Hackerrank score: {obj1.Hackerrank_score}</Text>
<Text>English score: {obj1.english_score}</Text>
</View>
</View>
</Page>
</Document>
}
fileName={"export-doc.pdf"}
>
{({ blob, url, loading, error }) => (
<button
onClick={() => {
if (!loading && blob) {
saveAs(blob, `trainee_${obj2.firstName}.pdf`);
}
}}
disabled={loading || !blob}
>
{loading ? "Generating PDF..." : "Export to PDF"}
</button>
)}
</PDFDownloadLink>
);

export default PDFExport;
151 changes: 151 additions & 0 deletions src/helpers/exportTocsv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
export const dataTocsv = (
obj1: {
_id: string;
gender: string;
Address: string;
birth_date: string;
phone: string;
field_of_study: string;
education_level: string;
province: string;
district: string;
sector: string;
isEmployed: boolean;
haveLaptop: boolean;
isStudent: boolean;
Hackerrank_score: any;
english_score: any;
interview_decision: any;
past_andela_programs: any;
},
obj2: {
lastName: string;
firstName: string;
_id: string;
email: string;
}
) => {
let allObjects: any = [];
let dataList: {
_id: string;
gender: string;
Address: string;
birth_date: string;
phone: string;
field_of_study: string;
education_level: string;
province: string;
district: string;
sector: string;
isEmployed: boolean;
haveLaptop: boolean;
isStudent: boolean;
Hackerrank_score: any;
english_score: any;
interview_decision: any;
past_andela_programs: any;
lastName: string;
firstName: string;
email: string;
}[] = [];
let traineeObj = Object.assign(obj1, obj2);

dataList.push(traineeObj);
let headers: string[] = [
"First name,last name,email,gender,Address,birth date,phone,field of study,education level,province,district,sector,isEmployed,haveLaptop,isStudent,Hackerrank score,english score,interview decision,past andela programs",
];
for (const item of dataList) {
let arr: any[] = [];
arr.push(item.firstName);
arr.push(item.lastName);
arr.push(item.email);
arr.push(item.gender);
arr.push(item.Address);
arr.push(item.birth_date);
arr.push(item.phone);
arr.push(item.field_of_study);
arr.push(item.education_level);
arr.push(item.province);
arr.push(item.district);
arr.push(item.sector);
arr.push(item.isEmployed);
arr.push(item.haveLaptop);
arr.push(item.isStudent);
arr.push(item.Hackerrank_score);
arr.push(item.english_score);
arr.push(item.interview_decision);
arr.push(item.past_andela_programs);

allObjects.push(arr);
}

let traineeCsv = dataList.reduce((acc: any, trainee) => {
const {
_id,
gender,
Address,
birth_date,
phone,
field_of_study,
education_level,
province,
district,
sector,
isEmployed,
haveLaptop,
isStudent,
Hackerrank_score,
english_score,
interview_decision,
past_andela_programs,
lastName,
firstName,
email,
} = trainee;
acc.push(
[
firstName,
lastName,
email,
gender,
Address,
birth_date,
phone,
field_of_study,
education_level,
province,
district,
sector,
isEmployed,
haveLaptop,
isStudent,
Hackerrank_score,
english_score,
interview_decision,
past_andela_programs,
].join(",")
);
return acc;
}, []);

downloadFile({
data: [...headers, ...traineeCsv].join("\n"),
fileName: `trainee_${traineeObj.firstName}`,
fileType: "text/csv",
});
};

const downloadFile = ({ data, fileName, fileType }) => {
const blob = new Blob([data], { type: fileType });

const a = document.createElement("a");
a.download = fileName;
a.href = window.URL.createObjectURL(blob);
const clickEvt = new MouseEvent("click", {
view: window,
bubbles: true,
cancelable: true,
});
a.dispatchEvent(clickEvt);
a.remove();
};
29 changes: 25 additions & 4 deletions src/pages/TrainneeDetails.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useEffect } from "react";
import React, { useState, useEffect, useRef } from "react";
import { BsEnvelope } from "react-icons/bs";
import { TiExportOutline } from "react-icons/ti";
import { FcApproval } from "react-icons/fc";
Expand All @@ -15,6 +15,10 @@ import {
updateManyScoreValues,
} from "../redux/actions/scoreValueActions";
import { toast } from "react-toastify";
import { Link } from "react-router-dom";
import { dataTocsv } from "../helpers/exportTocsv";

import PDFExport from "../components/PDFexport";

const TrainneeDetails = (props: any) => {
const params = useParams();
Expand Down Expand Up @@ -83,14 +87,19 @@ const TrainneeDetails = (props: any) => {
props.updateManyScoreValues(tsabus);
};

const divRef = useRef<HTMLDivElement>(null);

return (
<>
<Navbar />
<div className="flex items-center overflow-auto dark:bg-dark-frame-bg ">
{/* <div className="min-h-[50vh] dark:bg-dark-frame-bg w-[100%] block mt-10 md:w-[100%] md:mt-0 pl-[16rem] pt-[80px] md:pl-0"> */}
<div className="block w-[100%] pl-[16rem] h-max md:pl-0 mx-auto dark:bg-dark-frame-bg pb-10 mt-10 pt-[80px]">
{traineeDetails && (
<div className=" max-w-md bg-slate-50 dark:text-zinc-100 rounded-xl dark:bg-dark-bg shadow-md overflow-hidden md:w-[100%] mb-6 lg:flex lg:max-w-2xl mx-auto">
<div
ref={divRef}
className=" max-w-md bg-slate-50 dark:text-zinc-100 rounded-xl dark:bg-dark-bg shadow-md overflow-hidden md:w-[100%] mb-6 lg:flex lg:max-w-2xl mx-auto"
>
<div className="md:flex ">
<h2 className="top-5 m-5 font-medium md:m-3 ">
<BsFillPersonLinesFill className="float-left m-1" />
Expand Down Expand Up @@ -299,9 +308,21 @@ const TrainneeDetails = (props: any) => {
{open && (
<ul className="bg-[#1F2A37] font-light text-sm text-white m-1">
<li className="border-solid border-black border-b-2 ">
Export to PDF
{/* <Link to={"#"} onClick={}>
Export to PDF
</Link> */}
<PDFExport obj1={traineeDetails} obj2={traineeDetails.trainee_id} />
</li>
<li>
<Link
to={"#"}
onClick={() =>
dataTocsv(traineeDetails, traineeDetails.trainee_id)
}
>
Export to CSV
</Link>
</li>
<li>Export to CSV</li>
</ul>
)}
</button>
Expand Down
1 change: 0 additions & 1 deletion src/redux/actions/trainnee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export const getOneTraineeAllDetails =
});

const response = await datas.data.data.getOneTraineeAllDetails;
// console.log( response)
dispatch(creator(GET_ONE_TRAINEES_ALL_DETAILS, response));
} catch (error) {
if (error) {
Expand Down
2 changes: 1 addition & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ module.exports = () => {
"process.env": JSON.stringify(process.env),
}),
new webpack.ProvidePlugin({
process: "process/browser",
process: 'process/browser',
}),
],
};
Expand Down