From 05c8dd7974635a2ffd4963964f39eeb565577514 Mon Sep 17 00:00:00 2001 From: yichiang Date: Sun, 21 May 2017 13:36:14 -0700 Subject: [PATCH 1/3] add code --- src/App.js | 28 +++++++++++++++++++++++++++- src/List.js | 2 ++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/App.js b/src/App.js index 9940025..8e3ccba 100644 --- a/src/App.js +++ b/src/App.js @@ -13,7 +13,7 @@ class App extends Component { bookVal: '', selectedBook: null } - state = { tagFilters: [], selectedBook: null } + // state = { tagFilters: [], selectedBook: null } handleItemClick = (e, { name }) => this.setState({ activeItem: name }) constructor(props) { @@ -24,6 +24,7 @@ class App extends Component { this.changeBook = this.changeBook.bind(this); this._r = this._r.bind(this); this._nullFunc = this._nullFunc.bind(this); + // this.popularActive = this.popularActive.bind(this); } componentDidMount() { @@ -143,6 +144,25 @@ class App extends Component { }); } + const popularActive = (news) => { + var results = {}; + for (var i = 0; i < news.length; i++) { + const everyTagsOnNews = news[i].tags || []; + for (var j = 0; j < everyTagsOnNews.length; j++) { + if(results[everyTagsOnNews[j]]){ + results[everyTagsOnNews[j]]++ ; + }else{ + results[everyTagsOnNews[j]] = 1; + } + } + } + console.log("result", results); + const tempResult = Object.keys(results).map((key) => ({count: results[key], value: key})) || []; + console.log("tempResult", tempResult); + + return tempResult; + } + return (
@@ -175,6 +195,12 @@ class App extends Component { + + {/* {popularActive(this.props.data.news).map((x, tempIndex)=> + + )} */} Date: Mon, 22 May 2017 22:44:48 -0700 Subject: [PATCH 2/3] recover checbox feature --- src/BookTags.js | 17 ++++---- src/FirebaseService.js | 35 +++++++++++++++++ src/IconBar.js | 4 -- src/List.js | 66 ++++++------------------------- src/ListCheckbox.js | 85 ++++++++++++++++++++++++++++++++++++++++ src/MainPage.css | 3 +- src/MainPage.js | 8 ++-- src/NewsComment.js | 17 ++------ src/Tags.js | 7 ++-- src/index.js | 88 ++++++++++++++---------------------------- src/routes.js | 18 +-------- 11 files changed, 181 insertions(+), 167 deletions(-) create mode 100644 src/FirebaseService.js create mode 100644 src/ListCheckbox.js diff --git a/src/BookTags.js b/src/BookTags.js index b7deccb..2be88d4 100644 --- a/src/BookTags.js +++ b/src/BookTags.js @@ -31,14 +31,11 @@ class BookTags extends Component { } updateNewsAPI(newTags){ - console.log("update news", newTags, this.props.updateTags ) if(this.props.updateTags){ if(this.props.itemPropsName === 'tags'){ const newNews = Object.assign({}, this.props.item, {tags: newTags}); this.props.updateTags(this.props.item._id, newNews ); }else if(this.props.itemPropsName === 'userBooks'){ - console.log("save userbooks", newTags); - //Save array of ID to userbooks const newUserbooks = newTags.map(x=>{ const results = this.props.options.filter(y => y.fullName === x); if(results.length > 0){ @@ -134,7 +131,7 @@ class BookTags extends Component { if (availTags.length > 0) { let newTags = currentTags.concat(availTags[Math.floor(Math.random()*availTags.length)]); alert(`Brefing book suggestions: ${newTags}`); - } + } } @@ -163,12 +160,12 @@ class BookTags extends Component { value={this.state.value} {...this.props} />} -
} /> + + } /> ) class Tags extends Component { @@ -46,7 +48,6 @@ class Tags extends Component { return null; } }) - const newNews = Object.assign({}, this.props.item, {userBooks: newUserbooks}); this.props.updateTags(this.props.item._id, newNews ); } diff --git a/src/index.js b/src/index.js index 3e306b5..ab164d9 100644 --- a/src/index.js +++ b/src/index.js @@ -1,69 +1,37 @@ import React from 'react'; import ReactDOM from 'react-dom'; -import App from './App'; import registerServiceWorker from './registerServiceWorker'; -import './index.css'; -import * as firebase from 'firebase'; +import { BrowserRouter as Router, Route, Link } from 'react-router-dom' import Home from './Home'; import MainPage from './MainPage'; - -import { - BrowserRouter as Router, - Route, - Link -} from 'react-router-dom' -const config = { - apiKey: "AIzaSyA5Fh7tb7DREA5fgAKwJjh_llXfIKs9iMs", - authDomain: "my-test-website-c0fcc.firebaseapp.com", - databaseURL: "https://my-test-website-c0fcc.firebaseio.com", - projectId: "my-test-website-c0fcc", - storageBucket: "my-test-website-c0fcc.appspot.com", - messagingSenderId: "575661525828" - }; - -const fb = firebase - .initializeApp(config) - .database() - .ref(); - - //News API - const getNewsbyId = id => fb.child('news').child(id); - const addNews = data => fb.child('news').push(data, response => response); - const adduserArticle = data => fb.child("userArticle").push(data, response => response); - // this.bindAsArray(fb.child("userArticle"), "userArticle"); - const removeUserArticle = (id) => fb.child("userArticle").child(id).remove(); - - const updateNews = (id, data) => fb.child(`news/${id}`).update(data, response => response); - export const newsActions = { - addNews, - updateNews, - getNewsbyId, - adduserArticle, - removeUserArticle - }; - //User API - const getUsersbyId = id => fb.child('users').child(id); - const updateUserbyID = (id, data) => fb.child(`users/${id}`).update(data, response => response); - export const userActions = { - getUsersbyId, - updateUserbyID, - }; - - fb.on('value', snapshot => { - const store = snapshot.val(); - - console.log("index.js", store) - ReactDOM.render( - -
- } /> - - } /> -
-
, - document.getElementById('root') - ); +import App from './App'; +import './index.css'; +import { database, newsActions, userActions} from './FirebaseService'; + + + database.ref('news').on('value', snapshot => { + const newsStore = snapshot.val(); + database.ref('users').on('value', snapshot => { + const usersStore = snapshot.val(); + database.ref('userBooks').on('value', snapshot => { + const userBooksStore = snapshot.val(); + const store = Object.assign({}, {news:newsStore}, {users: usersStore}, {userBooks: userBooksStore}); + console.log("index.js", store) + ReactDOM.render( + +
+ } /> + + } /> +
+
, + document.getElementById('root') + ); + }); + + }); }); + registerServiceWorker(); diff --git a/src/routes.js b/src/routes.js index cbea1fb..d20dde6 100644 --- a/src/routes.js +++ b/src/routes.js @@ -1,24 +1,8 @@ import React from 'react'; -import {Route, IndexRoute} from 'react-router'; +import { Route, IndexRoute } from 'react-router'; import App from './App'; import Home from './Home'; import MainPage from './MainPage'; -// import * as firebase from 'firebase'; -// -// -// const config = { -// apiKey: "AIzaSyA5Fh7tb7DREA5fgAKwJjh_llXfIKs9iMs", -// authDomain: "my-test-website-c0fcc.firebaseapp.com", -// databaseURL: "https://my-test-website-c0fcc.firebaseio.com", -// projectId: "my-test-website-c0fcc", -// storageBucket: "my-test-website-c0fcc.appspot.com", -// messagingSenderId: "575661525828" -// }; -// -// const fb = firebase -// .initializeApp(config) -// .database() -// .ref(); export default ( From a296214bba72a73e78b82e7d60d78fda3d51262a Mon Sep 17 00:00:00 2001 From: yichiang Date: Mon, 22 May 2017 22:58:22 -0700 Subject: [PATCH 3/3] clear code --- src/IconBar.js | 1 - src/List.js | 13 +++++-------- src/ListCheckbox.js | 9 ++++----- src/MainPage.js | 5 ++--- src/NewsComment.js | 8 ++++---- src/index.js | 4 +--- 6 files changed, 16 insertions(+), 24 deletions(-) diff --git a/src/IconBar.js b/src/IconBar.js index 6f8e74f..87abb31 100644 --- a/src/IconBar.js +++ b/src/IconBar.js @@ -25,7 +25,6 @@ class IconBar extends Component { - ); } } diff --git a/src/List.js b/src/List.js index d4e4c37..287de93 100644 --- a/src/List.js +++ b/src/List.js @@ -39,9 +39,10 @@ class List extends Component { } render() { - const { items, newsActionsTemp } = this.props; + const { items, newsActionsTemp, userBooks, sentDisabled, activeItem, selectedBook } = this.props; + let feedback = null; - if (this.props.activeItem === 'sent') { + if (activeItem === 'sent') { let options = [ { key: 1, @@ -67,10 +68,6 @@ class List extends Component { ); } - const userBooks = this.props.userBooks; - - let sentDisabled = this.props.selectDisabled; - return ( {items.map((d,i) => { @@ -87,11 +84,11 @@ class List extends Component { - {(this.props.selectedBook&& d.userBooks.indexOf(this.props.selectedBook) > -1 ) && + {(selectedBook && d.userBooks.indexOf(selectedBook) > -1 ) && diff --git a/src/ListCheckbox.js b/src/ListCheckbox.js index 7af0399..9961314 100644 --- a/src/ListCheckbox.js +++ b/src/ListCheckbox.js @@ -69,15 +69,14 @@ class ListCheckbox extends Component { render() { - let items = this.props.items; - const { userArticleData } = this.state; + const { items, news, userBooks } = this.props; + const { userArticleData, checked } = this.state; let userArticle = userArticleData ? Object.keys(userArticleData).map(key => userArticleData[key]) : []; - const userBooks = this.props.userBooks; return ( this.checkBooked(e, data ,this.props.news)} /> + onChange={(e,data) => this.checkBooked(e, data, this.props.news)} /> ); } } diff --git a/src/MainPage.js b/src/MainPage.js index 5a72fd7..54bb758 100644 --- a/src/MainPage.js +++ b/src/MainPage.js @@ -23,7 +23,6 @@ class MainPage extends Component { } render() { - console.log("props", this.props); const news = this.state.newsData; return ( @@ -31,8 +30,8 @@ class MainPage extends Component {
-
{news? news.source : "No new was found"}
-
{news? moment(news.publishedAt).fromNow() : "No new was found"}
+
{news ? news.source : "No new was found"}
+
{news ? moment(news.publishedAt).fromNow() : "No new was found"}
{news && }

{news ? news.title : "No new was found"}

diff --git a/src/NewsComment.js b/src/NewsComment.js index fc280b8..dd09b85 100644 --- a/src/NewsComment.js +++ b/src/NewsComment.js @@ -47,13 +47,13 @@ class NewsComment extends Component { }); } render() { - const { item, updateNews } = this.props; - const usersOption = this.props.users.map(z=> ({key: z._id, value: z._id, text: z.name})); + const { item, updateNews, users } = this.props; + const usersOption = users.map(z=> ({key: z._id, value: z._id, text: z.name})); return (
Comments
- {item.comments&& item.comments.map(x =>{ - const userTemp = this.props.users.filter(y => y._id === x.userId)[0]; + { item.comments && item.comments.map(x =>{ + const userTemp = users.filter(y => y._id === x.userId)[0]; return ( diff --git a/src/index.js b/src/index.js index ab164d9..a3f24c1 100644 --- a/src/index.js +++ b/src/index.js @@ -2,13 +2,12 @@ import React from 'react'; import ReactDOM from 'react-dom'; import registerServiceWorker from './registerServiceWorker'; import { BrowserRouter as Router, Route, Link } from 'react-router-dom' - +import { database, newsActions, userActions} from './FirebaseService'; import Home from './Home'; import MainPage from './MainPage'; import App from './App'; import './index.css'; -import { database, newsActions, userActions} from './FirebaseService'; database.ref('news').on('value', snapshot => { @@ -30,7 +29,6 @@ import { database, newsActions, userActions} from './FirebaseService'; document.getElementById('root') ); }); - }); });