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

137 lines
4.8 KiB

import React, { FC, useEffect } from 'react'
import { useSelector, useDispatch } from 'react-redux'
import { Link, useParams, useHistory } from 'react-router-dom'
import classNames from 'classnames'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { faPlusSquare, faMinusSquare } from '@fortawesome/free-solid-svg-icons'
import { handleApiError } from 'src/api/errors'
import { fetchApp, installApp, uninstallApp } from 'src/actions/apps'
import { getAuthenticatedUserId } from 'src/selectors/authentication'
import { getEntity } from 'src/selectors/entities'
import { getIsFetching } from 'src/selectors/requests'
import { useConfig } from 'src/hooks'
import { setTitle, urlForBlob } from 'src/utils'
import { AppState, AppThunkDispatch, EntityType, App, RequestKey, Installation } from 'src/types'
import { fetchInstallations } from 'src/actions/composer'
import { getInstallations } from 'src/selectors/composer'
import PageHeader from 'src/components/page-header'
import Loading from 'src/components/pages/loading'
import AppInfo from 'src/components/app-info'
import { ClassDictionary } from 'src/types'
interface Params {
id: string
}
const ViewApp: FC = () => {
const { id } = useParams<Params>()
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 = () => {
const classes: ClassDictionary = {
'button': true,
'is-danger': installed,
'is-success': !installed,
'is-loading': fetching,
}
if (installed) {
const handleClick = async () => {
await dispatch(uninstallApp(id))
await dispatch(fetchApp(id))
}
return (
<button className={classNames(classes)} onClick={() => handleClick()}>
<span className="icon is-small">
<FontAwesomeIcon icon={faMinusSquare} />
</span>
<span>Uninstall</span>
</button>
)
} else {
const handleClick = async () => {
await dispatch(installApp(id))
await dispatch(fetchApp(id))
}
return (
<button className={classNames(classes)} onClick={() => handleClick()}>
<span className="icon is-small">
<FontAwesomeIcon icon={faPlusSquare} />
</span>
<span>Install</span>
</button>
)
}
}
const imageUrl = app.imageUrl ? urlForBlob(config, app.imageUrl) : undefined
return (
<div>
<PageHeader title={app.name} />
<div className="main-content">
<AppInfo app={app} />
<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 has-text-primary">{app.name}</h1>
<br />
<p>{app.about}</p>
</div>
</div>
</article>
<br />
<div className="buttons">
{renderButton()}
{isCreator && <Link className="button is-warning" to={`/a/${id}/edit`}>Edit App</Link>}
</div>
</div>
</div>
</div>
)
}
export default ViewApp