import React from "react"
import * as antd from "antd"
import { Icons } from "components/Icons"
import { LikeButton } from "components"
import moment from "moment"
import classnames from "classnames"
import { User } from "models"
import "./index.less"
function PostHeader(props) {
const [timeAgo, setTimeAgo] = React.useState(0)
const updateTimeAgo = () => {
setTimeAgo(moment(props.postData.created_at ?? "").fromNow())
}
React.useEffect(() => {
updateTimeAgo()
const interval = setInterval(() => {
updateTimeAgo()
}, 10000)
return () => {
clearInterval(interval)
}
}, [props.postData.created_at])
return
{props.postData.user?.fullName ?? `@${props.postData.user?.username}`}
{timeAgo}
}
function PostContent({ message }) {
return
{message}
}
function PostActions(props) {
return
}
export default class PostCard extends React.Component {
state = {
loading: true,
selfId: null,
data: this.props.data,
}
api = window.app.request
componentDidMount = async () => {
const selfId = await User.selfUserId()
window.app.ws.listen(`like.post.${this.props.data._id}`, async (data) => {
await this.setState({ data })
})
window.app.ws.listen(`unlike.post.${this.props.data._id}`, async (data) => {
await this.setState({ data })
})
await this.setState({
selfId,
loading: false
})
}
onClickLike = async (to) => {
let result = false
if (to) {
const apiResult = await await this.api.put.like({ post_id: this.props.data._id })
result = apiResult.success
} else {
const apiResult = await await this.api.put.unlike({ post_id: this.props.data._id })
result = apiResult.success
}
return result
}
onClickSave = async () => {
// TODO: save post
}
hasLiked = () => {
return this.state.data.likes.some(user_id => user_id === this.state.selfId)
}
isSelf = () => {
return this.state.selfId === this.state.data.user._id
}
render() {
const hasLiked = this.hasLiked()
if (this.state.loading) {
return
}
return
this.onClickLike(false)}
onClickSave={this.onClickSave}
likes={this.state.data.likes.length}
comments={this.state.data.comments.length}
/>
}
}