[ABANDONED] React/Redux front end for the Flexor social network.
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.
 
 

120 lines
4.5 KiB

import React, { FC, useEffect } from 'react'
import { useSelector, useDispatch } from 'react-redux'
import { Link, useParams, useHistory } from 'react-router-dom'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { faUserPlus, faUserMinus, faBan } from '@fortawesome/free-solid-svg-icons'
import { handleApiError } from 'src/api/errors'
import { fetchUser } from 'src/actions/users'
import { fetchUserPosts } from 'src/actions/posts'
import { getEntity } from 'src/selectors/entities'
import { getAuthenticatedUser } from 'src/selectors/authentication'
import { useDeepCompareEffect, useConfig } from 'src/hooks'
import { setTitle, urlForBlob } from 'src/utils'
import { AppState, EntityType, User, AppThunkDispatch } from 'src/types'
import PageHeader from 'src/components/page-header'
import UserInfo from 'src/components/user-info'
import Loading from 'src/components/pages/loading'
interface Params {
id: string
}
const ViewUser: FC = () => {
const { id } = useParams<Params>()
const self = useSelector<AppState, User | undefined>(getAuthenticatedUser)
const user = useSelector<AppState, User | undefined>(state => getEntity<User>(state, EntityType.User, 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)
}
}
init()
}, [])
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
return (
<div>
<PageHeader title={user.name} />
<div className="main-content">
<UserInfo user={user} />
<div className="centered-content">
<article className="media">
{imageUrl &&
<figure className="media-left">
<p className="image is-32x32">
<img src={imageUrl} style={{ width: 128 }} />
</p>
</figure>
}
<div className="media-content">
<div className="content">
<h1 className="is-size-3">
<span className="is-size-3">{user.name}</span> <span className="is-size-4 has-text-weight-bold">@{user.id}</span>
</h1>
{user.group && <h2 className="is-size-4">Community: <Link to={`/c/${user.group.id}`}>{user.group.name}</Link></h2>}
<br />
<p>{user.about}</p>
</div>
</div>
</article>
<br />
<div className="buttons">
<button className="button is-success">
<span className="icon is-small">
<FontAwesomeIcon icon={faUserPlus} />
</span>
<span>Susbcribe</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>
</div>
</div>
</div>
)
}
export default ViewUser