Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Amina/feature/90115/update jurisdiction #33

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/bot-skeleton/src/scratch/dbot-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ class DBotStore extends DBotStoreInterface {
this.is_mobile = store.is_mobile || false;
this.is_dark_mode_on = store.is_dark_mode_on || false;
this.client = store.client;
this.dashboard = store.dashboard;
this.flyout = store.flyout;
this.toolbar = store.toolbar;
this.toolbox = store.toolbox;
this.save_modal = store.save_modal;
this.load_modal = store.load_modal;
this.setContractUpdateConfig = store.setContractUpdateConfig;
this.toggleStrategyModal = store.toggleStrategyModal;
this.handleFileChange = store.handleFileChange;
this.startLoading = store.startLoading;
this.endLoading = store.endLoading;
this.setLoading = store.setLoading;

reaction(
() => this.client.loginid,
Expand Down
13 changes: 8 additions & 5 deletions packages/bot-skeleton/src/scratch/dbot.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class DBot {
*/
async initWorkspace(public_path, store, api_helpers_store, is_mobile) {
const recent_files = await getSavedWorkspaces();

return new Promise((resolve, reject) => {
__webpack_public_path__ = public_path; // eslint-disable-line no-global-assign
ApiHelpers.setInstance(api_helpers_store);
Expand All @@ -46,18 +47,13 @@ class DBot {
return;
}
this.workspace = Blockly.inject(el_scratch_div, {
grid: { spacing: 40, length: 11, colour: '#f3f3f3' },
media: `${__webpack_public_path__}media/`,
trashcan: !is_mobile,
zoom: { wheel: true, startScale: workspaceScale },
scrollbars: true,
});

this.workspace.cached_xml = { main: main_xml };
this.workspace.save_workspace_interval = setInterval(async () => {
// Periodically save the workspace.
await saveWorkspaceToRecent(Blockly.Xml.workspaceToDom(this.workspace), save_types.UNSAVED);
}, 10000);

this.workspace.addChangeListener(this.valueInputLimitationsListener.bind(this));
this.workspace.addChangeListener(event => updateDisabledBlocks(this.workspace, event));
Expand All @@ -73,12 +69,15 @@ class DBot {
// Push main.xml to workspace and reset the undo stack.
this.workspace.current_strategy_id = Blockly.utils.genUid();
Blockly.derivWorkspace.strategy_to_load = main_xml;
Blockly.mainWorkspace.strategy_to_load = main_xml;
let file_name = config.default_file_name;
if (recent_files && recent_files.length) {
const latest_file = recent_files[0];
Blockly.derivWorkspace.strategy_to_load = latest_file.xml;
Blockly.mainWorkspace.strategy_to_load = latest_file.xml;
file_name = latest_file.name;
Blockly.derivWorkspace.current_strategy_id = latest_file.id;
Blockly.mainWorkspace.current_strategy_id = latest_file.id;
}

const event_group = `dbot-load${Date.now()}`;
Expand Down Expand Up @@ -108,6 +107,10 @@ class DBot {
});
}

async saveRecentWorkspace() {
await saveWorkspaceToRecent(Blockly.Xml.workspaceToDom(this.workspace), save_types.UNSAVED);
}

/**
* Allows you to add a function that needs to be executed before running the bot. Each
* function needs to return true in order for the bot to run.
Expand Down
7 changes: 7 additions & 0 deletions packages/bot-skeleton/src/scratch/hooks/block_svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,13 @@ Blockly.BlockSvg.prototype.setCollapsed = function (collapsed) {
this.setErrorHighlighted(collapsed && this.hasErrorHighlightedDescendant());
};

export const blocksCoordinate = () => {
const during_purchase = Blockly.derivWorkspace?.getCanvas().children[1];
const after_purchase = Blockly.derivWorkspace?.getCanvas().children[2];
during_purchase?.setAttribute('transform', 'translate(720,0)');
after_purchase?.setAttribute('transform', 'translate(720,248)');
};

/**
* @deriv/bot: Add check for workspace.getCanvas() before appendChild() is called.
*/
Expand Down
5 changes: 2 additions & 3 deletions packages/bot-skeleton/src/scratch/hooks/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ Blockly.Css.CONTENT = [
'}',

'.injectionDiv {',
'height: 100%;',
'height: calc(100vh - 11rem);',
'position: relative;',
'overflow: hidden;' /* So blocks in drag surface disappear at edges */,
'touch-action: none',
'touch-action: none;',
'}',

'.blocklyNonSelectable {',
Expand Down Expand Up @@ -635,7 +635,6 @@ Blockly.Css.CONTENT = [

'.blocklyMainBackground {',
'stroke-width: 0;',
'fill: var(--general-main-1) !important',
// 'stroke: #c6c6c6;', /* Equates to #ddd due to border being off-pixel. */
'}',

Expand Down
39 changes: 20 additions & 19 deletions packages/bot-skeleton/src/scratch/hooks/trashcan.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
const trashcan_margin = 16;
const core_footer_height = 36;
export const initTrashCan = (margin = 16, height = 36) => {
/**
* Distance between trashcan and bottom edge of workspace.
* @type {number}
* @private
*/
Blockly.Trashcan.prototype.MARGIN_BOTTOM_ = margin * 2 + height; // eslint-disable-line

/**
* Distance between trashcan and bottom edge of workspace.
* @type {number}
* @private
*/
Blockly.Trashcan.prototype.MARGIN_BOTTOM_ = trashcan_margin * 2 + core_footer_height; // eslint-disable-line
/**
* Distance between trashcan and right edge of workspace.
* @type {number}
* @private
*/
Blockly.Trashcan.prototype.MARGIN_SIDE_ = margin; // eslint-disable-line

/**
* Distance between trashcan and right edge of workspace.
* @type {number}
* @private
*/
Blockly.Trashcan.prototype.MARGIN_SIDE_ = trashcan_margin; // eslint-disable-line
/**
* Inspect the contents of the trash.
* @deriv/bot: Noop for us, restore original functionality when trashcan can be inspected.
*/
Blockly.Trashcan.prototype.click = function () {};

/**
* Inspect the contents of the trash.
* @deriv/bot: Noop for us, restore original functionality when trashcan can be inspected.
*/
Blockly.Trashcan.prototype.click = function () {};
window.dispatchEvent(new Event('resize')); // trigger UI update
};
12 changes: 6 additions & 6 deletions packages/bot-skeleton/src/scratch/hooks/workspace_svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,12 +399,12 @@ Blockly.WorkspaceSvg.prototype.dispose = function (should_show_loading = false)
};

if (should_show_loading) {
const { startLoading, endLoading } = DBotStore.instance;
startLoading();
const { setLoading } = DBotStore.instance;
setLoading(true);

setTimeout(() => {
disposeFn();
endLoading();
setLoading(false);
}, 50);
} else {
disposeFn();
Expand All @@ -415,12 +415,12 @@ Blockly.WorkspaceSvg.prototype.dispose = function (should_show_loading = false)
* Dispose of all blocks in workspace, with an optimization to prevent resizes.
*/
Blockly.WorkspaceSvg.prototype.asyncClear = function () {
const { startLoading, endLoading } = DBotStore.instance;
startLoading();
const { setLoading } = DBotStore.instance;
setLoading(true);

return new Promise(resolve => {
this.clear();
endLoading();
setLoading(false);
resolve();
});
};
9 changes: 4 additions & 5 deletions packages/bot-skeleton/src/scratch/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ export const load = ({
workspace,
showIncompatibleStrategyDialog,
}) => {
const { startLoading, endLoading } = DBotStore.instance;
startLoading();
const { setLoading } = DBotStore.instance;
setLoading(true);

setTimeout(async () => {
const showInvalidStrategyError = () => {
endLoading();
setLoading(false);
const error_message = localize('XML file contains unsupported elements. Please check or modify file.');
globalObserver.emit('ui.log.error', error_message);
};
Expand Down Expand Up @@ -153,7 +153,7 @@ export const load = ({
console.error(e); // eslint-disable-line
return showInvalidStrategyError();
} finally {
endLoading();
setLoading(false);
}

return true;
Expand All @@ -176,7 +176,6 @@ export const loadBlocks = (xml, drop_event, event_group, workspace) => {
export const loadWorkspace = async (xml, event_group, workspace) => {
Blockly.Events.setGroup(event_group);
await workspace.asyncClear();

Blockly.Xml.domToWorkspace(xml, workspace);
};

Expand Down
6 changes: 3 additions & 3 deletions packages/bot-skeleton/src/scratch/xml/main.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<xml xmlns="http://www.w3.org/1999/xhtml" collection="false">
<xml xmlns="http://www.w3.org/1999/xhtml" collection="false" is_dbot="true">
<variables></variables>
<block type="trade_definition" x="0" y="0">
<statement name="TRADE_OPTIONS">
Expand Down Expand Up @@ -53,7 +53,7 @@
</block>
</statement>
</block>
<block type="during_purchase" x="770" y="0">
<block type="during_purchase" x="720" y="0">
<statement name="DURING_PURCHASE_STACK">
<block type="controls_if">
<value name="IF0">
Expand All @@ -62,7 +62,7 @@
</block>
</statement>
</block>
<block type="after_purchase" x="770" y="248">
<block type="after_purchase" x="720" y="248">
<statement name="AFTERPURCHASE_STACK">
<block type="trade_again"></block>
</statement>
Expand Down
2 changes: 2 additions & 0 deletions packages/bot-skeleton/src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ export { importExternal } from './html-helper';
export { onWorkspaceResize } from './workspace';
export { getSavedWorkspaces, saveWorkspaceToRecent, removeExistingWorkspace } from './local-storage';
export { timeSince } from './date-time-helper';
export { setColors } from '../scratch/hooks/colours';
export { blocksCoordinate } from '../scratch/hooks/block_svg';
9 changes: 6 additions & 3 deletions packages/bot-skeleton/src/utils/local-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import LZString from 'lz-string';
import localForage from 'localforage';
import DBotStore from '../scratch/dbot-store';
import { save_types } from '../constants/save-type';

/**
* Save workspace to localStorage
* @param {String} save_type // constants/save_types.js (unsaved, local, googledrive)
Expand All @@ -11,7 +10,11 @@ import { save_types } from '../constants/save-type';
export const saveWorkspaceToRecent = async (xml, save_type = save_types.UNSAVED) => {
// Ensure strategies don't go through expensive conversion.
xml.setAttribute('is_dbot', true);
const { save_modal } = DBotStore.instance;
const {
load_modal: { updateListStrategies },
save_modal,
} = DBotStore.instance;

const workspace_id = Blockly.derivWorkspace.current_strategy_id || Blockly.utils.genUid();
const workspaces = await getSavedWorkspaces();
const current_xml = Blockly.Xml.domToText(xml);
Expand Down Expand Up @@ -43,7 +46,7 @@ export const saveWorkspaceToRecent = async (xml, save_type = save_types.UNSAVED)
if (workspaces.length > 10) {
workspaces.pop();
}

updateListStrategies(workspaces);
localForage.setItem('saved_workspaces', LZString.compress(JSON.stringify(workspaces)));
};

Expand Down
2 changes: 1 addition & 1 deletion packages/bot-skeleton/src/utils/workspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const onWorkspaceResize = () => {

const el_scratch_div = document.getElementById('scratch_div');
if (el_scratch_div) {
el_scratch_div.style.width = '100vw';
el_scratch_div.style.width = 'calc(100vw - 3.2rem)';
el_scratch_div.style.height = 'var(--bot-content-height)';
Blockly.svgResize(workspace);
}
Expand Down
19 changes: 19 additions & 0 deletions packages/bot-web-ui/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const baseConfigForPackages = require('../../jest.config.base');

module.exports = {
...baseConfigForPackages,
clearMocks: true,
moduleNameMapper: {
'\\.s(c|a)ss$': '<rootDir>/../../__mocks__/styleMock.js',
'^.+\\.svg$': '<rootDir>/../../__mocks__/styleMock.js',
'^App/(.*)$': '<rootDir>/src/app/$1',
'^Components/(.*)$': '<rootDir>/src/components/$1',
'^Constants/(.*)$': '<rootDir>/src/constants/$1',
'^Stores/(.*)$': '<rootDir>/src/stores/$1',
'^Utils/(.*)$': '<rootDir>/src/utils/$1',
},
collectCoverageFrom: ['**/*.{js,jsx,ts,tsx}', '!**/node_modules/**'],
globals: {
__webpack_public_path__: '/',
},
};
Loading