Permalink
Browse files

Component to add expenses

  • Loading branch information...
dusty-phillips committed Aug 27, 2018
1 parent f98c15f commit b1abf4fdab8e5910ff39ae92d2e64d398b7741e5
Showing with 69 additions and 1 deletion.
  1. +2 −1 src/App.js
  2. +67 −0 src/components/AddExpense.js
@@ -4,7 +4,7 @@ import { Provider } from "react-redux"
import store from "./redux/store"
import AuthButton from "./components/AuthButton"
import CategoryList from "./components/CategoryList"
import AddExpense from "./components/AddExpense"
class App extends Component {
render() {
@@ -13,6 +13,7 @@ class App extends Component {
<div>
<AuthButton />
<CategoryList />
<AddExpense />
</div>
</Provider>
)
@@ -0,0 +1,67 @@
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import { compose } from 'redux'
import { firestoreConnect } from 'react-redux-firebase'
class AddExpense extends Component {
static propTypes = {
uid: PropTypes.string,
selectedCategory: PropTypes.string,
firestore: PropTypes.shape({
add: PropTypes.func.isRequired
}).isRequired
}
state = { description: '', cost: 0 }
addExpense() {
this.props.firestore.add(
{ collection: 'expenses' },
{
uid: this.props.uid,
description: this.state.description,
cost: this.state.cost,
category: this.props.selectedCategory
}
)
this.setState({ cost: 0, description: '' })
}
render() {
if (!this.props.uid) return null
if (!this.props.selectedCategory) return null
return (
<div>
<input
type="text"
value={this.state.description}
onChange={(evt) => this.setState({ description: evt.target.value })}
/>
<input
type="number"
value={this.state.cost}
step="0.01"
onChange={(evt) => this.setState({ cost: evt.target.value })}
/>
<button onClick={(evt) => this.addExpense()}>Add Expense</button>
</div >
)
}
}
const mapStateToProps = state => {
return {
uid: state.firebase.auth.uid,
selectedCategory: state.categories.selectedCategory
}
}
const mapDispatchToProps = {}
export default compose(
connect(mapStateToProps, mapDispatchToProps),
firestoreConnect(),
)(AddExpense)

0 comments on commit b1abf4f

Please sign in to comment.