Jessedbrown commited on
Commit
d4fe36e
·
1 Parent(s): 5cafcc5

feat: add transaction modal

Browse files
web/src/components/Modal/index.js ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import React from 'react';
2
+ import {
3
+ ModalContent,
4
+ ModalFooter,
5
+ ModalHeader,
6
+ ModalBody,
7
+ ModalOverlay,
8
+ } from './style';
9
+ import { useForm } from '../../hooks/useForm';
10
+ import MaskedInput from 'react-text-mask';
11
+ import createNumberMask from 'text-mask-addons/dist/createNumberMask';
12
+ import { handleTransaction } from '../../utils/api';
13
+ import { replaceCommaWithDot } from '../../utils/informationFormatters';
14
+
15
+ const defaultMaskOptions = {
16
+ prefix: 'R$',
17
+ suffix: '',
18
+ includeThousandsSeparator: true,
19
+ thousandsSeparatorSymbol: '.',
20
+ allowDecimal: true,
21
+ decimalSymbol: ',',
22
+ decimalLimit: 2,
23
+ integerLimit: 7,
24
+ allowNegative: false,
25
+ allowLeadingZeroes: false,
26
+ };
27
+
28
+ const CurrencyInput = ({ maskOptions, ...inputProps }) => {
29
+ const currencyMask = createNumberMask({
30
+ ...defaultMaskOptions,
31
+ ...maskOptions,
32
+ });
33
+
34
+ return <MaskedInput mask={currencyMask} {...inputProps} />;
35
+ };
36
+
37
+ CurrencyInput.defaultProps = {
38
+ inputMode: 'numeric',
39
+ maskOptions: {},
40
+ };
41
+
42
+ function Modal(props) {
43
+ const [form, handleFormChange, resetForm] = useForm({
44
+ amount: undefined,
45
+ category: null,
46
+ });
47
+
48
+ if (!props.show) {
49
+ return null;
50
+ }
51
+
52
+ const handleTransactionFormSubmit = async (event) => {
53
+ event.preventDefault();
54
+
55
+ const formattedAmount = replaceCommaWithDot(form.amount).replace('R$', '');
56
+ const transaction = {
57
+ amount: +formattedAmount,
58
+ category: form.category,
59
+ user_id: props.userId,
60
+ };
61
+
62
+ await handleTransaction(transaction);
63
+ window.location.reload(false);
64
+ };
65
+
66
+ return (
67
+ <ModalOverlay onClick={props.onClose}>
68
+ <ModalContent onClick={(e) => e.stopPropagation()}>
69
+ <ModalHeader>
70
+ <h3>Realizar transação</h3>
71
+ </ModalHeader>
72
+ <ModalBody>
73
+ <form onSubmit={handleTransactionFormSubmit}>
74
+ <label>Valor da transação: </label>
75
+ <CurrencyInput
76
+ onChange={handleFormChange}
77
+ value={form.amount}
78
+ required
79
+ name="amount"
80
+ />
81
+ <label htmlFor="category">Tipo de transação</label>
82
+ <select name="category" onChange={handleFormChange} required>
83
+ <option value={null}></option>
84
+ <option value="withdrawl">Retirada</option>
85
+ <option value="payment">Pagamento</option>
86
+ <option value="deposit">Depósito</option>
87
+ </select>
88
+ <button type="submit">Salvar transação</button>
89
+ </form>
90
+ </ModalBody>
91
+ <ModalFooter>
92
+ <button onClick={props.onClose}>Fechar</button>
93
+ </ModalFooter>
94
+ </ModalContent>
95
+ </ModalOverlay>
96
+ );
97
+ }
98
+
99
+ export default Modal;
web/src/components/Modal/style.js ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import styled from 'styled-components';
2
+
3
+ export const Overlay = styled.div``;
4
+
5
+ export const ModalContent = styled.div`
6
+ width: 500px;
7
+ background-color: var(--white);
8
+ z-index: 4;
9
+ `;
10
+
11
+ export const ModalHeader = styled.div`
12
+ padding: 10px;
13
+
14
+ h3 {
15
+ margin: 0;
16
+ padding: 10px 0;
17
+ color: var(--pink);
18
+ }
19
+ `;
20
+
21
+ export const ModalBody = styled.div`
22
+ padding: 25px;
23
+ border-top: 1px solid #eee;
24
+ border-bottom: 1px solid #eee;
25
+
26
+ form {
27
+ display: flex;
28
+ flex-direction: column;
29
+ label {
30
+ padding: 10px;
31
+ font-size: 14px;
32
+ }
33
+ input {
34
+ padding: 10px;
35
+ font-size: 18px;
36
+ }
37
+ button {
38
+ margin-top: 15px;
39
+ font-size: 18px;
40
+ text-transform: uppercase;
41
+ border-radius: 100px;
42
+ line-height: 24px;
43
+ background-color: var(--pink);
44
+ color: var(--white);
45
+ cursor: pointer;
46
+ border: none;
47
+ padding: 8px 12px;
48
+ letter-spacing: 0;
49
+ font-weight: 600;
50
+ width: auto;
51
+ }
52
+ }
53
+ `;
54
+
55
+ export const ModalFooter = styled.div`
56
+ padding: 10px;
57
+ `;
58
+
59
+ export const ModalOverlay = styled.div`
60
+ position: fixed;
61
+ left: 0;
62
+ right: 0;
63
+ bottom: 0;
64
+ top: 0;
65
+ background-color: rgba(0, 0, 0, 0.5);
66
+ display: flex;
67
+ align-items: center;
68
+ justify-content: center;
69
+ z-index: 2;
70
+ `;