You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
168 lines
6.2 KiB
168 lines
6.2 KiB
import React, { FC, useEffect } from 'react'
|
|
import { useSelector, useDispatch } from 'react-redux'
|
|
import { useParams, useHistory } from 'react-router-dom'
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
|
|
import { faUserPlus, faUserMinus, faUserClock, faBan } from '@fortawesome/free-solid-svg-icons'
|
|
import moment from 'moment'
|
|
|
|
import { handleApiError } from 'src/api/errors'
|
|
import { fetchUser, subscribe, unsubscribe } from 'src/actions/users'
|
|
import { fetchUserPosts } from 'src/actions/posts'
|
|
import { getEntity } from 'src/selectors/entities'
|
|
import { getAuthenticatedUser, getChecked } from 'src/selectors/authentication'
|
|
import { getUserPosts } from 'src/selectors/posts'
|
|
|
|
import { useDeepCompareEffect, useConfig, useTheme } from 'src/hooks'
|
|
import { setTitle, urlForBlob } from 'src/utils'
|
|
import { AppState, EntityType, User, Post, AppThunkDispatch, LevelItem } from 'src/types'
|
|
|
|
import Title from 'src/components/title'
|
|
import Subtitle from 'src/components/subtitle'
|
|
import Level from 'src/components/level'
|
|
import PostList from 'src/components/post-list'
|
|
import Section from 'src/components/section'
|
|
import HorizontalRule from 'src/components/horizontal-rule'
|
|
import Loading from 'src/components/pages/loading'
|
|
|
|
interface Params {
|
|
id: string
|
|
}
|
|
|
|
const ViewUser: FC = () => {
|
|
const { id } = useParams<Params>()
|
|
const theme = useTheme()
|
|
const checked = useSelector<AppState, boolean>(getChecked)
|
|
const self = useSelector<AppState, User | undefined>(getAuthenticatedUser)
|
|
const user = useSelector<AppState, User | undefined>(state => getEntity<User>(state, EntityType.User, id))
|
|
const posts = useSelector<AppState, Post[]>(state => getUserPosts(state, id))
|
|
const dispatch = useDispatch<AppThunkDispatch>()
|
|
const config = useConfig()
|
|
const history = useHistory()
|
|
|
|
useEffect(() => {
|
|
const init = async () => {
|
|
try {
|
|
await dispatch(fetchUser(id))
|
|
await dispatch(fetchUserPosts(id))
|
|
} catch (err) {
|
|
handleApiError(err, dispatch, history)
|
|
}
|
|
}
|
|
|
|
if (checked) init()
|
|
}, [checked])
|
|
|
|
useDeepCompareEffect(() => {
|
|
if (user) setTitle(user.name)
|
|
}, [user])
|
|
|
|
if (!user) return <Loading />
|
|
|
|
const isSelf = self && self.id === user.id
|
|
const isGroup = self && self.group && user.group && self.group.id === user.group.id
|
|
const imageUrl = user.imageUrl ? urlForBlob(config, user.imageUrl) : undefined
|
|
const coverImageUrl = user.coverImageUrl ? urlForBlob(config, user.coverImageUrl) : undefined
|
|
const subscription = self && user.subscriptions ? user.subscriptions.find(subscription => subscription.from === self.id && subscription.to === user.id) : undefined
|
|
const subscribed = subscription && !subscription.pending
|
|
const subscriptionPending = subscription && subscription.pending
|
|
|
|
const items: LevelItem[] = []
|
|
items.push({
|
|
label: 'Posts',
|
|
content: user.posts,
|
|
})
|
|
items.push({
|
|
label: 'Awards',
|
|
content: user.awards,
|
|
})
|
|
items.push({
|
|
label: 'Points',
|
|
content: user.points,
|
|
})
|
|
items.push({
|
|
label: 'Joined',
|
|
content: moment(user.created).format('MMMM Do, YYYY'),
|
|
})
|
|
|
|
return (
|
|
<div>
|
|
<Section>
|
|
{coverImageUrl &&
|
|
<div className="cover-image">
|
|
<img src={coverImageUrl} />
|
|
</div>
|
|
}
|
|
|
|
<div className="header">
|
|
{imageUrl &&
|
|
<div className="image">
|
|
<img src={imageUrl} style={{ width: 128 }} />
|
|
</div>
|
|
}
|
|
<div>
|
|
<Title>{user.name}</Title>
|
|
<p style={{ color: theme.text }}>{user.about}</p>
|
|
</div>
|
|
</div>
|
|
|
|
<Level items={items} />
|
|
<HorizontalRule />
|
|
|
|
<div className="buttons">
|
|
{subscribed &&
|
|
<button className="button is-danger" onClick={() => dispatch(unsubscribe(user.id))}>
|
|
<span className="icon is-small">
|
|
<FontAwesomeIcon icon={faUserMinus} />
|
|
</span>
|
|
<span>Unsusbcribe</span>
|
|
</button>
|
|
}
|
|
|
|
{subscriptionPending &&
|
|
<button className="button is-warning">
|
|
<span className="icon is-small">
|
|
<FontAwesomeIcon icon={faUserClock} />
|
|
</span>
|
|
<span>Pending</span>
|
|
</button>
|
|
}
|
|
|
|
{self && !isSelf && !subscribed && !subscriptionPending &&
|
|
<button className="button is-success" onClick={() => dispatch(subscribe(user.id))}>
|
|
<span className="icon is-small">
|
|
<FontAwesomeIcon icon={faUserPlus} />
|
|
</span>
|
|
<span>Subscribe</span>
|
|
</button>
|
|
}
|
|
|
|
{!isSelf &&
|
|
<button className="button is-danger">
|
|
<span className="icon is-small">
|
|
<FontAwesomeIcon icon={faBan} />
|
|
</span>
|
|
<span>Block</span>
|
|
</button>
|
|
}
|
|
|
|
{user.group && !isGroup &&
|
|
<button className="button is-danger">
|
|
<span className="icon is-small">
|
|
<FontAwesomeIcon icon={faBan} />
|
|
</span>
|
|
<span>Block Community: {user.group.name}</span>
|
|
</button>
|
|
}
|
|
</div>
|
|
</Section>
|
|
|
|
<div style={{ padding: '0px 1rem' }}>
|
|
<Subtitle>Posts</Subtitle>
|
|
</div>
|
|
|
|
<PostList posts={posts} />
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default ViewUser
|