use ws events on post list

This commit is contained in:
SrGooglo 2023-03-03 19:27:59 +00:00
parent bd86e2cec3
commit 99347bb42a
3 changed files with 48 additions and 59 deletions

View File

@ -27,10 +27,30 @@ export default class PostsLists extends React.Component {
state = { state = {
currentIndex: 0, currentIndex: 0,
openPost: null, openPost: null,
list: this.props.list ?? [],
} }
listRef = React.createRef() listRef = React.createRef()
timelineWsEvents = {
"post.new": (data) => {
console.log("New post => ", data)
this.setState({
list: [data, ...this.state.list],
})
},
"post.delete": (id) => {
console.log("Deleted post => ", id)
this.setState({
list: this.state.list.filter((post) => {
return post._id !== id
}),
})
}
}
componentDidMount = async () => { componentDidMount = async () => {
window.app.shortcuts.register({ window.app.shortcuts.register({
id: "postsFeed.scrollUp", id: "postsFeed.scrollUp",
@ -46,11 +66,32 @@ export default class PostsLists extends React.Component {
}, (event) => { }, (event) => {
this.scrollDown() this.scrollDown()
}) })
if (this.props.watchTimeline) {
Object.entries(this.timelineWsEvents).forEach(([event, callback]) => {
app.cores.api.listenEvent(event, callback)
})
}
} }
componentWillUnmount = async () => { componentWillUnmount = async () => {
window.app.shortcuts.remove("postsFeed.scrollUp") window.app.shortcuts.remove("postsFeed.scrollUp")
window.app.shortcuts.remove("postsFeed.scrollDown") window.app.shortcuts.remove("postsFeed.scrollDown")
if (this.props.watchTimeline) {
Object.entries(this.timelineWsEvents).forEach(([event, callback]) => {
app.cores.api.unlistenEvent(event, callback)
})
}
}
// watch if props.list has changed and update state.list
componentDidUpdate = async (prevProps) => {
if (prevProps.list !== this.props.list) {
this.setState({
list: this.props.list,
})
}
} }
scrollUp = () => { scrollUp = () => {
@ -128,7 +169,7 @@ export default class PostsLists extends React.Component {
} }
render() { render() {
if (this.props.posts.length === 0) { if (this.state.list.length === 0) {
if (typeof this.props.emptyListRender === "function") { if (typeof this.props.emptyListRender === "function") {
return React.createElement(this.props.emptyListRender) return React.createElement(this.props.emptyListRender)
} }
@ -154,7 +195,9 @@ export default class PostsLists extends React.Component {
fetching={this.props.loading} fetching={this.props.loading}
> >
{ {
this.props.posts.map((post, index) => { this.state.list.map((post, index) => {
console.log("Post => ", post, index)
return <PostCard return <PostCard
key={index} key={index}
data={post} data={post}

View File

@ -17,21 +17,6 @@ export default class ExplorePosts extends React.Component {
filledSearcher: false, filledSearcher: false,
} }
wsEvents = {
"post.new": async (data) => {
this.setState({
posts: [data, ...this.state.posts],
})
},
"post.delete": async (id) => {
this.setState({
posts: this.state.posts.filter((post) => {
return post._id !== id
}),
})
}
}
loadPosts = async ({ loadPosts = async ({
trim, trim,
replace = false replace = false
@ -90,16 +75,6 @@ export default class ExplorePosts extends React.Component {
componentDidMount = async () => { componentDidMount = async () => {
await this.loadPosts() await this.loadPosts()
Object.keys(this.wsEvents).forEach((event) => {
app.cores.api.listenEvent(event, this.wsEvents[event])
})
}
componentWillUnmount = async () => {
Object.keys(this.wsEvents).forEach((event) => {
app.cores.api.unlistenEvent(event, this.wsEvents[event])
})
} }
render() { render() {
@ -118,7 +93,7 @@ export default class ExplorePosts extends React.Component {
loading={this.state.loading} loading={this.state.loading}
hasMorePosts={this.state.hasMorePosts} hasMorePosts={this.state.hasMorePosts}
onLoadMore={this.loadPosts} onLoadMore={this.loadPosts}
posts={this.state.posts} list={this.state.posts}
/> />
} }
</div> </div>

View File

@ -27,21 +27,6 @@ export default class Feed extends React.Component {
posts: [], posts: [],
} }
wsEvents = {
"post.new": (data) => {
this.setState({
posts: [data, ...this.state.posts],
})
},
"post.delete": (id) => {
this.setState({
posts: this.state.posts.filter((post) => {
return post._id !== id
}),
})
}
}
loadData = async ({ loadData = async ({
trim, trim,
replace = false replace = false
@ -86,18 +71,6 @@ export default class Feed extends React.Component {
componentDidMount = async () => { componentDidMount = async () => {
await this.loadData() await this.loadData()
console.log(this.wsEvents)
Object.keys(this.wsEvents).forEach((event) => {
app.cores.api.listenEvent(event, this.wsEvents[event])
})
}
componentWillUnmount = async () => {
Object.keys(this.wsEvents).forEach((event) => {
app.cores.api.unlistenEvent(event, this.wsEvents[event])
})
} }
render() { render() {
@ -108,10 +81,8 @@ export default class Feed extends React.Component {
hasMorePosts={this.state.hasMorePosts} hasMorePosts={this.state.hasMorePosts}
emptyListRender={emptyListRender} emptyListRender={emptyListRender}
onLoadMore={this.loadData} onLoadMore={this.loadData}
posts={this.state.posts} list={this.state.posts}
{ watchTimeline
...this.props
}
/> />
} }
</div> </div>