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

fix: converted const to a arrow function #187

Closed
Closed
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
92 changes: 62 additions & 30 deletions src/external/bot-skeleton/scratch/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,24 +410,47 @@ const getDisabledBlocks = required_blocks_check => {
});
};

const throwNewErrorMessage = (error_blocks, key) => {
return error_blocks.forEach(block => {
if (key === 'misplaced' && block) globalObserver.emit('ui.log.error', error_message_map[block?.type]?.[key]);
else if (key === 'missing' && block) globalObserver.emit('ui.log.error', error_message_map[block]?.[key]);
else if (key === 'disabled' && block) {
const throwNewErrorMessage = async (error_blocks, key) => {
for (const block of error_blocks) {
const error_message_map_ = async (type, key) => {
try {
const data = await error_message_map[type];
return data[key];
} catch (error) {
console.error('Error fetching error message:', error);
return null;
}
};
if (key === 'misplaced' && block) {
const error_message = await error_message_map_(block, key);
if (error_message) {
globalObserver.emit('ui.log.error', error_message);
}
} else if (key === 'missing' && block) {
// console.log('block send outside', block, key);
const error_message = await error_message_map_(block, key);
if (error_message) {
globalObserver.emit('ui.log.error', error_message);
}
} else if (key === 'disabled' && block) {
let parent_block_error = false;
const parent_error_message = error_message_map[block.type]?.[key];
// console.log('block send outside', block.type, key);

const parent_error_message = await error_message_map_(block?.type, key);

if (block.disabled && parent_error_message) {
globalObserver.emit('ui.log.error', parent_error_message);
parent_block_error = true;
} else if (!parent_block_error && block.childBlocks_) {
block.childBlocks_.forEach(childBlock => {
const child_error_message = error_message_map[childBlock.type]?.[key];
if (child_error_message) globalObserver.emit('ui.log.error', child_error_message);
});
for (const childBlock of block.childBlocks_) {
const child_error_message = await error_message_map_(childBlock.type, key);
if (child_error_message) {
globalObserver.emit('ui.log.error', child_error_message);
}
}
}
}
});
}
};

export const isAllRequiredBlocksEnabled = workspace => {
Expand All @@ -448,6 +471,7 @@ export const isAllRequiredBlocksEnabled = workspace => {
const error_blocks = [...missing_blocks, ...disabled_blocks];
const blocks_required = error_blocks.length === 0;

console.log('blocks_required', error_blocks);
return blocks_required;
};

Expand Down Expand Up @@ -623,12 +647,16 @@ const downloadBlock = () => {
const xml_text = window.Blockly.Xml.domToPrettyText(xml_block);
saveAs({ data: xml_text, type: 'text/xml;charset=utf-8', filename: 'block.xml' });
};
const getLocalizedText = text => {
if (!text) return text;
return localize(text) || text;
};

const download_option = {
text: localize('Download Block'),
const download_option = () => ({
text: getLocalizedText('Download Block') || 'Download Block',
enabled: true,
callback: downloadBlock,
};
});

export const excludeOptionFromContextMenu = (menu, exclude_items) => {
for (let i = 0; i <= menu.length - 1; i++) {
Expand All @@ -641,23 +669,27 @@ export const excludeOptionFromContextMenu = (menu, exclude_items) => {
}
};

const common_included_items = [download_option];
const common_included_items = [download_option()];

const all_context_menu_options = [
localize('Duplicate'),
localize('Add Comment'),
localize('Remove Comment'),
localize('Collapse Block'),
localize('Expand Block'),
localize('Disable Block'),
localize('Enable Block'),
localize('Download Block'),
];
const all_context_menu_options = () =>
[
'Duplicate',
'Add Comment',
'Remove Comment',
'Collapse Block',
'Expand Block',
'Disable Block',
'Enable Block',
'Download Block',
].map(getLocalizedText);

const deleteBlocksLocaleText = localize('Delete Block');
const deleteAllBlocksLocaleText = localize('Delete All Blocks');
const deleteBlocksLocaleText = () => localize('Delete Block');
const deleteAllBlocksLocaleText = () => localize('Delete All Blocks');

export const modifyContextMenu = (menu, add_new_items = []) => {
const all_context_menu_options_ = all_context_menu_options();
const delete_block_lacale_text_ = deleteBlocksLocaleText();
const delete_all_blocks_locale_text_ = deleteAllBlocksLocaleText();
const include_items = [...common_included_items, ...add_new_items];
include_items.forEach(item => {
menu.push({
Expand All @@ -671,13 +703,13 @@ export const modifyContextMenu = (menu, add_new_items = []) => {
const menu_text = menu[i]?.text?.toLowerCase();
if (menu_text?.includes('delete')) {
if (menu_text.includes('block') && !menu_text.includes('blocks')) {
menu[i].text = deleteBlocksLocaleText;
menu[i].text = delete_block_lacale_text_;
} else {
menu[i].text = deleteAllBlocksLocaleText;
menu[i].text = delete_all_blocks_locale_text_;
}
} else {
const localized_text = localize(menu[i].text);
if (all_context_menu_options.includes(localized_text)) {
if (all_context_menu_options_.includes(localized_text)) {
menu[i].text = localized_text;
}
}
Expand Down
52 changes: 16 additions & 36 deletions src/external/bot-skeleton/utils/error-config.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,21 @@
import { Localize } from '@deriv-com/translations';
const generateErrorMessage = async (block_type, missing_space) => {
const { localize } = await import('@deriv-com/translations');
if (!missing_space) missing_space = localize('workspace');

const generateErrorMessage = (block_type, missing_space = 'workspace') => {
return {
missing: (
<Localize
i18n_default_text={`The {{block_type}} block is missing.`}
values={{
block_type,
}}
/>
),
misplaced: (
<Localize
i18n_default_text={`The {{block_type}} block is misplaced from {{missing_space}}.`}
values={{
block_type,
missing_space,
}}
/>
),

disabled: (
<Localize
i18n_default_text={`The {{block_type}} block is mandatory and cannot be deleted/disabled.`}
values={{
block_type,
}}
/>
),
default: (
<Localize
i18n_default_text={`The {{block_type}} block is mandatory and cannot be deleted/disabled.`}
values={{
block_type,
}}
/>
),
missing: localize('The {{block_type}} block is mandatory and cannot be deleted/disabled.', {
block_type,
}),
misplaced: localize('The {{block_type}} block is misplaced from {{missing_space}}.', {
block_type,
missing_space,
}),
disabled: localize('The {{block_type}} block is mandatory and cannot be deleted/disabled.', {
block_type,
}),
default: localize('The {{block_type}} block is mandatory and cannot be deleted/disabled.', {
block_type,
}),
};
};

Expand Down
6 changes: 3 additions & 3 deletions src/external/bot-skeleton/utils/error-handling.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ export const initErrorHandlingListener = (type = 'keydown') => {
export const handleError = (errorCode, observer) => {
switch (errorCode) {
case 'BLOCK_DELETION':
if (error_message_map[window.Blockly.getSelected().type]) {
observer.emit('ui.log.error', error_message_map[window.Blockly.getSelected().type]?.default);
}
error_message_map[window.Blockly.getSelected().type].then(data => {
observer.emit('ui.log.error', data?.default?.());
});
break;
default:
break;
Expand Down
Loading