Skip to content

Commit

Permalink
refactor parent categories selector controller code
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilelkihal committed Jan 2, 2025
1 parent 39a645d commit 18d87b7
Showing 1 changed file with 13 additions and 20 deletions.
33 changes: 13 additions & 20 deletions app/javascript/controllers/parent_categories_selector_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ export default class extends Controller {
check(event){
const input = event.currentTarget.querySelector('input')
const allInputs = this.chipsTarget.querySelectorAll('input')
const children = this.#categories_with_parents()
const parents = this.#categories_with_children(children)
const parents = this.#categories_with_children()

if(this.#id_to_acronym(input.value) in parents){
const parentChildren = parents[this.#id_to_acronym(input.value)]
Expand All @@ -27,28 +26,22 @@ export default class extends Controller {
}
}

#categories_with_parents() {
const children = {};
#categories_with_children(){
const parentToChildren = {};
this.categoriesValue.forEach(category => {
if (category.parentCategory.length > 0) {
const parentsAcronyms = category.parentCategory.map(p => p.split('/').pop());
children[category.acronym] = parentsAcronyms;
}
});
return children;
}
if (category.parentCategory) {
category.parentCategory.forEach(parentId => {
const parentAcronym = this.#id_to_acronym(parentId);
const childAcronym = this.#id_to_acronym(category.id);

#categories_with_children(children){
const parents = {};
Object.keys(children).forEach(child => {
children[child].forEach(parent => {
if (!parents[parent]) {
parents[parent] = [];
}
parents[parent].push(child);
if (!parentToChildren[parentAcronym]) {
parentToChildren[parentAcronym] = [];
}
parentToChildren[parentAcronym].push(childAcronym);
});
}
});
return parents
return parentToChildren
}

#id_to_acronym(id){
Expand Down

0 comments on commit 18d87b7

Please sign in to comment.