sipp11
9 years ago
11 changed files with 185 additions and 21 deletions
@ -0,0 +1,13 @@
|
||||
import AppDispatcher from '../dispatchers/AppDispatcher.js'; |
||||
import {TX_GET} from '../constants/TxConstants.js'; |
||||
|
||||
export default { |
||||
|
||||
gotTxs: (txs) => { |
||||
AppDispatcher.dispatch({ |
||||
actionType: TX_GET, |
||||
txs: txs |
||||
}) |
||||
} |
||||
|
||||
} |
@ -0,0 +1,74 @@
|
||||
import React from 'react'; |
||||
import AuthenticatedComponent from './AuthenticatedComponent'; |
||||
import AccountStore from '../stores/AccountStore'; |
||||
import AccountService from '../services/AccountService'; |
||||
import TxStore from '../stores/TxStore'; |
||||
import TxService from '../services/TxService'; |
||||
|
||||
/* |
||||
List of all Tx related to user
|
||||
*/ |
||||
|
||||
class Tx extends React.Component { |
||||
|
||||
constructor(props) { |
||||
super(props); |
||||
this.state = this.getTxState(); |
||||
this._onChange = this._onChange.bind(this); |
||||
} |
||||
|
||||
_onChange() { |
||||
this.setState(this.getTxState()); |
||||
} |
||||
|
||||
componentDidMount() { |
||||
if (!this.state.account) { |
||||
this.fetchAccount(this.props.params.accountId); |
||||
this.fetchTxs(this.props.params.accountId); |
||||
} else if ( this.state.account.id !== +this.props.params.accountId ) { |
||||
this.fetchAccount(this.props.params.accountId); |
||||
this.fetchTxs(this.props.params.accountId); |
||||
} |
||||
TxStore.addChangeListener(this._onChange); |
||||
AccountStore.addChangeListener(this._onChange); |
||||
} |
||||
|
||||
componentWillUnmount() { |
||||
AccountStore.removeChangeListener(this._onChange); |
||||
TxStore.removeChangeListener(this._onChange); |
||||
} |
||||
|
||||
fetchAccount(accountId) { |
||||
AccountService.getAccount(accountId); |
||||
} |
||||
|
||||
fetchTxs(accountId) { |
||||
TxService.getTxs(accountId); |
||||
} |
||||
|
||||
getTxState() { |
||||
return { |
||||
account: AccountStore.account, |
||||
txs: TxStore.txs |
||||
} |
||||
} |
||||
|
||||
renderTx(tx) { |
||||
return ( |
||||
<li>{tx.date} <b>{tx.amount} {tx.currency}</b></li> |
||||
) |
||||
} |
||||
|
||||
render() { |
||||
return ( |
||||
<div className="grid-block"> |
||||
<h1>{this.state.account.title}</h1> |
||||
<ul> |
||||
{this.state.txs.map(this.renderTx)} |
||||
</ul> |
||||
</div> |
||||
) |
||||
} |
||||
} |
||||
|
||||
export default AuthenticatedComponent(Tx); |
@ -0,0 +1,7 @@
|
||||
var BASE_URL = 'http://rocket.dev/'; |
||||
export default { |
||||
BASE_URL: BASE_URL, |
||||
TX_URL_SUFFIX: '/txs', |
||||
ACCOUNT_GET: 'ACCOUNT_GET', |
||||
TX_GET: 'TX_GET', |
||||
} |
@ -0,0 +1,25 @@
|
||||
import request from 'reqwest'; |
||||
import when from 'when'; |
||||
import {ACCOUNT_URL} from '../constants/AccountConstants'; |
||||
import {TX_URL_SUFFIX} from '../constants/TxConstants'; |
||||
import TxActions from '../actions/TxActions'; |
||||
import LoginStore from '../stores/LoginStore'; |
||||
|
||||
class AccountService { |
||||
|
||||
getTxs(accountId) { |
||||
request({ |
||||
url: ACCOUNT_URL + accountId + TX_URL_SUFFIX + '/', |
||||
method: 'GET', |
||||
headers: { |
||||
'Authorization': 'Bearer ' + LoginStore.jwt |
||||
} |
||||
}) |
||||
.then(function(response) { |
||||
TxActions.gotTxs(response.results); |
||||
}); |
||||
} |
||||
|
||||
} |
||||
|
||||
export default new AccountService() |
@ -0,0 +1,44 @@
|
||||
import {ACCOUNT_GET} from '../constants/AccountConstants'; |
||||
import {TX_GET} from '../constants/TxConstants'; |
||||
import {LOGOUT_USER} from '../constants/LoginConstants'; |
||||
import BaseStore from './BaseStore'; |
||||
|
||||
class TxStore extends BaseStore { |
||||
|
||||
constructor() { |
||||
super(); |
||||
this.subscribe(() => this._registerToActions.bind(this)) |
||||
this._account = null; |
||||
this._txs = []; |
||||
} |
||||
|
||||
_registerToActions(action) { |
||||
switch(action.actionType) { |
||||
case ACCOUNT_GET: |
||||
this._account = action.account; |
||||
this.emitChange(); |
||||
break; |
||||
case TX_GET: |
||||
this._txs = action.txs; |
||||
this.emitChange(); |
||||
break; |
||||
case LOGOUT_USER: |
||||
this._account = null; |
||||
this._txs = []; |
||||
this.emitChange(); |
||||
break; |
||||
default: |
||||
break; |
||||
}; |
||||
} |
||||
|
||||
get txs() { |
||||
return this._txs; |
||||
} |
||||
|
||||
get account() { |
||||
return this._account; |
||||
} |
||||
} |
||||
|
||||
export default new TxStore(); |
Loading…
Reference in new issue