Update reducers

This commit is contained in:
Anthony Laibe 2018-08-06 10:31:06 +01:00 committed by Pascal Precht
parent 6bc8a6d8c0
commit 96a706aeb0
No known key found for this signature in database
GPG Key ID: 0EE28D8D6FD85D7D
8 changed files with 62 additions and 22 deletions

View File

@ -22,7 +22,7 @@ export function fetchTransactions(blockFrom) {
}
export function fetchTransaction(hash) {
return axios.get(`${constants.httpEndpoint}/blockchain/transactions${hash}`);
return axios.get(`${constants.httpEndpoint}/blockchain/transactions/${hash}`);
}
export function fetchProcesses() {

View File

@ -32,7 +32,7 @@ function mapStateToProps(state, props) {
if(state.accounts.data) {
return {account: state.accounts.data.find(account => account.address === props.match.params.address)};
}
return null;
return {};
}
AccountContainer.propTypes = {

View File

@ -21,7 +21,7 @@ class BlockContainer extends Component {
return (
<React.Fragment>
<Block blocks={block} />
<Block block={block} />
<Transactions transactions={block.transactions} />
</React.Fragment>
);
@ -30,9 +30,9 @@ class BlockContainer extends Component {
function mapStateToProps(state, props) {
if(state.blocks.data) {
return {block: state.blocks.data.find(block => block.number === props.match.params.blockNumber)};
return {block: state.blocks.data.find(block => block.number.toString() === props.match.params.blockNumber)};
}
return null;
return {};
}
BlockContainer.propTypes = {

View File

@ -20,7 +20,7 @@ class TransactionContainer extends Component {
return (
<React.Fragment>
<Transaction transactions={transaction} />
<Transaction transaction={transaction} />
</React.Fragment>
);
}
@ -30,7 +30,7 @@ function mapStateToProps(state, props) {
if(state.transactions.data) {
return {transaction: state.transactions.data.find(transaction => transaction.hash === props.match.params.hash)};
}
return null;
return {};
}
TransactionContainer.propTypes = {

View File

@ -1,13 +1,23 @@
import {RECEIVE_ACCOUNTS, RECEIVE_ACCOUNTS_ERROR, RECEIVE_ACCOUNT, RECEIVE_ACCOUNT_ERROR} from "../actions";
function filterAccount(account, index, self) {
return index === self.findIndex((a) => a.address === account.address);
}
export default function accounts(state = {}, action) {
switch (action.type) {
case RECEIVE_ACCOUNTS:
return Object.assign({}, state, {data: action.accounts.data});
return {
...state, data: [...action.accounts.data, ...state.data || []]
.filter(filterAccount)
};
case RECEIVE_ACCOUNTS_ERROR:
return Object.assign({}, state, {error: true});
case RECEIVE_ACCOUNT:
return Object.assign({}, state, {data: action.account.data});
return {
...state, data: [action.account.data, ...state.data || []]
.filter(filterAccount)
};
case RECEIVE_ACCOUNT_ERROR:
return Object.assign({}, state, {error: true});
default:

View File

@ -1,15 +1,31 @@
import {RECEIVE_BLOCKS, RECEIVE_BLOCKS_ERROR} from "../actions";
import {RECEIVE_BLOCK, RECEIVE_BLOCK_ERROR, RECEIVE_BLOCKS, RECEIVE_BLOCKS_ERROR} from "../actions";
function sortBlock(a, b) {
return b.number - a.number;
}
function filterBlock(block, index, self) {
return index === self.findIndex((t) => t.number === block.number);
}
export default function blocks(state = {}, action) {
switch (action.type) {
case RECEIVE_BLOCKS:
return {
...state, data: [...state.data || [], ...action.blocks.data]
.filter((block, index, self) => index === self.findIndex((t) => t.number === block.number))
.sort((a, b) => b.number - a.number)
...state, data: [...action.blocks.data, ...state.data || []]
.filter(filterBlock)
.sort(sortBlock)
};
case RECEIVE_BLOCKS_ERROR:
return Object.assign({}, state, {error: true});
case RECEIVE_BLOCK:
return {
...state, data: [action.block.data, ...state.data || []]
.filter(filterBlock)
.sort(sortBlock)
};
case RECEIVE_BLOCK_ERROR:
return Object.assign({}, state, {error: true});
default:
return state;
}

View File

@ -1,21 +1,35 @@
import {RECEIVE_TRANSACTIONS, RECEIVE_TRANSACTIONS_ERROR} from "../actions";
import {RECEIVE_TRANSACTION, RECEIVE_TRANSACTION_ERROR, RECEIVE_TRANSACTIONS, RECEIVE_TRANSACTIONS_ERROR} from "../actions";
const BN_FACTOR = 10000;
function sortTransaction(a, b) {
return ((BN_FACTOR * b.blockNumber) + b.transactionIndex) - ((BN_FACTOR * a.blockNumber) + a.transactionIndex);
}
function filterTransaction(tx, index, self) {
return index === self.findIndex((t) => (
t.blockNumber === tx.blockNumber && t.transactionIndex === tx.transactionIndex
));
}
export default function transactions(state = {}, action) {
switch (action.type) {
case RECEIVE_TRANSACTIONS:
return {
...state, data: [...state.data || [], ...action.transactions.data]
.filter((tx, index, self) => index === self.findIndex((t) => (
t.blockNumber === tx.blockNumber && t.transactionIndex === tx.transactionIndex
)))
.sort((a, b) => (
((BN_FACTOR * b.blockNumber) + b.transactionIndex) - ((BN_FACTOR * a.blockNumber) + a.transactionIndex))
)
...state, data: [...action.transactions.data, ...state.data || []]
.filter(filterTransaction)
.sort(sortTransaction)
};
case RECEIVE_TRANSACTIONS_ERROR:
return Object.assign({}, state, {error: true});
case RECEIVE_TRANSACTION:
return {
...state, data: [action.transaction.data, ...state.data || []]
.filter(filterTransaction)
.sort(sortTransaction)
};
case RECEIVE_TRANSACTION_ERROR:
return Object.assign({}, state, {error: true});
default:
return state;
}

View File

@ -57,7 +57,7 @@ export function *watchFetchBlocks() {
export function *fetchAccount(payload) {
try {
const account = yield call(api.fetchAccounts, payload.address);
const account = yield call(api.fetchAccount, payload.address);
yield put(actions.receiveAccount(account));
} catch (e) {
yield put(actions.receiveAccountError());