-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
90 lines (81 loc) · 2.23 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import React, { useEffect, useState } from "react";
import { useSelector, useDispatch } from "react-redux";
import MonthCard from "~/components/MonthCard";
import DefaultRootState from "~/store/@types";
import {
Container,
MonthContainer,
ActionContainer,
PlusButton,
Calender,
InputValor,
InputInstallment,
InputName,
} from "./styles";
const Main: React.FC = () => {
const dispatch = useDispatch();
const [date, setDate] = useState<string>("");
const [valor, setValor] = useState<number>(0);
const [installment, setInstallment] = useState<number>(0);
const [name, setName] = useState<string>("");
const { months, loading } = useSelector(
(state: DefaultRootState) => state.main
);
const handleDate = (event: React.FormEvent<HTMLInputElement>) => {
const currentDate: string = event.currentTarget.value;
const currentDateSplited = currentDate.split("-");
setDate(`${currentDateSplited[0]}-${currentDateSplited[1]}-01`);
};
const handleValue = (
dateToSet: string,
amount: number,
installmentToSet: number,
nameToSet: string
) => {
dispatch({
type: "SPEDING_REQUEST",
date: dateToSet,
amount,
installment: installmentToSet,
name: nameToSet,
});
};
useEffect(() => {
dispatch({ type: "BALANCE_NAVIGATION" });
}, []);
return (
<Container>
<ActionContainer>
<InputName
setValue={setName}
type="text"
placeholder="Nome da dívida"
/>
<InputValor
setValue={setValor}
type="number"
placeholder="Valor devedor"
/>
<InputInstallment
setValue={setInstallment}
type="number"
placeholder="parcelas"
/>
<Calender type="date" onChange={handleDate} />
<PlusButton onClick={() => handleValue(date, valor, installment, name)}>
{loading ? "Carregando..." : "Adicionar"}
</PlusButton>
</ActionContainer>
<MonthContainer>
{months.map((item, index) => (
<MonthCard
key={index.toString()}
month={item.name}
amount={item.amount}
/>
))}
</MonthContainer>
</Container>
);
};
export default Main;