[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.
 
 

143 lines
4.7 KiB

import React, { FC, useEffect } from 'react'
import { useSelector, useDispatch } from 'react-redux'
import { Link, useParams, useHistory } from 'react-router-dom'
import { faPlusSquare, faMinusSquare } from '@fortawesome/free-solid-svg-icons'
import moment from 'moment'
import { handleApiError } from 'src/api/errors'
import { fetchApp, installApp, uninstallApp } from 'src/actions/apps'
import { fetchInstallations } from 'src/actions/composer'
import { getAuthenticatedUserId } from 'src/selectors/authentication'
import { getInstallations } from 'src/selectors/composer'
import { getEntity } from 'src/selectors/entities'
import { getIsFetching } from 'src/selectors/requests'
import { useConfig, useTheme } from 'src/hooks'
import { setTitle, urlForBlob } from 'src/utils'
import { AppState, AppThunkDispatch, EntityType, App, RequestKey, Installation, LevelItem } from 'src/types'
import Title from 'src/components/title'
import Section from 'src/components/section'
import HorizontalRule from 'src/components/horizontal-rule'
import Button from 'src/components/controls/button'
import Level from 'src/components/level'
import Loading from 'src/components/pages/loading'
interface Params {
id: string
}
const ViewApp: FC = () => {
const { id } = useParams<Params>()
const theme = useTheme()
const app = useSelector<AppState, App | undefined>(state => getEntity<App>(state, EntityType.App, id))
const installations = useSelector<AppState, Installation[]>(getInstallations)
const selfId = useSelector<AppState, string | undefined>(getAuthenticatedUserId)
const fetching = useSelector<AppState, boolean>(state => getIsFetching(state, RequestKey.InstallApp) || getIsFetching(state, RequestKey.UninstallApp))
const dispatch = useDispatch<AppThunkDispatch>()
const config = useConfig()
const history = useHistory()
useEffect(() => {
try {
dispatch(fetchApp(id))
dispatch(fetchInstallations())
} catch (err) {
handleApiError(err, dispatch, history)
}
}, [])
useEffect(() => {
if (app) setTitle(app.name)
}, [app])
if (!app) return <Loading />
const isCreator = app.user.id === selfId
const installed = !!installations.find(i => i.app.id === app.id)
const renderButton = () => {
if (installed) {
const handleClick = async () => {
await dispatch(uninstallApp(id))
await dispatch(fetchApp(id))
}
return (
<Button text="Uninstall" icon={faMinusSquare} loading={fetching} onClick={() => handleClick()} color="#fff" backgroundColor={theme.red} />
)
} else {
const handleClick = async () => {
await dispatch(installApp(id))
await dispatch(fetchApp(id))
}
return (
<Button text="Install" icon={faPlusSquare} loading={fetching} onClick={() => handleClick()} color={theme.primaryAlternate} backgroundColor={theme.primary} />
)
}
}
const imageUrl = app.imageUrl ? urlForBlob(config, app.imageUrl) : undefined
const coverImageUrl = app.coverImageUrl ? urlForBlob(config, app.coverImageUrl) : undefined
const items: LevelItem[] = []
items.push({
label: 'Users',
content: app.users,
})
items.push({
label: 'Rating',
content: app.rating.toString(),
})
if (app.companyName) {
items.push({
label: 'Company',
content: app.companyName,
})
}
items.push({
label: 'Updated',
content: moment(app.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>{app.name}</Title>
<p style={{ color: theme.text }}>{app.about}</p>
</div>
</div>
<Level items={items} />
<div style={{ textAlign: 'center', padding: '1rem' }}>
{renderButton()}
</div>
<HorizontalRule />
<div className="buttons">
{isCreator && <Link style={{ color: theme.secondary }} to={`/a/${id}/edit`}>View/Edit App</Link>}
</div>
</Section>
</div>
)
}
export default ViewApp