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.
170 lines
6.8 KiB
170 lines
6.8 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 { faIdCard, faCheckCircle } from '@fortawesome/free-solid-svg-icons'
|
|
|
|
import { handleApiError } from 'src/api/errors'
|
|
import { fetchApp, updateApp } from 'src/actions/apps'
|
|
import { initForm, initField, setFieldNotification } from 'src/actions/forms'
|
|
import { showNotification } from 'src/actions/notifications'
|
|
import { getAuthenticatedUserId } from 'src/selectors/authentication'
|
|
import { getEntity } from 'src/selectors/entities'
|
|
import { getForm } from 'src/selectors/forms'
|
|
|
|
import { useAuthenticationCheck, useDeepCompareEffect } from 'src/hooks'
|
|
import { setTitle, valueFromForm } from 'src/utils'
|
|
import { AppState, AppThunkDispatch, EntityType, App, Form, NotificationType } from 'src/types'
|
|
|
|
import PageHeader from 'src/components/page-header'
|
|
import Loading from 'src/components/pages/loading'
|
|
|
|
import TextField from 'src/components/forms/text-field'
|
|
import TextareaField from 'src/components/forms/textarea-field'
|
|
import ImageField from 'src/components/forms/image-field'
|
|
import CoverImageField from 'src/components/forms/cover-image-field'
|
|
import IconImageField from 'src/components/forms/icon-image-field'
|
|
|
|
interface Params {
|
|
id: string
|
|
}
|
|
|
|
const EditApp: FC = () => {
|
|
useAuthenticationCheck()
|
|
|
|
const { id } = useParams<Params>()
|
|
const app = useSelector<AppState, App | undefined>(state => getEntity<App>(state, EntityType.App, id))
|
|
const form = useSelector<AppState, Form>(getForm)
|
|
const selfId = useSelector<AppState, string | undefined>(getAuthenticatedUserId)
|
|
const dispatch = useDispatch<AppThunkDispatch>()
|
|
const history = useHistory()
|
|
|
|
useEffect(() => {
|
|
try {
|
|
dispatch(fetchApp(id))
|
|
} catch (err) {
|
|
handleApiError(err, dispatch, history)
|
|
}
|
|
}, [])
|
|
|
|
useDeepCompareEffect(() => {
|
|
if (app) {
|
|
if (app.user.id !== selfId) {
|
|
history.push(`/a/${id}`)
|
|
}
|
|
|
|
setTitle(app.name)
|
|
|
|
dispatch(initForm())
|
|
dispatch(initField('name', app.name))
|
|
dispatch(initField('about', app.about || ''))
|
|
dispatch(initField('websiteUrl', app.websiteUrl || ''))
|
|
dispatch(initField('companyName', app.companyName || ''))
|
|
dispatch(initField('version', ''))
|
|
dispatch(initField('composerUrl', app.composerUrl || ''))
|
|
dispatch(initField('rendererUrl', app.rendererUrl || ''))
|
|
dispatch(initField('image', app.imageUrl || ''))
|
|
dispatch(initField('coverImage', app.coverImageUrl || ''))
|
|
dispatch(initField('iconImage', app.iconImageUrl || ''))
|
|
}
|
|
}, [app])
|
|
|
|
if (!app) return <Loading />
|
|
|
|
const handleUpdate = async () => {
|
|
const name = valueFromForm<string>(form, 'name')
|
|
const about = valueFromForm<string>(form, 'about')
|
|
const websiteUrl = valueFromForm<string>(form, 'websiteUrl')
|
|
const companyName = valueFromForm<string>(form, 'companyName')
|
|
const version = valueFromForm<string>(form, 'version')
|
|
const composerUrl = valueFromForm<string>(form, 'composerUrl')
|
|
const rendererUrl = valueFromForm<string>(form, 'rendererUrl')
|
|
const imageUrl = valueFromForm<string>(form, 'image')
|
|
const coverImageUrl = valueFromForm<string>(form, 'coverImage')
|
|
const iconImageUrl = valueFromForm<string>(form, 'iconImage')
|
|
|
|
if (!name) {
|
|
dispatch(showNotification(NotificationType.Error, 'Name is required'))
|
|
dispatch(setFieldNotification('name', NotificationType.Error, 'This is required'))
|
|
return
|
|
}
|
|
|
|
if (!version) {
|
|
dispatch(showNotification(NotificationType.Error, 'Version is required'))
|
|
dispatch(setFieldNotification('version', NotificationType.Error, 'This is required'))
|
|
return
|
|
}
|
|
|
|
try {
|
|
await dispatch(updateApp(id, {
|
|
name,
|
|
about,
|
|
websiteUrl,
|
|
companyName,
|
|
version,
|
|
composerUrl,
|
|
rendererUrl,
|
|
imageUrl,
|
|
coverImageUrl,
|
|
iconImageUrl,
|
|
}))
|
|
|
|
dispatch(showNotification(NotificationType.Success, 'Updated'))
|
|
} catch (err) {
|
|
handleApiError(err, dispatch, history)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div>
|
|
<PageHeader title={app.name} />
|
|
|
|
<div className="main-content">
|
|
<div className="centered-content">
|
|
<div className="field">
|
|
<label className="label">ID</label>
|
|
<div className="control has-icons-left">
|
|
<input className="input" type="text" value={app.id} readOnly />
|
|
<span className="icon is-small is-left">
|
|
<FontAwesomeIcon icon={faIdCard} />
|
|
</span>
|
|
</div>
|
|
</div>
|
|
<br />
|
|
|
|
<TextField name="name" label="Name" placeholder="App Name" />
|
|
<br />
|
|
<TextareaField name="about" label="About" placeholder="Description of this app" />
|
|
<br />
|
|
<TextField name="websiteUrl" label="Website" placeholder="Website URL (optional)" />
|
|
<br />
|
|
<TextField name="companyName" label="Company" placeholder="Your company or organization (optional)" />
|
|
<br />
|
|
<TextField name="version" label="Version" placeholder="Current Version of the app (ex: 0.0.1beta5)" help={`Last Version: ${app.version}`} />
|
|
<br /><hr />
|
|
|
|
<ImageField name="image" />
|
|
<br />
|
|
<CoverImageField name="coverImage" />
|
|
<br />
|
|
<IconImageField name="iconImage" />
|
|
<br /><hr />
|
|
|
|
<TextField name="composerUrl" label="Composer URL" placeholder="URL for the composer web page" />
|
|
<br />
|
|
<TextField name="rendererUrl" label="Renderer URL" placeholder="URL for the renderer template" />
|
|
<br /><br />
|
|
|
|
<button className="button is-primary" onClick={() => handleUpdate()}>
|
|
<span className="icon is-small">
|
|
<FontAwesomeIcon icon={faCheckCircle} />
|
|
</span>
|
|
<span>Save</span>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default EditApp
|