Skip to content

Commit

Permalink
Merge pull request #27 from moutinhofuturedev/feat/add-axios
Browse files Browse the repository at this point in the history
Feat/add axios
  • Loading branch information
moutinhofuturedev authored Oct 22, 2023
2 parents 40053a1 + 996f91e commit 252b56c
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 56 deletions.
41 changes: 32 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@hookform/resolvers": "^3.3.2",
"@radix-ui/react-dialog": "^1.0.4",
"@radix-ui/react-radio-group": "^1.1.3",
"axios": "^1.5.1",
"lucide-react": "^0.258.0",
"polished": "^4.2.2",
"react": "^18.2.0",
Expand Down
2 changes: 1 addition & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ThemeProvider } from 'styled-components'
import { defaultTheme } from './styles/theme/default'
import { GlobalStyle } from './styles/global'
import { Transactions } from './pages/Transactions'
import { TransactionsProvider } from './contexts/TransactionContext'
import { TransactionsProvider } from './context/TransactionContext'

const App = () => {
return (
Expand Down
2 changes: 1 addition & 1 deletion src/components/SearchForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useContext } from 'react'
import { useForm } from 'react-hook-form'
import { zodResolver } from '@hookform/resolvers/zod'
import * as z from 'zod'
import { TransactionsContext } from '../../contexts/TransactionContext'
import { TransactionsContext } from '../../context/TransactionContext'

const searchFormSchema = z.object({
query: z.string(),
Expand Down
2 changes: 1 addition & 1 deletion src/components/SearchForm/searchForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { fireEvent, render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'

import { SearchForm } from '.'
import { TransactionsContext } from '../../contexts/TransactionContext'
import { TransactionsContext } from '../../context/TransactionContext'

describe('SearchForm', () => {
it('renders SearchForm component', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ReactNode, createContext, useEffect, useState } from 'react'
import { api } from '../lib/api'

export interface TransactionType {
id: number
Expand All @@ -24,16 +25,13 @@ export const TransactionsProvider = ({
const [transactions, setTransactions] = useState<TransactionType[]>([])

const fetchTransactions = async (query?: string) => {
const url = new URL('http://localhost:3000/transactions')
const response = await api.get('/transactions', {
params: {
q: query,
},
})

if (query) {
url.searchParams.append('q', query)
}

const response: Response = await fetch(url)
const data: TransactionType[] = await response.json()

setTransactions(data)
setTransactions(response.data)
}

useEffect(() => {
Expand Down
File renamed without changes.
40 changes: 20 additions & 20 deletions src/hooks/useSummary.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import { useContext } from 'react'
import { TransactionsContext } from '../contexts/TransactionContext'
import { TransactionsContext } from '../context/TransactionContext'

export const useSummary = () => {
const { transactions } = useContext(TransactionsContext)

const summary =
transactions &&
transactions.reduce(
(accumulator, transaction) => {
if (transaction.expense === 'income') {
accumulator.income += transaction.price
accumulator.total += transaction.price
} else {
accumulator.outcome += transaction.price
accumulator.total -= transaction.price
}
const summary = transactions
? transactions.reduce(
(accumulator, transaction) => {
if (transaction.expense === 'income') {
accumulator.income += transaction.price
accumulator.total += transaction.price
} else {
accumulator.outcome += transaction.price
accumulator.total -= transaction.price
}

return accumulator
},
{
income: 0,
outcome: 0,
total: 0,
},
)
return accumulator
},
{
income: 0,
outcome: 0,
total: 0,
},
)
: null

return summary
}
5 changes: 5 additions & 0 deletions src/lib/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import axios from 'axios'

export const api = axios.create({
baseURL: 'http://localhost:3000',
})
29 changes: 15 additions & 14 deletions src/pages/Transactions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Header } from '../../components/Header'
import { SearchForm } from '../../components/SearchForm'
import { Summary } from '../../components/Summary'
import { Transaction } from '../../components/Transaction'
import { TransactionsContext } from '../../contexts/TransactionContext'
import { TransactionsContext } from '../../context/TransactionContext'

export const Transactions = () => {
const { transactions } = useContext(TransactionsContext)
Expand All @@ -13,19 +13,20 @@ export const Transactions = () => {
<Header title="DT Money" name="Nova transação" />
<Summary />
<SearchForm />
{transactions &&
transactions.map((transaction) => {
return (
<Transaction
key={transaction.id}
tableExpense={transaction.expense}
tableDescription={transaction.description}
tablePrice={transaction.price}
tableCategory={transaction.category}
tableDate={transaction.createdAt}
/>
)
})}
{transactions
? transactions.map((transaction) => {
return (
<Transaction
key={transaction.id}
tableExpense={transaction.expense}
tableDescription={transaction.description}
tablePrice={transaction.price}
tableCategory={transaction.category}
tableDate={transaction.createdAt}
/>
)
})
: null}
</>
)
}
2 changes: 1 addition & 1 deletion src/pages/Transactions/transactions.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Transactions } from '.'
import {
TransactionsContext,
TransactionType,
} from '../../contexts/TransactionContext'
} from '../../context/TransactionContext'

const mockTransactions: TransactionType[] = [
{
Expand Down

0 comments on commit 252b56c

Please sign in to comment.