|
1 |
| -import React from "react" |
| 1 | +import React, { useState } from "react" |
2 | 2 | import { ResponsiveBar } from "@nivo/bar"
|
| 3 | +import { FormContext, useForm } from "react-hook-form" |
3 | 4 |
|
4 |
| -import { StatisticsContainer, GraphContainer } from "./styles" |
| 5 | +import Api from "../../services/api" |
| 6 | +import { StatisticsContainer, GraphContainer, StatisticForm } from "./styles" |
5 | 7 |
|
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 | +} |
21 | 30 |
|
22 | 31 | 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 | + |
23 | 72 | return (
|
24 | 73 | <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> |
26 | 92 | <GraphContainer>
|
27 | 93 | <ResponsiveBar
|
28 |
| - data={data} |
29 |
| - keys={["reclamação", "denúncia"]} |
30 |
| - indexBy="month" |
| 94 | + data={statistics} |
| 95 | + keys={keys} |
| 96 | + indexBy="date" |
31 | 97 | margin={{ top: 50, right: 130, bottom: 50, left: 60 }}
|
32 | 98 | padding={0.3}
|
33 | 99 | colors={{ scheme: "nivo" }}
|
|
0 commit comments