1
0
mirror of https://github.com/fazo96/ipfs-boards synced 2025-01-25 14:54:19 +01:00

handle transition when the url changes but the page is the same

This commit is contained in:
Enrico Fasoli 2015-12-10 21:15:26 +01:00
parent 997304844a
commit 8cef8bfdba
3 changed files with 50 additions and 36 deletions

View File

@ -25,14 +25,11 @@ module.exports = function(boardsAPI){
}) })
}, },
componentWillReceiveProps: function(nextProps) { componentWillReceiveProps: function(nextProps) {
if(nextProps.params.commenthash !== this.props.params.commenthash){ boardsAPI.use(boards => this.downloadComment(boards,nextProps))
location.reload() // cheap hack, should swap with something more efficient
}
}, },
init: function(boards){ downloadComment: function(boards,props){
if(this.state.init) return this.setState({ comment: false })
this.setState({ api: true, boards: boards }) boards.downloadComment(props.params.commenthash,props.params.userid,props.params.boardname,(err,comment) => {
boards.downloadComment(this.props.params.commenthash,this.props.params.userid,this.props.params.boardname,(err,comment) => {
if(err){ if(err){
this.setState({ comment: { title: 'Error', text: err.Message || err.Error }}) this.setState({ comment: { title: 'Error', text: err.Message || err.Error }})
} else { } 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(){ getContext: function(){
if(this.props.params.userid){ if(this.props.params.userid){
if(this.props.params.boardname) if(this.props.params.boardname)

View File

@ -23,10 +23,11 @@ module.exports = function(boardsAPI){
} }
}) })
}, },
init: function(boards){ componentWillReceiveProps: function(nextProps) {
if(this.state.init) return boardsAPI.use(boards => this.downloadPost(boards,nextProps))
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) => { downloadPost: function(boards,props){
boards.downloadPost(props.params.posthash,props.params.userid,props.params.boardname,props.params.userid,(err,post) => {
if(err){ if(err){
this.setState({ post: { title: 'Error', text: err.Message || err.Error }}) this.setState({ post: { title: 'Error', text: err.Message || err.Error }})
} else { } 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(){ getContext: function(){
if(this.props.params.userid){ if(this.props.params.userid){
if(this.props.params.boardname) if(this.props.params.boardname)

View File

@ -13,44 +13,50 @@ module.exports = function(boardsAPI){
boardsAPI.use(boards => { boardsAPI.use(boards => {
if(boards.isInit){ if(boards.isInit){
this.setState({ api: boards, id: boards.id }) this.setState({ api: boards, id: boards.id })
this.init() this.init(boards)
} }
var ee = boards.getEventEmitter() var ee = boards.getEventEmitter()
ee.on('init',err => { ee.on('init',err => {
if(!err && this.isMounted()){ if(!err && this.isMounted()){
this.setState({ api: boards, id: boards.id }) this.setState({ api: boards, id: boards.id })
this.init() this.init(boards)
} }
}) })
}) })
}, },
init: function(){ componentWillReceiveProps: function(nextProps) {
if(this.state.init) return boardsAPI.use(boards => this.downloadProfile(boards,nextProps))
boardsAPI.use(boards => { },
var ee = boards.getEventEmitter() downloadProfile: function(boards,props){
if(boards.isInit || this.state.api){ var ee = boards.getEventEmitter()
var uid = this.props.params.userid var uid = props.params.userid
if(uid === 'me') uid = boards.id if(uid === 'me') uid = boards.id
console.log('About to ask for profile for',uid) ee.on('boards for '+uid,l => {
ee.on('boards for '+uid,l => { if(!this.isMounted() || props.params.userid !== uid) return true
if(!this.isMounted()) return true this.setState({ boards: l })
this.setState({ boards: l }) })
boards.getProfile(uid,(err,res) => {
if(!this.isMounted()) return true
if(err){
this.setState({
name: <Icon name="ban" />,
description: err
}) })
boards.getProfile(uid,(err,res) => { } else {
if(!this.isMounted()) return true this.setState({ name: res.name, description: res.description })
if(err){
this.setState({
name: <Icon name="ban" />,
description: err
})
} else {
this.setState({ name: res.name, description: res.description })
}
})
this.setState({ init: true })
} }
}) })
}, },
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(){ linkToEditor: function(){
var uid = this.props.params.userid var uid = this.props.params.userid
if(uid === 'me' && this.state.id) uid = this.state.id if(uid === 'me' && this.state.id) uid = this.state.id