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.0 KiB
120 lines
4.0 KiB
import React, { FC, useEffect } from 'react'
|
|
import { useSelector, useDispatch } from 'react-redux'
|
|
import { useParams, useHistory } from 'react-router-dom'
|
|
import { faEdit, faUserCheck, faBan } from '@fortawesome/free-solid-svg-icons'
|
|
import moment from 'moment'
|
|
|
|
import { handleApiError } from 'src/api/errors'
|
|
import { fetchGroup } from 'src/actions/groups'
|
|
import { getAuthenticated } from 'src/selectors/authentication'
|
|
import { getEntity } from 'src/selectors/entities'
|
|
|
|
import { useDeepCompareEffect, useConfig, useTheme } from 'src/hooks'
|
|
import { setTitle, urlForBlob } from 'src/utils'
|
|
import { AppState, EntityType, Group, GroupMembershipType, AppThunkDispatch, LevelItem } from 'src/types'
|
|
|
|
import Title from 'src/components/title'
|
|
import Level from 'src/components/level'
|
|
import Section from 'src/components/section'
|
|
import PrimaryButton from 'src/components/controls/primary-button'
|
|
import Button from 'src/components/controls/button'
|
|
import Loading from 'src/components/pages/loading'
|
|
import HorizontalRule from 'src/components/horizontal-rule'
|
|
|
|
interface Params {
|
|
id: string
|
|
}
|
|
|
|
const ViewGroup: FC = () => {
|
|
const { id } = useParams<Params>()
|
|
const theme = useTheme()
|
|
const group = useSelector<AppState, Group | undefined>(state => getEntity<Group>(state, EntityType.Group, id))
|
|
const authenticated = useSelector<AppState, boolean>(getAuthenticated)
|
|
const dispatch = useDispatch<AppThunkDispatch>()
|
|
const config = useConfig()
|
|
const history = useHistory()
|
|
|
|
useEffect(() => {
|
|
try {
|
|
dispatch(fetchGroup(id))
|
|
} catch (err) {
|
|
handleApiError(err, dispatch, history)
|
|
}
|
|
}, [])
|
|
|
|
useDeepCompareEffect(() => {
|
|
if (group) setTitle(group.name)
|
|
}, [group])
|
|
|
|
if (!group) return <Loading />
|
|
|
|
const isAdmin = group.membership === GroupMembershipType.Admin
|
|
const isMember = !!group.membership
|
|
const imageUrl = group.imageUrl ? urlForBlob(config, group.imageUrl) : undefined
|
|
const coverImageUrl = group.coverImageUrl ? urlForBlob(config, group.coverImageUrl) : undefined
|
|
|
|
const items: LevelItem[] = []
|
|
items.push({
|
|
label: 'Members',
|
|
content: group.members,
|
|
})
|
|
items.push({
|
|
label: 'Posts',
|
|
content: group.posts,
|
|
})
|
|
items.push({
|
|
label: 'Awards',
|
|
content: group.awards,
|
|
})
|
|
items.push({
|
|
label: 'Points',
|
|
content: group.points,
|
|
})
|
|
items.push({
|
|
label: 'Created',
|
|
content: moment(group.updated).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} />
|
|
</div>
|
|
}
|
|
<div>
|
|
<Title>{group.name}</Title>
|
|
<p style={{ color: theme.text }}>{group.about}</p>
|
|
</div>
|
|
</div>
|
|
|
|
<Level items={items} />
|
|
<HorizontalRule />
|
|
|
|
<div className="buttons">
|
|
{!authenticated &&
|
|
<PrimaryButton text="Create an Account" icon={faUserCheck} onClick={() => history.push(`/c/${group.id}/register`)} />
|
|
}
|
|
|
|
{!isMember &&
|
|
<Button text="Block" icon={faBan} onClick={() => history.push(`/c/${group.id}/register`)} color={theme.backgroundPrimary} backgroundColor={theme.red} />
|
|
}
|
|
|
|
{isAdmin &&
|
|
<PrimaryButton text={`Edit ${group.name}`} icon={faEdit} onClick={() => history.push(`/c/${group.id}/admin/`)} />
|
|
}
|
|
</div>
|
|
</Section>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default ViewGroup
|