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

392 lines
11 KiB
JavaScript
Raw Normal View History

2018-08-01 18:09:58 +00:00
import {combineReducers} from 'redux';
2018-10-04 13:19:14 +00:00
import {REQUEST, SUCCESS, FAILURE, CONTRACT_COMPILE, FILES, LOGOUT, AUTHENTICATE,
FETCH_CREDENTIALS, UPDATE_BASE_ETHER, CHANGE_THEME, FETCH_THEME, EXPLORER_SEARCH, DEBUGGER_INFO,
2018-10-17 13:11:10 +00:00
SIGN_MESSAGE, VERIFY_MESSAGE, TOGGLE_BREAKPOINT,
2018-10-24 12:45:12 +00:00
UPDATE_DEPLOYMENT_PIPELINE, WEB3_CONNECT, WEB3_DEPLOY, WEB3_ESTIMAGE_GAS, FETCH_EDITOR_TABS} from "../actions";
import {EMBARK_PROCESS_NAME, DARK_THEME, DEPLOYMENT_PIPELINES, DEFAULT_HOST} from '../constants';
2018-08-07 14:09:55 +00:00
const BN_FACTOR = 10000;
2018-10-03 11:51:14 +00:00
const VOID_ADDRESS = '0x0000000000000000000000000000000000000000';
const MAX_ELEMENTS = 200;
2018-08-07 14:09:55 +00:00
const entitiesDefaultState = {
accounts: [],
blocks: [],
transactions: [],
processes: [],
2018-10-17 22:36:46 +00:00
services: [],
2018-08-07 14:09:55 +00:00
processLogs: [],
commandSuggestions: [],
2018-08-07 14:09:55 +00:00
contracts: [],
contractProfiles: [],
2018-08-16 11:17:13 +00:00
contractFunctions: [],
2018-08-16 15:18:07 +00:00
contractDeploys: [],
2018-08-30 12:13:37 +00:00
contractCompiles: [],
2018-08-09 19:33:07 +00:00
contractLogs: [],
2018-10-09 14:42:46 +00:00
contractEvents: [],
2018-08-08 19:03:46 +00:00
messages: [],
messageChannels: [],
versions: [],
2018-08-13 11:08:59 +00:00
plugins: [],
2018-08-30 12:13:37 +00:00
ensRecords: [],
2018-08-31 20:44:43 +00:00
files: [],
2018-08-30 12:13:37 +00:00
gasOracleStats: [],
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);
},
2018-10-11 14:19:46 +00:00
processes: function(a, b) {
if (a.name === EMBARK_PROCESS_NAME) return -1;
if (b.name === EMBARK_PROCESS_NAME) return 1;
return 0;
},
2018-10-17 13:59:57 +00:00
commandSuggestions: function(a, b) {
if (a.value.indexOf('.').length > 0) {
let a_levels = a.value.split('.').length;
let b_levels = b.value.split('.').length;
let diff = b_levels - a_levels;
if (diff !== 0) return diff * -1;
}
let lengthDiff = b.value.length - a.value.length;
if (lengthDiff !== 0) return lengthDiff * -1;
return 0;
},
2018-08-07 14:09:55 +00:00
processLogs: function(a, b) {
2018-10-11 14:19:46 +00:00
if (a.name !== b.name) {
if(a.name < b.name) return -1;
if(a.name > b.name) return 1;
return 0;
}
if (a.id === undefined && b.id === undefined) {
return b.timestamp - a.timestamp;
}
2018-10-11 09:10:27 +00:00
return b.id - a.id;
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-31 09:57:52 +00:00
},
2018-08-30 12:13:37 +00:00
files: function(a, b) {
if (a.name < b.name) return -1;
if (a.name > b.name) return 1;
return 0;
2018-10-24 12:45:12 +00:00
},
2018-08-07 14:09:55 +00:00
};
const filtrer = {
processes: function(process, index, self) {
2018-10-18 11:10:14 +00:00
if (["embark", "blockchain"].indexOf(process.name) === -1) return false;
return index === self.findIndex((t) => t.name === process.name);
},
services: function(process, index, self) {
2018-08-07 14:09:55 +00:00
return index === self.findIndex((t) => t.name === process.name);
},
2018-10-11 09:10:27 +00:00
processLogs: function(processLog, index, self) {
2018-10-11 14:19:46 +00:00
if (processLog.id !== undefined) {
2018-10-15 20:36:30 +00:00
return index === self.findIndex((p) => p.id === processLog.id) && index <= MAX_ELEMENTS;
2018-10-11 14:19:46 +00:00
}
return true;
},
2018-08-07 14:09:55 +00:00
contracts: function(contract, index, self) {
return index === self.findIndex((t) => t.className === contract.className);
2018-08-07 14:09:55 +00:00
},
commandSuggestions: function(command, index, self) {
2018-10-14 03:27:20 +00:00
return index === self.findIndex((c) => (
command.value === c.value
));
},
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) {
if (index > MAX_ELEMENTS) {
return false;
}
2018-08-07 14:09:55 +00:00
return index === self.findIndex((t) => t.number === block.number);
},
transactions: function(tx, index, self) {
if (index > MAX_ELEMENTS) {
return false;
}
2018-08-07 14:09:55 +00:00
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-10-03 11:51:14 +00:00
return record.name && record.address && record.address !== VOID_ADDRESS && index === self.findIndex((r) => (
r.address === record.address && r.name === record.name
2018-08-14 09:23:48 +00:00
));
2018-08-30 12:13:37 +00:00
},
files: function(file, index, self) {
return index === self.findIndex((f) => (
file.name === f.name
));
2018-08-31 20:47:58 +00:00
},
gasOracleStats: function(stat, index, _self) {
return index === 0; // Only keep last one
2018-09-19 14:59:01 +00:00
},
versions: function(version, index, self) {
return index === self.findIndex((v) => v.value === version.value && v.name === version.name);
2018-08-07 14:09:55 +00:00
}
};
function entities(state = entitiesDefaultState, action) {
2018-08-30 12:13:37 +00:00
if (action.type === FILES[SUCCESS]) {
return {...state, files: action.files};
}
2018-08-07 14:09:55 +00:00
for (let name of Object.keys(state)) {
let filter = filtrer[name] || (() => true);
let sort = sorter[name] || (() => true);
if (action[name] && action[name].length > 1) {
return {...state, [name]: [...action[name], ...state[name]].sort(sort).filter(filter)};
2018-08-07 14:09:55 +00:00
}
if (action[name] && action[name].length === 1) {
let entity = action[name][0];
let nested = Object.keys(state).reduce((acc, entityName) => {
if (entity && entity[entityName] && entity[entityName].length > 0) {
2018-08-07 14:09:55 +00:00
let entityFilter = filtrer[entityName] || (() => true);
let entitySort = sorter[entityName] || (() => true);
acc[entityName] = [...entity[entityName], ...state[entityName]].sort(entitySort).filter(entityFilter);
2018-08-07 14:09:55 +00:00
}
return acc;
}, {});
return {
...state, ...nested, [name]: [...action[name], ...state[name]].sort(sort).filter(filter)
2018-08-07 14:09:55 +00:00
};
}
}
return state;
}
2018-09-19 14:59:01 +00:00
function errorMessage(_state = null, action) {
return action.error || null;
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 && action[name][0]) {
2018-08-14 16:17:47 +00:00
newState[name] = action[name][0].error;
}
}
return {...state, ...newState};
}
2018-08-07 14:09:55 +00:00
function loading(_state = false, action) {
2018-10-11 14:19:46 +00:00
return action.type.endsWith(REQUEST);
2018-08-07 14:09:55 +00:00
}
2018-08-01 13:13:51 +00:00
2018-08-30 12:13:37 +00:00
function compilingContract(state = false, action) {
if (action.type === CONTRACT_COMPILE[REQUEST]) {
2018-08-30 12:13:37 +00:00
return true;
} else if (action.type === CONTRACT_COMPILE[FAILURE] || action.type === CONTRACT_COMPILE[SUCCESS]) {
return false;
}
return state;
}
const DEFAULT_CREDENTIALS_STATE = {
host: process.env.NODE_ENV === 'development' ? DEFAULT_HOST : window.location.host,
token: '',
authenticated: false,
authenticating: false
};
2018-10-03 11:51:14 +00:00
function credentials(state = DEFAULT_CREDENTIALS_STATE, action) {
2018-09-19 14:59:01 +00:00
if (action.type === LOGOUT[SUCCESS]) {
2018-10-03 11:51:14 +00:00
return DEFAULT_CREDENTIALS_STATE;
2018-09-19 14:59:01 +00:00
}
if (action.type === AUTHENTICATE[FAILURE]) {
return {error: action.error, ...DEFAULT_CREDENTIALS_STATE};
2018-10-03 11:51:14 +00:00
}
if (action.type === AUTHENTICATE[SUCCESS]) {
return {...state, ...{authenticated: true, authenticating: false, token: action.token, host: action.host, error: null}};
2018-09-19 14:59:01 +00:00
}
2018-10-03 11:51:14 +00:00
if (action.type === FETCH_CREDENTIALS[SUCCESS]) {
return {...state, ...{token: action.token, host: action.host}};
}
if (action.type === AUTHENTICATE[REQUEST]) {
return {...state, ...{authenticating: true}};
}
2018-10-03 11:51:14 +00:00
return state;
}
2018-10-04 15:08:13 +00:00
function baseEther(state = '1', action) {
if (action.type === UPDATE_BASE_ETHER) {
2018-10-04 12:48:09 +00:00
return action.payload;
}
return state;
}
2018-10-16 20:43:54 +00:00
function theme(state=DARK_THEME, action) {
if (action.type === CHANGE_THEME[REQUEST] || (action.type === FETCH_THEME[SUCCESS] && action.theme)) {
2018-10-15 20:21:20 +00:00
return action.theme;
}
return state
}
function deploymentPipeline(state = DEPLOYMENT_PIPELINES.embark, action) {
if (action.type === UPDATE_DEPLOYMENT_PIPELINE) {
return action.payload;
}
2018-10-15 20:21:20 +00:00
return state;
}
2018-10-16 19:41:10 +00:00
function searchResult(state = {}, action) {
2018-10-17 13:59:57 +00:00
if (action.type === EXPLORER_SEARCH[SUCCESS]) {
return action.searchResult;
}
2018-10-16 20:17:17 +00:00
if (action.type === EXPLORER_SEARCH[REQUEST]) {
return {};
}
2018-10-17 13:59:57 +00:00
return state;
}
const DEFAULT_MESSAGE_SIGNATURE_STATE = {
pending: false,
error: null,
payload: null
};
function messageSignature(state = DEFAULT_MESSAGE_SIGNATURE_STATE, action) {
if (action.type === SIGN_MESSAGE[REQUEST]) {
return {...state, pending: true, error: null, payload: null };
}
if (action.type === SIGN_MESSAGE[FAILURE]) {
return {...state, pending: false, error: action.signMessageError };
}
if (action.type === SIGN_MESSAGE[SUCCESS]) {
return {...state, ...{
pending: false,
error: null,
payload: {
signature: action.signature,
message: action.message,
signer: action.signer
}
}};
}
return state;
}
const DEFAULT_MESSAGE_VERIFICATION_STATE = {
pending: false,
error: null,
payload: null
};
function messageVerification(state = DEFAULT_MESSAGE_VERIFICATION_STATE, action) {
if (action.type === VERIFY_MESSAGE[REQUEST]) {
return {...state, pending: true, error: null, payload: null };
}
if (action.type === VERIFY_MESSAGE[FAILURE]) {
return {...state, pending: false, error: action.verifyMessageError };
}
if (action.type === VERIFY_MESSAGE[SUCCESS]) {
return {...state, ...{
pending: false,
error: null,
payload: {
verifiedAddress: action.address
}
}};
}
return state;
}
2018-10-18 10:09:03 +00:00
function breakpoints(state = {}, action) {
if (action.type === TOGGLE_BREAKPOINT[SUCCESS]) {
2018-10-18 10:09:03 +00:00
const {filename, lineNumber} = action.payload;
let lineNumbers = state[filename] || [];
if (lineNumbers.includes(lineNumber)){
lineNumbers = lineNumbers.filter(ln => ln !== lineNumber);
} else {
lineNumbers.push(lineNumber);
}
return {...state, [filename]: lineNumbers};
}
return state;
}
function web3(state = {deployments: {}, gasEstimates: {}}, action) {
if (action.type === WEB3_CONNECT[SUCCESS]) {
return {...state, instance: action.web3};
} else if (action.type === WEB3_DEPLOY[REQUEST]) {
return {...state, deployments: {...state['deployments'], [action.contract.className]: {running: true, error: null}}};
} else if (action.type === WEB3_DEPLOY[SUCCESS]){
return {...state, deployments: {...state['deployments'], [action.contract.className]: {...action.receipt, running: false, error: null}}};
} else if (action.type === WEB3_DEPLOY[FAILURE]){
return {...state, deployments: {...state['deployments'], [action.contract.className]: {error: action.web3Error, running: false}}};
} else if (action.type === WEB3_ESTIMAGE_GAS[REQUEST]){
return {...state, gasEstimates: {...state['gasEstimates'], [action.contract.className]: {running: true, error: null}}};
} else if (action.type === WEB3_ESTIMAGE_GAS[SUCCESS]){
return {...state, gasEstimates: {...state['gasEstimates'], [action.contract.className]: {gas: action.gas, running: false, error: null}}};
} else if (action.type === WEB3_ESTIMAGE_GAS[FAILURE]){
return {...state, gasEstimates: {...state['gasEstimates'], [action.contract.className]: {error: action.web3Error, running: false}}};
}
return state
}
function debuggerInfo(state={}, action) {
if (action.type === DEBUGGER_INFO[SUCCESS]) {
return action.data;
}
2018-10-24 18:11:24 +00:00
return state;
2018-10-24 12:45:12 +00:00
}
function editorTabs(state = [], action) {
if (action.type === FETCH_EDITOR_TABS[SUCCESS] && action.editorTabs) {
return action.editorTabs;
}
return state;
}
2018-08-02 10:27:33 +00:00
const rootReducer = combineReducers({
2018-08-07 14:09:55 +00:00
entities,
loading,
2018-08-30 12:13:37 +00:00
compilingContract,
2018-08-14 16:17:47 +00:00
errorMessage,
errorEntities,
2018-10-04 12:48:09 +00:00
credentials,
2018-10-15 20:21:20 +00:00
baseEther,
searchResult,
messageSignature,
2018-10-18 10:09:03 +00:00
messageVerification,
breakpoints,
deploymentPipeline,
web3,
debuggerInfo,
2018-10-24 12:45:12 +00:00
theme,
editorTabs
2018-08-02 10:27:33 +00:00
});
2018-08-01 13:13:51 +00:00
export default rootReducer;