From 8cef8bfdba5054284269ec8c80f6830837ac25f1 Mon Sep 17 00:00:00 2001 From: Enrico Fasoli Date: Thu, 10 Dec 2015 21:15:26 +0100 Subject: [PATCH] handle transition when the url changes but the page is the same --- webapp/pages/commentpage.jsx | 16 ++++++----- webapp/pages/postpage.jsx | 14 ++++++--- webapp/pages/profile.jsx | 56 ++++++++++++++++++++---------------- 3 files changed, 50 insertions(+), 36 deletions(-) diff --git a/webapp/pages/commentpage.jsx b/webapp/pages/commentpage.jsx index 517e094..34f81c7 100644 --- a/webapp/pages/commentpage.jsx +++ b/webapp/pages/commentpage.jsx @@ -25,14 +25,11 @@ module.exports = function(boardsAPI){ }) }, componentWillReceiveProps: function(nextProps) { - if(nextProps.params.commenthash !== this.props.params.commenthash){ - location.reload() // cheap hack, should swap with something more efficient - } + boardsAPI.use(boards => this.downloadComment(boards,nextProps)) }, - init: function(boards){ - if(this.state.init) return - this.setState({ api: true, boards: boards }) - boards.downloadComment(this.props.params.commenthash,this.props.params.userid,this.props.params.boardname,(err,comment) => { + downloadComment: function(boards,props){ + this.setState({ comment: false }) + boards.downloadComment(props.params.commenthash,props.params.userid,props.params.boardname,(err,comment) => { if(err){ this.setState({ comment: { title: 'Error', text: err.Message || err.Error }}) } else { @@ -40,6 +37,11 @@ module.exports = function(boardsAPI){ } }) }, + init: function(boards){ + if(this.state.init) return + this.setState({ api: true, boards: boards }) + this.downloadComment(boards,this.props) + }, getContext: function(){ if(this.props.params.userid){ if(this.props.params.boardname) diff --git a/webapp/pages/postpage.jsx b/webapp/pages/postpage.jsx index 2133f1c..29fccba 100644 --- a/webapp/pages/postpage.jsx +++ b/webapp/pages/postpage.jsx @@ -23,10 +23,11 @@ module.exports = function(boardsAPI){ } }) }, - init: function(boards){ - if(this.state.init) return - this.setState({ api: true, boards: boards }) - boards.downloadPost(this.props.params.posthash,this.props.params.userid,this.props.params.boardname,this.props.params.userid,(err,post) => { + componentWillReceiveProps: function(nextProps) { + boardsAPI.use(boards => this.downloadPost(boards,nextProps)) + }, + downloadPost: function(boards,props){ + boards.downloadPost(props.params.posthash,props.params.userid,props.params.boardname,props.params.userid,(err,post) => { if(err){ this.setState({ post: { title: 'Error', text: err.Message || err.Error }}) } else { @@ -34,6 +35,11 @@ module.exports = function(boardsAPI){ } }) }, + init: function(boards){ + if(this.state.init) return + this.setState({ api: true, boards: boards }) + this.downloadPost(boards,this.props) + }, getContext: function(){ if(this.props.params.userid){ if(this.props.params.boardname) diff --git a/webapp/pages/profile.jsx b/webapp/pages/profile.jsx index a8ad67d..dc7f59b 100644 --- a/webapp/pages/profile.jsx +++ b/webapp/pages/profile.jsx @@ -13,44 +13,50 @@ module.exports = function(boardsAPI){ boardsAPI.use(boards => { if(boards.isInit){ this.setState({ api: boards, id: boards.id }) - this.init() + this.init(boards) } var ee = boards.getEventEmitter() ee.on('init',err => { if(!err && this.isMounted()){ this.setState({ api: boards, id: boards.id }) - this.init() + this.init(boards) } }) }) }, - init: function(){ - if(this.state.init) return - boardsAPI.use(boards => { - var ee = boards.getEventEmitter() - if(boards.isInit || this.state.api){ - var uid = this.props.params.userid - if(uid === 'me') uid = boards.id - console.log('About to ask for profile for',uid) - ee.on('boards for '+uid,l => { - if(!this.isMounted()) return true - this.setState({ boards: l }) + componentWillReceiveProps: function(nextProps) { + boardsAPI.use(boards => this.downloadProfile(boards,nextProps)) + }, + downloadProfile: function(boards,props){ + var ee = boards.getEventEmitter() + var uid = props.params.userid + if(uid === 'me') uid = boards.id + ee.on('boards for '+uid,l => { + if(!this.isMounted() || props.params.userid !== uid) return true + this.setState({ boards: l }) + }) + boards.getProfile(uid,(err,res) => { + if(!this.isMounted()) return true + if(err){ + this.setState({ + name: , + description: err }) - boards.getProfile(uid,(err,res) => { - if(!this.isMounted()) return true - if(err){ - this.setState({ - name: , - description: err - }) - } else { - this.setState({ name: res.name, description: res.description }) - } - }) - this.setState({ init: true }) + } else { + this.setState({ name: res.name, description: res.description }) } }) }, + init: function(boards){ + if(this.state.init) return + var ee = boards.getEventEmitter() + if(boards.isInit || this.state.api){ + var uid = this.props.params.userid + if(uid === 'me') uid = boards.id + this.downloadProfile(boards,this.props) + this.setState({ init: true }) + } + }, linkToEditor: function(){ var uid = this.props.params.userid if(uid === 'me' && this.state.id) uid = this.state.id