embark-area-51/embark-ui/src/reducers/index.js

104 lines
2.9 KiB
JavaScript
Raw Normal View History

2018-08-01 18:09:58 +00:00
import {combineReducers} from 'redux';
2018-08-07 14:09:55 +00:00
import {REQUEST} from "../actions";
2018-08-07 08:40:20 +00:00
import fiddleRecuder from './fiddleReducer';
2018-08-07 14:09:55 +00:00
const BN_FACTOR = 10000;
const entitiesDefaultState = {
accounts: [],
blocks: [],
transactions: [],
processes: [],
processLogs: [],
contracts: [],
contractProfiles: [],
2018-08-09 19:33:07 +00:00
contractLogs: [],
2018-08-08 19:03:46 +00:00
commands: [],
messages: [],
messageChannels: [],
2018-08-08 19:03:46 +00:00
messageVersion: null
2018-08-07 14:09:55 +00:00
};
const sorter = {
blocks: function(a, b) {
return b.number - a.number;
},
transactions: function(a, b) {
return ((BN_FACTOR * b.blockNumber) + b.transactionIndex) - ((BN_FACTOR * a.blockNumber) + a.transactionIndex);
},
processLogs: function(a, b) {
return a.timestamp - b.timestamp;
2018-08-08 19:03:46 +00:00
},
2018-08-09 19:33:07 +00:00
contractLogs: function(a, b) {
return a.timestamp - b.timestamp;
},
2018-08-08 19:03:46 +00:00
messages: function(a, b) {
return a.time - b.time;
2018-08-07 14:09:55 +00:00
}
};
const filtrer = {
processes: function(process, index, self) {
return index === self.findIndex((t) => t.name === process.name);
},
contracts: function(contract, index, self) {
return index === self.findIndex((t) => t.className === contract.className);
2018-08-07 14:09:55 +00:00
},
accounts: function(account, index, self) {
return index === self.findIndex((t) => t.address === account.address);
},
blocks: function(block, index, self) {
return index === self.findIndex((t) => t.number === block.number);
},
transactions: function(tx, index, self) {
return index === self.findIndex((t) => (
t.blockNumber === tx.blockNumber && t.transactionIndex === tx.transactionIndex
));
}
};
function entities(state = entitiesDefaultState, action) {
for (let name of Object.keys(state)) {
let filter = filtrer[name] || (() => true);
let sort = sorter[name] || (() => true);
2018-08-08 19:03:46 +00:00
if (action[name] && !Array.isArray(action[name])) {
return {...state, [name]: action[name]};
}
if (action[name] && (!Array.isArray(action[name]) || action[name].length > 1)) {
2018-08-07 14:09:55 +00:00
return {...state, [name]: [...action[name], ...state[name]].filter(filter).sort(sort)};
}
if (action[name] && action[name].length === 1) {
let entity = action[name][0];
let nested = Object.keys(state).reduce((acc, entityName) => {
if (entity[entityName] && entity[entityName].length > 0) {
let entityFilter = filtrer[entityName] || (() => true);
let entitySort = sorter[entityName] || (() => true);
acc[entityName] = [...entity[entityName], ...state[entityName]].filter(entityFilter).sort(entitySort);
}
return acc;
}, {});
return {
...state, ...nested, [name]: [...action[name], ...state[name]].filter(filter).sort(sort)
};
}
}
return state;
}
function errorMessage(state = null, action) {
}
function loading(_state = false, action) {
return action.type.endsWith(REQUEST);
}
2018-08-01 13:13:51 +00:00
2018-08-02 10:27:33 +00:00
const rootReducer = combineReducers({
2018-08-07 14:09:55 +00:00
entities,
loading,
2018-08-07 08:40:20 +00:00
errorMessage,
fiddles: fiddleRecuder
2018-08-02 10:27:33 +00:00
});
2018-08-01 13:13:51 +00:00
export default rootReducer;