From 77ecd5e75487ff4aeb2b2832b54b08100b91d51e Mon Sep 17 00:00:00 2001 From: cch5ng Date: Tue, 28 Mar 2017 15:48:23 -0700 Subject: [PATCH] fix: #25; cleanup and update render() to get latest saved questions more dynamically --- src/components/Child.js | 107 +++++++++++++--------------------------- 1 file changed, 35 insertions(+), 72 deletions(-) diff --git a/src/components/Child.js b/src/components/Child.js index 5ec2364..5beef15 100644 --- a/src/components/Child.js +++ b/src/components/Child.js @@ -7,6 +7,7 @@ import localforage from 'localforage'; import '../App.css'; import h5bp_interview from '../utilities/h5bp_interview.json'; import { CATEGORIES } from '../utilities/constants'; +import { getCategoriesList, shortCategory } from '../utilities'; class Child extends Component { constructor(props) { @@ -44,85 +45,47 @@ class Child extends Component { componentWillMount() { let that = this; - //test - //window.location.reload(); - //if (this.state.viewState === 'read') { - localforage.getItem(that.listId).then(function(value) { - // This code runs once the value has been loaded - // from the offline store. - console.log(value); - let stateObj = {}; - stateObj.key = that.listId; - stateObj.name = value.name; - stateObj.questions = value.questions; - for (let i = 0; i < value.questions.length; i++) { - let categ = Object.keys(value.questions[i])[0]; - console.log('categ: ' + categ); - console.log('type categ: ' + typeof categ); - console.log('value.questions[i][categ]: ' + value.questions[i][categ]); - stateObj[categ] = value.questions[i][categ] || []; - } - - that.setState(stateObj); - - - //that.setState({ - //name: value.name, - -//maybe make questions into a different format -/*{ - 'category1': [question_string1, question_string2], - 'category2': [question_string1, question_string2] -} -*/ - //questions: value.questions - // questions is [{'category': [question_string1, question_string2]}] - //}); - console.log('this.state.GeneralQuestions: ' + that.state['GeneralQuestions']) - - }).catch(function(err) { - // This code runs if there were any errors - console.log(err); - }); - // } - // if (this.state.viewState === 'edit') { - // console.log('do something for edit view') - // } + localforage.getItem(that.listId).then(function(value) { + let stateObj = {}; + stateObj.key = that.listId; + stateObj.name = value.name; + stateObj.questions = value.questions; + for (let i = 0; i < value.questions.length; i++) { + let categ = Object.keys(value.questions[i])[0]; + stateObj[categ] = value.questions[i][categ] || []; + } + + that.setState(stateObj); + }).catch(function(err) { + // This code runs if there were any errors + console.log(err); + }); } render() { var that = this; var category; - var questions = []; - if (this.state.questions) { - var questionsList = this.state.questions.map(function(questionObj, idx) { - for (let key in questionObj) { - category = key; - //console.log('category: ' + category); - questions = questionObj[key]; - //console.log('questions.length: ' + questions.length); - } - - if (questions.length) { - return ( -
-

{category}

- -
- ) - } else { + let categoriesList = getCategoriesList(); + + var questionsList = categoriesList.map((categ) => { + if (this.state[shortCategory(categ)].length) { + return ( +
+

{categ}

+ +
+ ) + } else { return null - } - }); - } + } + }) -//this.state.questions && return (

{this.state.name} List