From b41faea372aa1418320294f83275036ba3467346 Mon Sep 17 00:00:00 2001 From: Andres Valencia Date: Tue, 5 Nov 2019 21:50:16 -0600 Subject: [PATCH 1/4] Implemented algorithm to map parent-children objets in menu nav --- package-lock.json | 41 ++++++++++++++++++++++++--------- src/components/subjects-tree.js | 4 ++-- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/package-lock.json b/package-lock.json index e98b7a8..c54b191 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3790,7 +3790,8 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "aproba": { "version": "1.2.0", @@ -3811,12 +3812,14 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, + "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -3831,17 +3834,20 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "core-util-is": { "version": "1.0.2", @@ -3958,7 +3964,8 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "ini": { "version": "1.3.5", @@ -3970,6 +3977,7 @@ "version": "1.0.0", "bundled": true, "dev": true, + "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -3984,6 +3992,7 @@ "version": "3.0.4", "bundled": true, "dev": true, + "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -3991,12 +4000,14 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "minipass": { "version": "2.3.5", "bundled": true, "dev": true, + "optional": true, "requires": { "safe-buffer": "^5.1.2", "yallist": "^3.0.0" @@ -4015,6 +4026,7 @@ "version": "0.5.1", "bundled": true, "dev": true, + "optional": true, "requires": { "minimist": "0.0.8" } @@ -4095,7 +4107,8 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "object-assign": { "version": "4.1.1", @@ -4107,6 +4120,7 @@ "version": "1.4.0", "bundled": true, "dev": true, + "optional": true, "requires": { "wrappy": "1" } @@ -4192,7 +4206,8 @@ "safe-buffer": { "version": "5.1.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "safer-buffer": { "version": "2.1.2", @@ -4228,6 +4243,7 @@ "version": "1.0.2", "bundled": true, "dev": true, + "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -4247,6 +4263,7 @@ "version": "3.0.1", "bundled": true, "dev": true, + "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -4290,12 +4307,14 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "yallist": { "version": "3.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true } } }, diff --git a/src/components/subjects-tree.js b/src/components/subjects-tree.js index 309025c..647a1de 100644 --- a/src/components/subjects-tree.js +++ b/src/components/subjects-tree.js @@ -1,7 +1,5 @@ import React, {Component} from 'react'; import PropTypes from 'prop-types'; -import SavedItems from "./saved-items"; -import ToggleTheme from "./toggle-theme"; import axios from 'axios'; import {sortByNameAscending, toggleDifferentClasses} from '../common/helpers'; @@ -86,6 +84,8 @@ export default class SubjectsTree extends Component { renderSubjectTopics = (subjects, subject) => { sortByNameAscending(subjects[subject]); + console.log(`===== Rendering subject "${subject}" =====`, subjects[subject]); + return subjects[subject].map((topic) => { return (
this.handleTopicClick(e, topic)}> From 15e9987a9e30f5ebdb187017a737fbc4ad402e09 Mon Sep 17 00:00:00 2001 From: Andres Valencia Date: Wed, 6 Nov 2019 00:43:23 -0600 Subject: [PATCH 2/4] Update on algorithm to group topic's subjects --- src/components/subjects-tree.js | 60 ++++++++++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 5 deletions(-) diff --git a/src/components/subjects-tree.js b/src/components/subjects-tree.js index 62369ed..3d6215a 100644 --- a/src/components/subjects-tree.js +++ b/src/components/subjects-tree.js @@ -1,7 +1,7 @@ import React, {Component} from 'react'; import PropTypes from 'prop-types'; -import SavedItems from "./saved-items"; -import ToggleTheme from "./toggle-theme"; +// import SavedItems from "./saved-items"; +// import ToggleTheme from "./toggle-theme"; import SearchBar from './searchbar.js'; import axios from 'axios'; import {sortByNameAscending, toggleDifferentClasses} from '../common/helpers'; @@ -84,12 +84,62 @@ export default class SubjectsTree extends Component { }); } + getTopicSubjects = (data) => { + let subjects = []; + + if (!data) { + return subjects; + } + + let parents = []; + let lastParent = null; + + data.reduce((a, e) => { + let name = e.name; + + let filtered = Object.keys(a).filter((item) => { + return name.startsWith(item); + }); + + a[name] = name in a ? ++a[name] : 0; + + if(filtered.length === 0){ + parents.push(name); + lastParent = name; + + subjects.push({ 'Topic': name, 'TopicData': e, 'Subjects': [] }); + } + + if (lastParent) { + let subject = subjects.filter((item) => { + return item.Topic === lastParent; + })[0]; + + if (subject) { + if (subject.Subjects.filter((obj) => { return obj === e; }).length === 0) { + if (name !== lastParent) { + subject.Subjects.push(e); + } + } + } + } + + return a; + }, {}); + + return subjects; + } + renderSubjectTopics = (subjects, subject) => { - sortByNameAscending(subjects[subject]); + let data = subjects[subject]; + + sortByNameAscending(data); + + let topicSubjects = this.getTopicSubjects(data); - console.log(`===== Rendering subject "${subject}" =====`, subjects[subject]); + console.log(topicSubjects); - return subjects[subject].map((topic) => { + return data.map((topic) => { return (
this.handleTopicClick(e, topic)}> From feccb9901f34a26e47354483ebffad33ed752adc Mon Sep 17 00:00:00 2001 From: Andres Valencia Date: Wed, 6 Nov 2019 01:06:09 -0600 Subject: [PATCH 3/4] Added code to handle nested topics and subjects, pending to fix the CSS --- src/components/subjects-tree.js | 40 ++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/src/components/subjects-tree.js b/src/components/subjects-tree.js index 3d6215a..646d57f 100644 --- a/src/components/subjects-tree.js +++ b/src/components/subjects-tree.js @@ -137,16 +137,44 @@ export default class SubjectsTree extends Component { let topicSubjects = this.getTopicSubjects(data); - console.log(topicSubjects); + //console.log(topicSubjects); - return data.map((topic) => { - return ( -
this.handleTopicClick(e, topic)}> + return topicSubjects.map((item) => { + let divObj =
+ + if (item.Subjects.length > 0) { + let subjectDivs = item.Subjects.map((subjectItem) => { + return ( +
this.handleTopicClick(e, subjectItem)}> + +

{subjectItem.name}

+
+ ) + }) + + divObj =
this.handleTopicClick(e, item.TopicData)}> + +

{item.Topic}

+ {subjectDivs} +
+ } else { + divObj =
this.handleTopicClick(e, item.TopicData)}> -

{topic.name}

+

{item.Topic}

- ) + } + + return divObj; }) + + // return data.map((topic) => { + // return ( + //
this.handleTopicClick(e, topic)}> + // + //

{topic.name}

+ //
+ // ) + // }) } handleTopicClick = (e, topic) => { From 342239f876ae38c877908099c8bc23398ff26f6d Mon Sep 17 00:00:00 2001 From: Andres Valencia Date: Tue, 26 Nov 2019 01:01:12 -0600 Subject: [PATCH 4/4] Removed unused variables --- src/components/subjects-tree.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/components/subjects-tree.js b/src/components/subjects-tree.js index 646d57f..adbdbbd 100644 --- a/src/components/subjects-tree.js +++ b/src/components/subjects-tree.js @@ -91,7 +91,6 @@ export default class SubjectsTree extends Component { return subjects; } - let parents = []; let lastParent = null; data.reduce((a, e) => { @@ -104,7 +103,6 @@ export default class SubjectsTree extends Component { a[name] = name in a ? ++a[name] : 0; if(filtered.length === 0){ - parents.push(name); lastParent = name; subjects.push({ 'Topic': name, 'TopicData': e, 'Subjects': [] }); @@ -125,7 +123,7 @@ export default class SubjectsTree extends Component { } return a; - }, {}); + }, {}); return subjects; }