From e55733c9512980f37ba09f4ca3e85f1106a24377 Mon Sep 17 00:00:00 2001 From: Nishant Arora Date: Fri, 30 Dec 2016 18:21:12 +0530 Subject: [PATCH] Added article history view page --- api/archives.js | 58 +++++++++++++++++++++ client/app/index.jsx | 2 + client/app/static/article.jsx | 1 + client/app/static/browse_archives.jsx | 75 +++++++++++++++++++++++++++ client/app/static/history.jsx | 38 ++++++++++++++ client/app/static/simple_article.jsx | 70 +++++++++++++++++++++++++ index.js | 3 ++ 7 files changed, 247 insertions(+) create mode 100644 api/archives.js create mode 100644 client/app/static/browse_archives.jsx create mode 100644 client/app/static/history.jsx create mode 100644 client/app/static/simple_article.jsx diff --git a/api/archives.js b/api/archives.js new file mode 100644 index 0000000..263f6cc --- /dev/null +++ b/api/archives.js @@ -0,0 +1,58 @@ +/* +This file contains all endpoints related to archives +*/ + +var Articles = require('../models/article.js'); +var Topics = require('../models/topic.js'); +var Archives = require('../models/archive.js'); +var Users = require('../models/user.js'); + +var db = require('../db.js'); //this file contains the knex file import. it's equal to knex=require('knex') + +module.exports = function(app){ + app.get('/archives/:id/',function(req,res){ + /* + This is a GET endpoint that responds with one article of the specific ID (identified through the ID param) + the article is present in the data object in the returning object. + the error key in the returning object is a boolen which is false if there is no error and true otherwise + */ + Archives.forge({id: req.params.id}) + .fetch() + .then(function (archive) { + console.log("ARCHIVE================================="); + console.log(archive); + Users.forge({id: archive.attributes.user_id}).fetch().then(function(user){ + archiveObj = archive.toJSON(); + userObj = user.toJSON(); + archiveObj.user = { + id: userObj.id, + name: userObj.name, + email: userObj.email, + about: userObj.about + }; + }) + .then(function(){ + res.json({ + error: { + error: false, + message: '' + }, + code: 'B113', + data: archiveObj + }); + }) + }) + .catch(function (error) { + res.status(500).json({ + error: { + error: true, + message: error.message + }, + code: 'B114', + data: { + + } + }); + }); + }); +} diff --git a/client/app/index.jsx b/client/app/index.jsx index dc233e9..5694ecf 100644 --- a/client/app/index.jsx +++ b/client/app/index.jsx @@ -10,6 +10,7 @@ import Product from './static/product.jsx'; import Article from './static/article.jsx'; import NewArticle from './static/new.jsx'; import EditArticle from './static/edit.jsx'; +import ArticleHistory from './static/history.jsx'; render( @@ -21,6 +22,7 @@ render( + diff --git a/client/app/static/article.jsx b/client/app/static/article.jsx index efe4ff2..20e9c24 100644 --- a/client/app/static/article.jsx +++ b/client/app/static/article.jsx @@ -85,6 +85,7 @@ class ViewArticle extends React.Component {

{this.state.article.user.about}

Edit + History diff --git a/client/app/static/browse_archives.jsx b/client/app/static/browse_archives.jsx new file mode 100644 index 0000000..f69ba19 --- /dev/null +++ b/client/app/static/browse_archives.jsx @@ -0,0 +1,75 @@ +import React from 'react'; +import Error from './error.jsx'; +import Loader from './loader.jsx'; +import {hashHistory} from 'react-router'; + +class BrowseArchives extends React.Component { + constructor(props) { + super(props); + this.archiveSelect = this.archiveSelect.bind(this); + this.state = {error: "", archives: []}; + } + + componentDidMount() { + console.log("Component Mounted!"); + var myHeaders = new Headers({ + "Content-Type": "application/x-www-form-urlencoded", + "x-access-token": localStorage.getItem('userToken') + }); + var myInit = { method: 'GET', + headers: myHeaders, + }; + var that = this; + var url = '/api/articles/'+this.props.articleId+'/history'; + fetch(url,myInit) + .then(function(response) { + console.log(response); + return response.json(); + }) + .then(function(response) { + if(response.error.error) + that.setState({error: response.error.message}) + else { + that.setState({archives: response.data}) + console.log(that.state.archives); + } + console.log(response); + }); + } + + archiveSelect(id,e) { + e.preventDefault(); + this.props.archiveChange(id) + } + + + render () { + if(this.state.error) { + return + } + if(this.state.archives.length<1) { + return ; + } + else { + return( +
+
+ +
+
+
+ +
); + } + } +} + +export default BrowseArchives; diff --git a/client/app/static/history.jsx b/client/app/static/history.jsx new file mode 100644 index 0000000..ddc7595 --- /dev/null +++ b/client/app/static/history.jsx @@ -0,0 +1,38 @@ +import React from 'react'; +import {Link} from 'react-router'; +import Loader from './loader.jsx'; +import Error from './error.jsx'; +import BrowseArchives from './browse_archives.jsx'; +import SimpleArticle from './simple_article.jsx'; +var Remarkable = require('remarkable'); + +class ArticleHistory extends React.Component { + + constructor(props) { + super(props); + this.archiveUpdate = this.archiveUpdate.bind(this); + this.state = {archive_id: ""}; + } + + + archiveUpdate(id) { + this.setState({archive_id: id}); + } + + render () { + return( +
+
+ + +
+
+ + +
+
+ ); + } +} + +export default ArticleHistory; diff --git a/client/app/static/simple_article.jsx b/client/app/static/simple_article.jsx new file mode 100644 index 0000000..4c49f25 --- /dev/null +++ b/client/app/static/simple_article.jsx @@ -0,0 +1,70 @@ +import React from 'react'; +import {Link} from 'react-router'; +import Loader from './loader.jsx'; +import Error from './error.jsx'; +var Remarkable = require('remarkable'); + +class SimpleArticle extends React.Component { + constructor(props) { + super(props); + this.state = {error: "", article: {}}; + } + + componentWillReceiveProps(nextProps){ + var myHeaders = new Headers({ + "Content-Type": "application/x-www-form-urlencoded", + "x-access-token": localStorage.getItem('userToken') + }); + var myInit = { method: 'GET', + headers: myHeaders, + }; + var that = this; + fetch('/api/archives/'+nextProps.archiveId,myInit) + .then(function(response) { + console.log(response); + return response.json(); + }) + .then(function(response) { + if(response.error.error) + that.setState({error: response.error.message}) + else { + that.setState({article: response.data}) + } + console.log(response); + }); + } + + getRawMarkupBody() { + var md = new Remarkable(); + return { __html: md.render(this.state.article.body) }; + } + + + render () { + if(this.state.error) { + return + } + if(this.state.article && this.state.article.user) { + return(
+
+
+

{this.state.article.title} +

+
+ Edited by {this.state.article.user.name} +
+
+
+
+
+
+ ); + } + else { + return

Please select the archive

; + } + } +} + +export default SimpleArticle; diff --git a/index.js b/index.js index f3bbee6..20bf3ab 100644 --- a/index.js +++ b/index.js @@ -83,6 +83,9 @@ require('./api/topics')(apiRoutes); // Importing all endpoints for users require('./api/users')(apiRoutes); +// Importing all endpoints for archives +require('./api/archives')(apiRoutes); + app.use('/api', apiRoutes); app.use(express.static(__dirname + '/client'));