Skip to content

Commit

Permalink
Connecting List to the store to get the receipts and tags
Browse files Browse the repository at this point in the history
  • Loading branch information
youssef-md committed Nov 6, 2018
1 parent 5e0780f commit fc8a8ee
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
8 changes: 4 additions & 4 deletions src/components/Receipt/ReceiptList/List.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ export class List extends React.Component {
}

getTagName = (tagId) => {
if(this.props.tags)
if(!(this.props.tags === undefined || this.props.tags.length == 0))
return this.props.tags[tagId - 1].category
else
return 'carregando...'
}

getTagColor = (tagId) => {
if(this.props.tags)
if(!(this.props.tags === undefined || this.props.tags.length == 0))
return this.props.tags[tagId - 1].color
else
return '#424242'
Expand All @@ -93,9 +93,9 @@ export class List extends React.Component {
}
export const mapStateToProps = state => {
return {
tags: state.tags
tags: state.tags,
receipts: state.receipts
}
}

export default connect(mapStateToProps)(List)

26 changes: 11 additions & 15 deletions src/components/Receipt/ReceiptList/ReceiptList.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,42 @@ import getAllTags from '../../../services/getAllTags'
const smallDevice = window.matchMedia('(max-width: 800px)').matches
export class ReceiptList extends Component {
state = {
loaded: false,
receiptsLoaded: false,
tagsLoaded: false,
}
componentDidMount() {
this.fetchReceipts()
this.fetchTags()
this.fetchReceipts()
}
render() {
return (
<div >
<Navbar/>
{ this.state.loaded && <List receipts={this.props.receipts}
onGetAllReceipts={this.fetchReceipts}
isSmall={smallDevice} /> }
{ this.state.receiptsLoaded && <List onGetAllReceipts={this.fetchReceipts}
isSmall={smallDevice}/>}
<MenuButton />
</div>
)
}

fetchReceipts = async() => {
const receipts = await getAllReceipts()
this.setState({ loaded: true })
this.props.onReceiptsAdded(receipts)
this.setState({ receiptsLoaded: true })
}

fetchTags = async() => {
const tags = await getAllTags()
this.setState({ tags })
this.props.onTagsAdded(tags)
const tags = await getAllTags()
this.props.onTagsAdded(tags)
this.setState({ tagsLoaded: true })
}
}
export const mapStateToProps = state => {
return {
receipts: state.receipts
}
}

export const mapDispatchToProps = dispatch => {
return {
onReceiptsAdded: (receipts) => dispatch({type: actionTypes.ADD_RECEIPTS, receipts: receipts}),
onTagsAdded: (tags) => dispatch({ type: actionTypes.ADD_TAGS, tags: tags })
}
}

export default connect(mapStateToProps, mapDispatchToProps)(ReceiptList)
export default connect(null, mapDispatchToProps)(ReceiptList)
2 changes: 1 addition & 1 deletion src/store/reducers/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const initialState = {
filePDF: null,
fileExtracted: null,
receipts: [],
tags: null
tags: []
}

const reducer = (state = initialState, action) => {
Expand Down

0 comments on commit fc8a8ee

Please sign in to comment.