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

Bug fix in update selector func #152

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "web-scraper-chrome-extension",
"version": "0.5.1",
"version": "0.5.2",
"description": "Web data extraction tool implemented as chrome extension",
"scripts": {
"lint": "eslint --ext .js src",
Expand Down
12 changes: 12 additions & 0 deletions src/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -355,13 +355,25 @@
"modal_confirm_action_title_delete_selector": {
"message": "Are you sure you want to delete selector <span id='modal-selector-id'></span>?"
},
"modal_confirm_action_title_update_selector": {
"message": "The new selector type does not support additional selectors. Remove additional child selectors?"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why "additional"?

},
"modal_confirm_action_title_update_selector_can_have_child": {
"message": "The new selector type supports child selectors. Remove or keep existing child selectors?"
},
"modal_confirm_action_title_delete_sitemap": {
"message": "Are you sure you want to delete sitemap <span id='modal-sitemap-id'></span>?"
},
"modal_confirm_action_message_delete_selector": {
"message": "The following selectors will be removed (<span id='modal-child-count'></span> obj):"
},
"modal_confirm_action_message_update_selector": {
"message": "Child selectors (<span id='modal-child-count'></span> obj):"
},
"modal_confirm_action_submit_delete_selector": { "message": "Confirm" },
"modal_confirm_action_submit_update_selector": { "message": "Delete selectors" },
"modal_confirm_action_submit_update_save_selector": { "message": "Save selectors" },
"modal_confirm_action_cancel_update_selector": { "message": "Cancel" },
"modal_confirm_action_cancel_delete_selector": { "message": "Cancel" },
"modal_confirm_action_submit_delete_sitemap": { "message": "Confirm" },
"modal_confirm_action_cancel_delete_sitemap": { "message": "Cancel" },
Expand Down
13 changes: 13 additions & 0 deletions src/_locales/ru/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,19 @@
"modal_confirm_action_message_delete_selector": {
"message": "Будут удалены следующие селекторы (<span id='modal-child-count'></span> шт.):"
},
"modal_confirm_action_message_update_selector": {
"message": "Дочерние селекторы (<span id='modal-child-count'></span> obj):"
},
"modal_confirm_action_title_update_selector": {
"message": "Новый тип селектора не поддерживает дочерние селекторы. Удалить существующие дочерние селекторы?"
},
"modal_confirm_action_title_update_selector_can_have_child": {
"message": "Новый тип селектора поддерживает дочерние селекторы. Удалить или сохранить существующие дочерние селекторы?"
},
"modal_confirm_action_submit_update_selector": { "message": "Удалить селекторы" },
"modal_confirm_action_submit_update_save_selector": { "message": "Сохранить селекторы" },
"modal_confirm_action_cancel_update_selector": { "message": "Отмена" },

"modal_confirm_action_submit_delete_selector": { "message": "Подтвердить" },
"modal_confirm_action_cancel_delete_selector": { "message": "Отмена" },
"modal_confirm_action_submit_delete_sitemap": { "message": "Подтвердить" },
Expand Down
56 changes: 53 additions & 3 deletions src/scripts/Controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@ export default class SitemapController {
'.delete_selector_submit': {
click: this.confirmDeleteSelector,
},
'.update_selector_submit': {
click: this.confirmDeleteChildSelectors,
},
'#modal-save-child-selectors': {
click: this.confirmSaveChildSelectors,
},
'.delete_sitemap_submit': {
click: this.confirmDeleteSitemap,
},
Expand Down Expand Up @@ -1401,7 +1407,38 @@ export default class SitemapController {
const newSelector = this.getCurrentlyEditedSelector();
const validator = this.getFormValidator();
validator.revalidateField('id');
// cancel submit if invalid form
if (selector.type !== newSelector.type) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modal init should be after form validation and removeCurrentContentSelector call, otherwise it is possible to create invalid selector

const children = sitemap.selectors.filter(selectorFromList =>
selectorFromList.parentSelectors.includes(selector.uuid)
);
if (children.length > 0) {
this.initConfirmActionPanel({ action: 'update_selector' });
if (newSelector.canHaveChildSelectors()) {
const saveSelectorsBtn = $('<button/>', {
class: 'btn btn-primary',
id: 'modal-save-child-selectors',
text: Translator.getTranslationByKey(
'modal_confirm_action_submit_update_save_selector'
),
});
$('.modal-footer').append(saveSelectorsBtn);
$('#modal-title').text(
Translator.getTranslationByKey(
'modal_confirm_action_title_update_selector_can_have_child'
)
);
}
$('#modal-child-count').text(children.length);
$('#modal-message').after('<ul id="list-deleted-children"></ul>');
children.forEach(child => {
const $child = $('<li></li>');
$child.text(`#${child.uuid} ${child.id}`);
$('#list-deleted-children').append($child);
});
$('#modal-message').show();
Comment on lines +1431 to +1438
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extract duplicated code into method

return true;
}
}
if (!this.isValidForm()) {
return false;
}
Expand All @@ -1411,16 +1448,29 @@ export default class SitemapController {
} catch (err) {
console.error(err);
}
await this.updateSelector();
}
async updateSelector(saveChildSelectors = false) {
Comment on lines +1452 to +1453
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add newlines between methods

const sitemap = this.state.currentSitemap;
const selector = this.state.currentSelector;
const newSelector = this.getCurrentlyEditedSelector();
try {
sitemap.updateSelector(selector, newSelector);
sitemap.updateSelector(selector, newSelector, saveChildSelectors);
await this.store.saveSitemap(sitemap, undefined, this.getCurrentProjectId());
} catch (err) {
console.error(err);
} finally {
this.showSitemapSelectorList();
}
}

async confirmSaveChildSelectors(button) {
await this.updateSelector(true);
$('#confirm-action-modal').modal('hide');
}
async confirmDeleteChildSelectors(button) {
await this.updateSelector(false);
$('#confirm-action-modal').modal('hide');
}
/**
* Get selector from selector editing form
*/
Expand Down
25 changes: 22 additions & 3 deletions src/scripts/Sitemap.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,32 @@ export default class Sitemap {
return urls;
}

updateSelector(selector, selectorData) {
updateSelector(selector, selectorData, saveChildSelectors = false) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like that the signature suggests children may be deleted even if selector type is not changed. We can think of another naming here (e.g. saveChildrenForNewType?), or implement deliberate removal - maybe it will simplify the code.

// selector is undefined when creating a new one and delete old one, if it exist
if (selector === undefined || selector.type !== selectorData.type) {
if (selector) {
this.deleteSelector(selector);
if (
selector.canHaveChildSelectors() &&
selectorData.canHaveChildSelectors() &&
saveChildSelectors
) {
//custom logic: we don’t delete children, but redefined them a parent
const children = this.selectors.filter(selectorFromList =>
selectorFromList.parentSelectors.includes(selector.uuid)
);
const newSelector = SelectorList.createSelector(selectorData);
children.forEach(child => {
const parentUuidIndex = child.parentSelectors.indexOf(selector.uuid);
child.parentSelectors[parentUuidIndex] = newSelector.uuid;
});
selector = newSelector;
Comment on lines +164 to +173
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar logic is present below in // update child selectors section, maybe it is possible to simplify it. Also I'm not sure if it is even possible to receive different selector uids here when we update existing selector.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can try to simplify this, after the main part of the logic is approved.

} else {
this.deleteSelector(selector);
selector = SelectorList.createSelector(selectorData);
}
} else {
selector = SelectorList.createSelector(selectorData);
}
selector = SelectorList.createSelector(selectorData);
}

// update child selectors
Expand Down