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

127 lines
3.6 KiB
JavaScript
Raw Normal View History

2018-08-01 18:09:58 +00:00
import {combineReducers} from 'redux';
2018-08-14 16:17:47 +00:00
import {REQUEST, SUCCESS} from "../actions";
2018-08-07 14:09:55 +00:00
const BN_FACTOR = 10000;
2018-08-14 09:23:48 +00:00
const voidAddress = '0x0000000000000000000000000000000000000000';
2018-08-07 14:09:55 +00:00
const entitiesDefaultState = {
accounts: [],
blocks: [],
transactions: [],
processes: [],
processLogs: [],
contracts: [],
contractProfiles: [],
2018-08-15 13:59:16 +00:00
contractFiles: [],
2018-08-09 19:33:07 +00:00
contractLogs: [],
2018-08-08 19:03:46 +00:00
commands: [],
messages: [],
messageChannels: [],
2018-08-14 10:09:13 +00:00
fiddles: [],
versions: [],
2018-08-13 11:08:59 +00:00
plugins: [],
ensRecords: []
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
},
2018-08-15 13:59:16 +00:00
contractFiles: function(contractFile, index, self) {
return index === self.findIndex((c) => c.filename === contractFile.filename);
},
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
));
2018-08-14 09:23:48 +00:00
},
ensRecords: function(record, index, self) {
2018-08-14 15:22:15 +00:00
return record.name && record.address && record.address !== voidAddress && index === self.findIndex((r) => (
r.address=== record.address && r.name === record.name
2018-08-14 09:23:48 +00:00
));
2018-08-07 14:09:55 +00:00
}
};
function entities(state = entitiesDefaultState, action) {
for (let name of Object.keys(state)) {
let filter = filtrer[name] || (() => true);
let sort = sorter[name] || (() => true);
if (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) {
2018-08-10 02:38:55 +00:00
return action.error || state;
2018-08-07 14:09:55 +00:00
}
2018-08-14 16:17:47 +00:00
function errorEntities(state = {}, action) {
if (!action.type.endsWith(SUCCESS)) {
return state;
}
let newState = {};
for (let name of Object.keys(entitiesDefaultState)) {
if (action[name] && action[name].length > 0) {
newState[name] = action[name][0].error;
}
}
return {...state, ...newState};
}
2018-08-07 14:09:55 +00:00
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-14 16:17:47 +00:00
errorMessage,
errorEntities
2018-08-02 10:27:33 +00:00
});
2018-08-01 13:13:51 +00:00
export default rootReducer;