Skip to content

Commit

Permalink
DEAR-98 add print, download csv
Browse files Browse the repository at this point in the history
  • Loading branch information
smuefsmuef committed Jul 29, 2024
1 parent 9799cff commit 0dd5ace
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 55 deletions.
47 changes: 31 additions & 16 deletions app/(main)/insights/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {} from '@radix-ui/react-select';
import Loading from '@components/Loading/Loading';
import { Button } from '@components/ui/Buttons/Button';
import { FileBarChart2, Printer } from 'lucide-react';
import convertToCSV from '@/(main)/insights/utils/downloadCSV';
import WorkkindBarChart from './components/WorkkindBarChart';
import HappinessLineChart from './components/HappinessLineChart';

Expand Down Expand Up @@ -103,11 +104,25 @@ const InsightsPage: React.FC = () => {
}
};

const downloadCSV = (): void => {
if (!happinessInsightData) return;
const csv = convertToCSV(happinessInsightData);
const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' });
const link = document.createElement('a');
const url = URL.createObjectURL(blob);
link.setAttribute('href', url);
link.setAttribute('download', 'happiness_insights.csv');
link.style.visibility = 'hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};

return (
<div>
<div className="print-content">
{user && selectedTeam && data && sprints ? (
<div className="space-y-4">
<div className="grid grid-cols-4 gap-4">
<div className="print-content space-y-4">
<div className="grid grid-cols-4 items-center gap-4">
<Select onValueChange={handleTeamChange} defaultValue={selectedTeam.name}>
<SelectTrigger className="w-[180px]">
<SelectValue placeholder="Select a Team" />
Expand Down Expand Up @@ -139,20 +154,20 @@ const InsightsPage: React.FC = () => {
</SelectContent>
</Select>

<Button size="sm" variant="outline" onClick={window.print}>
<FileBarChart2 className="mr-2 h-4 w-4" />
<span className="sr-only sm:not-sr-only sm:whitespace-nowrap">Export CSV</span>
</Button>
<Button size="sm" variant="outline" onClick={window.print}>
<Printer className="mr-2 h-4 w-4" />
<span className="sr-only sm:not-sr-only sm:whitespace-nowrap">Print</span>
</Button>
</div>
<div className="grid grid-cols-3 gap-10">
{/* <HappinessMonthlyBarChart /> */}
{/* <DaysTrackedRadialChart /> */}
{/* <WorkkindRadarChart /> */}
<div className="col-span-2 flex justify-end space-x-4">
<Button size="icon" variant="outline" onClick={downloadCSV} className="no-print">
<FileBarChart2 className="w-4" />
</Button>
<Button size="icon" variant="outline" onClick={window.print} className="no-print">
<Printer className="w-4" />
</Button>
</div>
</div>
{/* <div className="grid grid-cols-3 gap-10"> */}
{/* <HappinessMonthlyBarChart /> */}
{/* <DaysTrackedRadialChart /> */}
{/* <WorkkindRadarChart /> */}
{/* </div> */}
<div className="grid grid-cols-2 gap-10">
<div>
<HappinessLineChart happinessInsights={happinessInsightData} />
Expand Down
15 changes: 15 additions & 0 deletions app/(main)/insights/utils/downloadCSV.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { HappinessInsightsChartDTO } from '@/types/InsightsType';

const convertToCSV = (objArray: HappinessInsightsChartDTO[]): string => {
if (!Array.isArray(objArray) || objArray.length === 0) {
return '';
}

const keys = Object.keys(objArray[0]);
const header = keys.join(',');
const csvRows = objArray.map((obj) => keys.map((key) => obj[key as keyof HappinessInsightsChartDTO]).join(','));

return [header, ...csvRows].join('\r\n');
};

export default convertToCSV;
115 changes: 76 additions & 39 deletions styles/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,101 @@
@tailwind utilities;

:root {
--font-outfit: 'Outfit', sans-serif;
--font-outfit: 'Outfit', sans-serif;
}

html,
body {
height: 100%;
padding: 0;
margin: 0;
font-family: var(--font-outfit);
height: 100%;
padding: 0;
margin: 0;
font-family: var(--font-outfit);
}

@layer utilities {
.text-balance {
text-wrap: balance;
}
.text-balance {
text-wrap: balance;
}
}

@layer base {
h1 {
@apply text-2xl font-extrabold;
}
h1 {
@apply text-2xl font-extrabold;
}

h2 {
@apply text-xl font-medium;
}
h2 {
@apply text-xl font-medium;
}

h3 {
@apply text-sm font-medium;
}
h3 {
@apply text-sm font-medium;
}
}

@layer base {
:root {
--chart-1: 12 76% 61%;
--chart-2: 173 58% 39%;
--chart-3: 197 37% 24%;
--chart-4: 43 74% 66%;
--chart-5: 27 87% 67%;
}

.dark {
--chart-1: 220 70% 50%;
--chart-2: 160 60% 45%;
--chart-3: 30 80% 55%;
--chart-4: 280 65% 60%;
--chart-5: 340 75% 55%;
}
:root {
--chart-1: 12 76% 61%;
--chart-2: 173 58% 39%;
--chart-3: 197 37% 24%;
--chart-4: 43 74% 66%;
--chart-5: 27 87% 67%;
}

.dark {
--chart-1: 220 70% 50%;
--chart-2: 160 60% 45%;
--chart-3: 30 80% 55%;
--chart-4: 280 65% 60%;
--chart-5: 340 75% 55%;
}
}

.rotate-360 {
animation: rotate 0.4s linear;
animation: rotate 0.4s linear;
}

@keyframes rotate {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}

@media print {

@page {
size: landscape;
margin: 1em;
}

body {
transform: scale(0.9);
transform-origin: top left;
}

/* Hide elements that should not be printed */
.no-print {
display: none;
}

.print-container {
width: 100%;
margin: 0;
padding: 0;
}

.print-content {
page-break-inside: avoid;
margin: 0;
padding: 10px;
font-size: 1em;
}

/* Prevent page breaks within the print content */
.print-content, .print-content > div {
page-break-inside: avoid;
break-inside: avoid;
}
}

0 comments on commit 0dd5ace

Please sign in to comment.