From 9f4c08635f1c5849901ea731b674de7271636517 Mon Sep 17 00:00:00 2001 From: Aaron Imming Date: Fri, 4 Oct 2019 11:51:44 +0800 Subject: [PATCH] Refactor dropdown logic --- src/botPage/view/blockly/blocks/shared.js | 87 ++++++++++++++----- .../view/blockly/blocks/trade/components.js | 6 +- .../view/blockly/blocks/trade/index.js | 6 ++ .../view/blockly/blocks/trade/tools.js | 2 +- 4 files changed, 73 insertions(+), 28 deletions(-) diff --git a/src/botPage/view/blockly/blocks/shared.js b/src/botPage/view/blockly/blocks/shared.js index 7e5ed841fe..c7f61cb17d 100644 --- a/src/botPage/view/blockly/blocks/shared.js +++ b/src/botPage/view/blockly/blocks/shared.js @@ -16,6 +16,10 @@ let purchaseChoices = [[translate('Click to select'), '']]; export const getPurchaseChoices = () => purchaseChoices; const filterPurchaseChoices = (contractType, oppositesName) => { + if (oppositesName.toLowerCase() === 'na') { + return [[translate('Not available'), 'na']]; + } + const { [oppositesName]: tradeTypes } = config.opposites; let tmpPurchaseChoices = tradeTypes.filter(k => @@ -86,46 +90,79 @@ const getActiveMarket = markets => fieldGeneratorMapping.MARKET_LIST = () => { const markets = getActiveMarket(symbolApi.activeSymbols.getMarkets()); + + if (Object.keys(markets).length === 0) { + return [[translate('Not available'), 'na']]; + } return Object.keys(markets).map(e => [markets[e].name, e]); }; fieldGeneratorMapping.SUBMARKET_LIST = block => () => { const markets = getActiveMarket(symbolApi.activeSymbols.getMarkets()); const marketName = block.getFieldValue('MARKET_LIST'); - if (!marketName || marketName === 'Invalid') { - return [['', 'Invalid']]; + const submarketOptions = []; + + if (Object.keys(markets).length > 0 && marketName !== 'na') { + const marketObj = markets[marketName]; + + if (marketObj) { + const submarkets = getActiveSubMarket(marketObj.submarkets); + + submarketOptions.push( + ...Object.keys(submarkets) + .map(e => [submarkets[e].name, e]) + // Filter out markets we don't have contracts for + .filter(submarket => !['energy'].includes(submarket[1])) + ); + } } - const submarkets = getActiveSubMarket(markets[marketName].submarkets); - return ( - Object.keys(submarkets) - .map(e => [submarkets[e].name, e]) - // Filter out markets we don't have contracts for - .filter(submarket => !['energy'].includes(submarket[1])) - ); + + if (submarketOptions.length === 0) { + return [[translate('Not available'), 'na']]; + } + + return submarketOptions; }; fieldGeneratorMapping.SYMBOL_LIST = block => () => { const markets = getActiveMarket(symbolApi.activeSymbols.getMarkets()); const submarketName = block.getFieldValue('SUBMARKET_LIST'); - if (!submarketName || submarketName === 'Invalid') { - return [['', '']]; + const symbolOptions = []; + + if (Object.keys(markets).length > 0 && submarketName !== 'na') { + const marketName = block.getFieldValue('MARKET_LIST'); + const marketObj = markets[marketName]; + + if (marketObj) { + const { submarkets } = marketObj; + + if (Object.keys(submarkets).length > 0 && submarkets[submarketName]) { + const symbols = getActiveSymbols(submarkets[submarketName].symbols); + + symbolOptions.push( + ...Object.keys(symbols) + .map(e => [symbols[e].display, symbols[e].symbol]) + // Filter out symbols we don't have contracts for (these symbols have only forward-starting) + .filter(symbol => !['frxGBPNOK', 'frxUSDNOK', 'frxUSDNEK', 'frxUSDSEK'].includes(symbol[1])) + ); + } + } } - const marketName = block.getFieldValue('MARKET_LIST'); - const submarkets = getActiveSubMarket(markets[marketName].submarkets); - const symbols = getActiveSymbols(submarkets[submarketName].symbols); - return ( - Object.keys(symbols) - .map(e => [symbols[e].display, symbols[e].symbol]) - // Filter out symbols we don't have contracts for (these symbols have only forward-starting) - .filter(symbol => !['frxGBPNOK', 'frxUSDNOK', 'frxUSDNEK', 'frxUSDSEK'].includes(symbol[1])) - ); + + if (symbolOptions.length === 0) { + return [[translate('Not available'), 'na']]; + } + + return symbolOptions; }; fieldGeneratorMapping.TRADETYPECAT_LIST = block => () => { const symbol = block.getFieldValue('SYMBOL_LIST'); - if (!symbol) { - return [['', '']]; + + if (!symbol || symbol === 'na') { + return [[translate('Not available'), 'na']]; } + const allowedCategories = symbolApi.getAllowedCategories(symbol.toLowerCase()); return Object.keys(config.conditionsCategoryName) .filter(e => allowedCategories.indexOf(e) >= 0) @@ -134,9 +171,11 @@ fieldGeneratorMapping.TRADETYPECAT_LIST = block => () => { fieldGeneratorMapping.TRADETYPE_LIST = block => () => { const tradeTypeCat = block.getFieldValue('TRADETYPECAT_LIST'); - if (!tradeTypeCat) { - return [['', '']]; + + if (!tradeTypeCat || tradeTypeCat === 'na') { + return [[translate('Not available'), 'na']]; } + return ( config.conditionsCategory[tradeTypeCat] .map(e => [config.opposites[e.toUpperCase()].map(c => c[Object.keys(c)[0]]).join('/'), e]) diff --git a/src/botPage/view/blockly/blocks/trade/components.js b/src/botPage/view/blockly/blocks/trade/components.js index a11467343e..2c54a6831c 100644 --- a/src/botPage/view/blockly/blocks/trade/components.js +++ b/src/botPage/view/blockly/blocks/trade/components.js @@ -8,7 +8,7 @@ export const marketDropdown = block => { block .appendDummyInput('MARKETDEFINITION') .appendField(`${translate('Market')}:`) - .appendField(new Blockly.FieldDropdown(fieldGeneratorMapping.MARKET_LIST), 'MARKET_LIST') + .appendField(new Blockly.FieldDropdown(fieldGeneratorMapping.MARKET_LIST()), 'MARKET_LIST') .appendField('>') .appendField(new Blockly.FieldDropdown(fieldGeneratorMapping.SUBMARKET_LIST(block)), 'SUBMARKET_LIST') .appendField('>') @@ -28,10 +28,10 @@ export const contractTypes = block => { if (!block.getInput('CONTRACT_TYPE')) { const getContractTypes = () => { const tradeType = block.getFieldValue('TRADETYPE_LIST'); - if (tradeType) { + if (tradeType && tradeType !== 'na') { return [[translate('Both'), 'both'], ...oppositesToDropdown(config.opposites[tradeType.toUpperCase()])]; } - return [['', '']]; + return [[translate('Not available'), 'na']]; }; block .appendDummyInput('CONTRACT_TYPE') diff --git a/src/botPage/view/blockly/blocks/trade/index.js b/src/botPage/view/blockly/blocks/trade/index.js index 77dc0f9139..0327010694 100644 --- a/src/botPage/view/blockly/blocks/trade/index.js +++ b/src/botPage/view/blockly/blocks/trade/index.js @@ -107,6 +107,12 @@ Blockly.Blocks.trade = { resetTradeFields(this, ev); } + if (ev.type === Blockly.Events.BLOCK_CREATE && ev.group !== 'load') { + const marketField = this.getField('MARKET_LIST'); + marketField.setValue(''); + marketField.setValue(marketField.menuGenerator_[0][1]); // eslint-disable-line + } + decorateTrade(ev); }, }; diff --git a/src/botPage/view/blockly/blocks/trade/tools.js b/src/botPage/view/blockly/blocks/trade/tools.js index ad62dbdf0b..96439a86c7 100644 --- a/src/botPage/view/blockly/blocks/trade/tools.js +++ b/src/botPage/view/blockly/blocks/trade/tools.js @@ -9,7 +9,7 @@ export const getParentValue = (block, fieldName) => { export const updateInputList = block => { const tradeType = getParentValue(block, 'TRADETYPE_LIST'); - if (tradeType) { + if (Blockly.Blocks[tradeType]) { Blockly.Blocks[tradeType].init.call(block); } };