index.tsx 2.53 KB
Newer Older
Lucas Simon Susin's avatar
Lucas Simon Susin committed
1
2
3
4
/* eslint-disable @typescript-eslint/no-explicit-any */
import { useCallback, useEffect } from 'react';
import { saveAs } from 'file-saver';
import Papa from 'papaparse';
Giovanni Gonçalves Migon's avatar
Giovanni Gonçalves Migon committed
5
6
7
import Container from '../../components/Container';
import { InputHeader } from '../../components/InputHeader/Index';
import { InputText } from '../../components/InputText/Index';
Lucas Simon Susin's avatar
Lucas Simon Susin committed
8
9
10
11
import { useGet } from '../../hooks/useGet';
import { CsvDownloadFilesDTO } from '../../types/CsvDownloadFilesDTO';
import Spinner from '../../components/Spinner';
import CsvImage from '../../assets/images/csv.png';
Giovanni Gonçalves Migon's avatar
Giovanni Gonçalves Migon committed
12
13

function History() {
Lucas Simon Susin's avatar
Lucas Simon Susin committed
14
15
16
	const { data: info, request: getInfo, isLoading } = useGet<CsvDownloadFilesDTO[]>();

	useEffect(() => {
Bruno Chanan's avatar
Bruno Chanan committed
17
		getInfo(`/sprint`);
Lucas Simon Susin's avatar
Lucas Simon Susin committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
	}, []);

	const CsvContainer = useCallback((item: any) => {
		const handleParseCSV = () => {
			const csvData: any[] | Papa.UnparseObject<any> = [];

			item.stories.forEach((item: any) => {
				const row = {
					Story: [item.name],
					SumOfTasksVotes: item.final_points,
					Tasks: item.tasks.map((task: any) => task.name),
					TasksVotes: item.tasks.map((task: any) => task.points),
				};

				csvData.push(row);
			});

			const csv = Papa.unparse(csvData, { delimiter: ';' });
			return csv;
		};

		const downloadFile = () => {
			const csv = handleParseCSV();
			const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' });
			saveAs(blob, `Squad_-_${item.name}.csv`);
		};

		return (
Bruno Chanan's avatar
Bruno Chanan committed
46
			<div className="max-w-sm flex p-3 rounded-lg shadow-xl cursor-pointer" onClick={downloadFile}>
Lucas Simon Susin's avatar
Lucas Simon Susin committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
				<div className="flex flex-shrink-0 items-center justify-center">
					<img src={CsvImage} alt="csv" className="h-12 w-12" />
				</div>
				<div className="ml-6 pt-1">
					<h4 className="text-xl text-gray-900">{item.name}</h4>
					<p className="text-base text-gray-600">
						Soma das histórias:{' '}
						{item?.stories?.reduce((acc: any, story: any) => acc + story?.final_points, 0)}
					</p>
				</div>
			</div>
		);
	}, []);

Giovanni Gonçalves Migon's avatar
Giovanni Gonçalves Migon committed
61
62
	return (
		<Container title="histórico">
Lucas Simon Susin's avatar
Lucas Simon Susin committed
63
64
			<InputHeader noButton>
				<InputText value="" placeholder="Arquivo" size="medium" isRequired={true} />
Giovanni Gonçalves Migon's avatar
Giovanni Gonçalves Migon committed
65
			</InputHeader>
Lucas Simon Susin's avatar
Lucas Simon Susin committed
66
67
68
69
70
			{isLoading ? (
				<div className="w-full flex items-center justify-center mt-8 h-60">
					<Spinner color="triider-primary-orange-dark" size="16" />
				</div>
			) : (
Bruno Chanan's avatar
Bruno Chanan committed
71
				<div className="w-full overflow-auto gap-14 grid grid-cols-3 items-start justify-start mt-8 h-60">
Lucas Simon Susin's avatar
Lucas Simon Susin committed
72
73
74
					{info?.map((item) => CsvContainer(item))}
				</div>
			)}
Giovanni Gonçalves Migon's avatar
Giovanni Gonçalves Migon committed
75
76
77
78
79
		</Container>
	);
}

export default History;