Skip to content

Commit 4021b54

Browse files
committed
Implementando gráfico
1 parent 923aa41 commit 4021b54

File tree

2 files changed

+89
-21
lines changed

2 files changed

+89
-21
lines changed

src/pages/StatisticsPage/Statistics.tsx

+87-21
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,99 @@
1-
import React from "react"
1+
import React, { useState } from "react"
22
import { ResponsiveBar } from "@nivo/bar"
3+
import { FormContext, useForm } from "react-hook-form"
34

4-
import { StatisticsContainer, GraphContainer } from "./styles"
5+
import Api from "../../services/api"
6+
import { StatisticsContainer, GraphContainer, StatisticForm } from "./styles"
57

6-
const data = [
7-
{
8-
month: "janeiro",
9-
denuncia: 190,
10-
denunciaColor: "hsl(148, 70%, 50%)",
11-
sandwich: 186,
12-
sandwichColor: "hsl(52, 70%, 50%)",
13-
kebab: 70,
14-
kebabColor: "hsl(57, 70%, 50%)",
15-
fries: 93,
16-
friesColor: "hsl(339, 70%, 50%)",
17-
donut: 27,
18-
donutColor: "hsl(148, 70%, 50%)",
19-
},
20-
]
8+
interface StatisticFormData {
9+
init: string
10+
end: string
11+
}
12+
13+
interface StatisticResponse {
14+
mesAno: string
15+
sugestão: string
16+
elogio: string
17+
solicitacao: string
18+
reclamacao: string
19+
denuncia: string
20+
}
21+
22+
interface StatisticData {
23+
date: string
24+
sugestão: number
25+
elogio: number
26+
solicitacao: number
27+
reclamacao: number
28+
denuncia: number
29+
}
2130

2231
const Statistics: React.FC = () => {
32+
const [statistics, setStatistics] = useState<StatisticData[]>([])
33+
const [keys, setKeys] = useState<string[]>([])
34+
const form = useForm<StatisticFormData>()
35+
36+
async function handleFetchStatistic(data: StatisticFormData) {
37+
const keysToSave: string[] = []
38+
const response = await Api.get<StatisticResponse[]>({
39+
pathUrl: "/statistics/types",
40+
config: { params: { init: data.init, end: data.end } },
41+
})
42+
43+
if (!response) {
44+
return
45+
}
46+
47+
const formattedData = response.data.map((item) => ({
48+
date: item.mesAno.replace("#", "/"),
49+
sugestão: Number(item.sugestão),
50+
elogio: Number(item.elogio),
51+
solicitacao: Number(item.solicitacao),
52+
reclamacao: Number(item.reclamacao),
53+
denuncia: Number(item.denuncia),
54+
}))
55+
56+
setStatistics(formattedData)
57+
58+
for (const item of formattedData) {
59+
for (const key in item) {
60+
if (key === "date") {
61+
continue
62+
} else {
63+
keysToSave.push(key)
64+
}
65+
}
66+
}
67+
68+
console.log(keysToSave)
69+
setKeys(keysToSave)
70+
}
71+
2372
return (
2473
<StatisticsContainer>
25-
<h1>Estatísticas</h1>
74+
<section>
75+
<h1>Estatística</h1>
76+
<FormContext {...form}>
77+
<StatisticForm onSubmit={form.handleSubmit(handleFetchStatistic)}>
78+
<div>
79+
<div>
80+
<label htmlFor="init">Data de inicio</label>
81+
<input name="init" type="date" ref={form.register} />
82+
</div>
83+
<div>
84+
<label htmlFor="init">Data de fim</label>
85+
<input name="end" type="date" ref={form.register} />
86+
</div>
87+
</div>
88+
<button type="submit">Gerar estatística</button>
89+
</StatisticForm>
90+
</FormContext>
91+
</section>
2692
<GraphContainer>
2793
<ResponsiveBar
28-
data={data}
29-
keys={["reclamação", "denúncia"]}
30-
indexBy="month"
94+
data={statistics}
95+
keys={keys}
96+
indexBy="date"
3197
margin={{ top: 50, right: 130, bottom: 50, left: 60 }}
3298
padding={0.3}
3399
colors={{ scheme: "nivo" }}

src/pages/StatisticsPage/styles.ts

+2
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,5 @@ export const GraphContainer = styled.div`
148148
height: 500px;
149149
background: #eee;
150150
`
151+
152+
export const StatisticForm = styled(StyledForm)``

0 commit comments

Comments
 (0)