Skip to content

Commit

Permalink
feat: create modal and form
Browse files Browse the repository at this point in the history
  • Loading branch information
robsonnatanael committed Apr 6, 2022
1 parent 4d99bf9 commit 584d8a2
Show file tree
Hide file tree
Showing 10 changed files with 366 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .eslintcache
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/index.tsx":"1","/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/App.tsx":"2","/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/styles/global.ts":"3","/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/components/Header/index.tsx":"4","/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/components/Header/styles.ts":"5","/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/components/Dashboard/index.tsx":"6","/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/components/Dashboard/styles.ts":"7","/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/components/Summary/index.tsx":"8","/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/components/Summary/styles.ts":"9","/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/components/TransactionsTable/index.tsx":"10","/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/components/TransactionsTable/styles.ts":"11","/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/services/api.ts":"12"},{"size":556,"mtime":1648952246282,"results":"13","hashOfConfig":"14"},{"size":262,"mtime":1648938300862,"results":"15","hashOfConfig":"14"},{"size":909,"mtime":1648944108002,"results":"16","hashOfConfig":"14"},{"size":324,"mtime":1648779011418,"results":"17","hashOfConfig":"14"},{"size":543,"mtime":1648943023772,"results":"18","hashOfConfig":"14"},{"size":263,"mtime":1648947003732,"results":"19","hashOfConfig":"14"},{"size":145,"mtime":1648937924902,"results":"20","hashOfConfig":"14"},{"size":800,"mtime":1648943380912,"results":"21","hashOfConfig":"14"},{"size":632,"mtime":1648943356142,"results":"22","hashOfConfig":"14"},{"size":948,"mtime":1648953717462,"results":"23","hashOfConfig":"14"},{"size":638,"mtime":1648948051382,"results":"24","hashOfConfig":"14"},{"size":106,"mtime":1648953563212,"results":"25","hashOfConfig":"14"},{"filePath":"26","messages":"27","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"16wifw6",{"filePath":"28","messages":"29","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"30","messages":"31","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"32"},{"filePath":"33","messages":"34","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"32"},{"filePath":"35","messages":"36","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"32"},{"filePath":"37","messages":"38","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"32"},{"filePath":"39","messages":"40","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"32"},{"filePath":"41","messages":"42","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"32"},{"filePath":"43","messages":"44","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"32"},{"filePath":"45","messages":"46","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"47","messages":"48","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"32"},{"filePath":"49","messages":"50","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/index.tsx",[],"/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/App.tsx",[],"/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/styles/global.ts",[],["51","52"],"/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/components/Header/index.tsx",[],"/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/components/Header/styles.ts",[],"/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/components/Dashboard/index.tsx",[],"/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/components/Dashboard/styles.ts",[],"/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/components/Summary/index.tsx",[],"/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/components/Summary/styles.ts",[],"/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/components/TransactionsTable/index.tsx",[],"/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/components/TransactionsTable/styles.ts",[],"/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/services/api.ts",[],{"ruleId":"53","replacedBy":"54"},{"ruleId":"55","replacedBy":"56"},"no-native-reassign",["57"],"no-negated-in-lhs",["58"],"no-global-assign","no-unsafe-negation"]
[{"/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/index.tsx":"1","/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/App.tsx":"2","/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/styles/global.ts":"3","/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/components/Header/index.tsx":"4","/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/components/Header/styles.ts":"5","/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/components/Dashboard/index.tsx":"6","/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/components/Dashboard/styles.ts":"7","/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/components/Summary/index.tsx":"8","/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/components/Summary/styles.ts":"9","/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/components/TransactionsTable/index.tsx":"10","/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/components/TransactionsTable/styles.ts":"11","/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/services/api.ts":"12","/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/components/NewTransactionModal/index.tsx":"13","/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/components/NewTransactionModal/styles.ts":"14"},{"size":1108,"mtime":1649204161600,"results":"15","hashOfConfig":"16"},{"size":894,"mtime":1649123706128,"results":"17","hashOfConfig":"16"},{"size":1511,"mtime":1649126788408,"results":"18","hashOfConfig":"16"},{"size":470,"mtime":1649013876991,"results":"19","hashOfConfig":"16"},{"size":543,"mtime":1648943023772,"results":"20","hashOfConfig":"16"},{"size":263,"mtime":1648947003732,"results":"21","hashOfConfig":"16"},{"size":145,"mtime":1648937924902,"results":"22","hashOfConfig":"16"},{"size":800,"mtime":1648943380912,"results":"23","hashOfConfig":"16"},{"size":632,"mtime":1648943356142,"results":"24","hashOfConfig":"16"},{"size":1393,"mtime":1649204195950,"results":"25","hashOfConfig":"16"},{"size":638,"mtime":1648948051382,"results":"26","hashOfConfig":"16"},{"size":106,"mtime":1648953563212,"results":"27","hashOfConfig":"16"},{"size":2582,"mtime":1649199540280,"results":"28","hashOfConfig":"16"},{"size":1719,"mtime":1649198533310,"results":"29","hashOfConfig":"16"},{"filePath":"30","messages":"31","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"16wifw6",{"filePath":"32","messages":"33","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"34"},{"filePath":"35","messages":"36","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"34"},{"filePath":"37","messages":"38","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"34"},{"filePath":"39","messages":"40","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"34"},{"filePath":"41","messages":"42","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"34"},{"filePath":"43","messages":"44","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"34"},{"filePath":"45","messages":"46","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"34"},{"filePath":"47","messages":"48","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"34"},{"filePath":"49","messages":"50","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"51","messages":"52","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"34"},{"filePath":"53","messages":"54","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"34"},{"filePath":"55","messages":"56","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"34"},{"filePath":"57","messages":"58","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"34"},"/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/index.tsx",[],"/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/App.tsx",[],["59","60"],"/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/styles/global.ts",[],"/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/components/Header/index.tsx",[],"/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/components/Header/styles.ts",[],"/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/components/Dashboard/index.tsx",[],"/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/components/Dashboard/styles.ts",[],"/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/components/Summary/index.tsx",[],"/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/components/Summary/styles.ts",[],"/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/components/TransactionsTable/index.tsx",[],"/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/components/TransactionsTable/styles.ts",[],"/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/services/api.ts",[],"/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/components/NewTransactionModal/index.tsx",[],"/home/robsonnatanael/develop/ignite/chapter-II/dtmoney/src/components/NewTransactionModal/styles.ts",[],{"ruleId":"61","replacedBy":"62"},{"ruleId":"63","replacedBy":"64"},"no-native-reassign",["65"],"no-negated-in-lhs",["66"],"no-global-assign","no-unsafe-negation"]
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
"dependencies": {
"axios": "^0.26.1",
"miragejs": "^0.1.43",
"polished": "^4.2.1",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-modal": "^3.14.4",
"react-scripts": "4.0.1",
"styled-components": "^5.3.5",
"web-vitals": "^0.2.4"
Expand All @@ -19,6 +21,7 @@
"@types/node": "^12.0.0",
"@types/react": "^16.9.53",
"@types/react-dom": "^16.9.8",
"@types/react-modal": "^3.13.1",
"@types/styled-components": "^5.1.24",
"typescript": "^4.0.3"
},
Expand Down
22 changes: 21 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
import { useState } from 'react';
import Modal from 'react-modal';
import { Dashboard } from "./components/Dashboard";
import { Header } from "./components/Header";
import { NewTransactionModal } from './components/NewTransactionModal';

import { GlobalStyle } from "./styles/global";

Modal.setAppElement('#root');

export function App() {
const [isNewTransactionModalOpen, setIsNewTransactionModalOpen] = useState(false);

function handleOpenNewTransactionModal() {
setIsNewTransactionModalOpen(true);
}

function handleCloseNewTransactionModal() {
setIsNewTransactionModalOpen(false);
}

return (
<>
<Header />
<Header onOpenNewTransactionModal={handleOpenNewTransactionModal} />
<Dashboard />
<NewTransactionModal
isOpen={isNewTransactionModalOpen}
onRequestClose={handleCloseNewTransactionModal}
/>
<GlobalStyle />
</>
);
Expand Down
8 changes: 6 additions & 2 deletions src/components/Header/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import logoImg from '../../assets/logo.svg';
import { Container, Content } from './styles';

export function Header() {
interface HeaderProps {
onOpenNewTransactionModal: () => void;
}

export function Header({ onOpenNewTransactionModal }: HeaderProps) {
return (
<Container>
<Content>
<img src={logoImg} alt="dt money" />
<button type="button">
<button type="button" onClick={onOpenNewTransactionModal}>
Nova transação
</button>
</Content>
Expand Down
98 changes: 98 additions & 0 deletions src/components/NewTransactionModal/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { FormEvent, useState } from 'react';
import Modal from 'react-modal';
import closeImg from '../../assets/close.svg';
import incomeImg from '../../assets/income.svg';
import outcomeImg from '../../assets/outcome.svg';
import { api } from '../../services/api';
import { Container, TransactionTypeContainer, RadioBox } from './styles';

interface NewTransactionModalProps {
isOpen: boolean;
onRequestClose: () => void;
}

export function NewTransactionModal({ isOpen, onRequestClose }: NewTransactionModalProps) {
const [title, setTitle] = useState('');
const [value, setValue] = useState(0);
const [category, setCategory] = useState('');
const [type, setType] = useState('deposit');

function handleCreateNewTransaction(event: FormEvent) {
event.preventDefault();

const data = {
title,
value,
category,
type
}

api.post('/transactions', data);
}

return (
<Modal
isOpen={isOpen}
onRequestClose={onRequestClose}
overlayClassName="react-modal-overlay"
className="react-modal-content"
>
<button
type='button'
onClick={onRequestClose}
className="react-modal-close"
>
<img src={closeImg} alt="Fechar modal" />
</button>

<Container onSubmit={handleCreateNewTransaction}>
<h2>Cadastrar transação</h2>

<input
placeholder='Título'
value={title}
onChange={event => setTitle(event.target.value)}
/>

<input
type="number"
placeholder='Valor'
value={value}
onChange={event => setValue(Number(event.target.value))}
/>

<TransactionTypeContainer>
<RadioBox
type="button"
onClick={() => { setType('deposit'); }}
isActive={type === 'deposit'}
activeColor="green"
>
<img src={incomeImg} alt="Entrada" />
<span>Entrada</span>
</RadioBox>

<RadioBox
type="button"
onClick={() => { setType('withdraw'); }}
isActive={type === 'withdraw'}
activeColor="red"
>
<img src={outcomeImg} alt="Saída" />
<span>Saída</span>
</RadioBox>
</TransactionTypeContainer>

<input
placeholder='Categoria'
value={category}
onChange={event => setCategory(event.target.value)}
/>

<button type='submit'>
Cadastrar
</button>
</Container>
</Modal >
);
}
96 changes: 96 additions & 0 deletions src/components/NewTransactionModal/styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import styled from "styled-components";
import { darken, transparentize } from "polished";

export const Container = styled.form`
h2 {
color: var(--text-title);
font-size: 1.5rem;
margin-bottom: 2rem;
}
input {
width: 100%;
padding: 0 1.5rem;
height: 4rem;
border-radius: 0.25rem;
border: 1px solid #d7d7d7;
background: #e7e9ee;
font-weight: 400;
font-size: 1rem;
&::placeholder {
color: var(--text-body);
}
& + input {
margin-top: 1rem;
}
}
button[type="submit"] {
width: 100%;
padding: 0 1.5rem;
height: 4rem;
background: var(--green);
color: #fff;
border-radius: 0.25rem;
border: 0;
font-size: 1rem;
font-weight: 600;
margin-top: 1.5rem;
transition: filter 0.2s;
&:hover {
filter: brightness(0.9);
}
}
`;

export const TransactionTypeContainer = styled.div`
margin: 1rem 0px;
display: grid;
grid-template-columns: 1fr 1fr;
gap: 0.5rem;
`;

interface RadioBoxProps {
isActive: boolean;
activeColor: "green" | "red";
}

const colors = {
green: "#33cc95",
red: "#e52e4d",
};

export const RadioBox = styled.button<RadioBoxProps>`
height: 4rem;
border: 1px solid #d7d7d7;
border-radius: 0.25rem;
background: ${(props) =>
props.isActive
? transparentize(0.9, colors[props.activeColor])
: "transparent"};
display: flex;
align-items: center;
justify-content: center;
transition: border-color 0.2s;
&:hover {
border-color: ${darken(0.1, "#d7d7d7")};
}
img {
width: 20px;
height: 20px;
}
span {
display: inline-block;
margin-left: 1rem;
font-size: 1rem;
color: var(--text-title);
}
`;
44 changes: 30 additions & 14 deletions src/components/TransactionsTable/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import { useEffect } from "react";
import { useEffect, useState } from "react";
import { api } from "../../services/api";
import { Container } from "./styles";

interface Transaction {
id: number;
title: string;
amount: number;
type: string;
category: string;
createdAt: string;
}

export function TransactionsTable() {
const [transactions, setTransactions] = useState<Transaction[]>([]);

useEffect(() => {
api.get('transactions')
.then(response => console.log(response.data))
.then(response => setTransactions(response.data.transactions));
}, [])

return (
Expand All @@ -20,18 +31,23 @@ export function TransactionsTable() {
</tr>
</thead>
<tbody>
<tr>
<td>Desenvolvimento de website</td>
<td className="deposit">R$12.000</td>
<td>Desenvolvimento</td>
<td>02/04/2022</td>
</tr>
<tr>
<td>Financiamento Imobiliário</td>
<td className="withdraw">- R$700</td>
<td>Financiamento</td>
<td>05/04/2022</td>
</tr>
{transactions.map(transaction => (
<tr key={transaction.id}>
<td>{transaction.title}</td>
<td className={transaction.type}>
{new Intl.NumberFormat('pt-BR', {
style: 'currency',
currency: 'BRL'
}).format(transaction.amount)}
</td>
<td>{transaction.category}</td>
<td>
{new Intl.DateTimeFormat('pt-BR').format(
new Date(transaction.createdAt)
)}
</td>
</tr>
))}
</tbody>
</table>
</Container>
Expand Down
Loading

0 comments on commit 584d8a2

Please sign in to comment.