// edit-app.tsx // Copyright (C) 2020 Dwayne Harris // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see . import React, { FC, useEffect, useState } from 'react' import { useSelector, useDispatch } from 'react-redux' import { useParams, useHistory } from 'react-router-dom' import { faIdCard, faCheckCircle, faKey, faShieldAlt, faLink, faAddressBook, faCodeBranch } from '@fortawesome/free-solid-svg-icons' import { handleApiError } from '../../api/errors' import { fetchApp, updateApp } from '../../actions/apps' import { initForm, initField, setFieldNotification } from '../../actions/forms' import { showNotification } from '../../actions/notifications' import { getAuthenticatedUserId } from '../../selectors/authentication' import { getEntity } from '../../selectors/entities' import { getForm } from '../../selectors/forms' import { getIsFetching } from '../../selectors/requests' import { useAuthenticationCheck, useDeepCompareEffect, useTheme } from '../../hooks' import { setTitle, valueFromForm } from '../../utils' import { AppState, AppThunkDispatch, EntityType, App, NotificationType, RequestKey } from '../../types' import Title from '../../components/title' import Section from '../../components/section' import HorizontalRule from '../../components/horizontal-rule' import Loading from '../../components/pages/loading' import TextField from '../../components/controls/text-field' import TextareaField from '../../components/controls/textarea-field' import ImageField from '../../components/controls/image-field' import CoverImageField from '../../components/controls/cover-image-field' import IconImageField from '../../components/controls/icon-image-field' import StaticField from '../../components/controls/static-field' import PrimaryButton from '../../components/controls/primary-button' interface Params { id: string } const EditApp: FC = () => { useAuthenticationCheck() const theme = useTheme() const { id } = useParams() const fetching = useSelector(state => getIsFetching(state, RequestKey.UpdateApp)) const app = useSelector(state => getEntity(state, EntityType.App, id)) const form = useSelector(getForm) const selfId = useSelector(getAuthenticatedUserId) const dispatch = useDispatch() const history = useHistory() const [showPrivateKey, setShowPrivateKey] = useState(false) 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 const handleUpdate = async () => { const name = valueFromForm(form, 'name') const about = valueFromForm(form, 'about') const websiteUrl = valueFromForm(form, 'websiteUrl') const companyName = valueFromForm(form, 'companyName') const version = valueFromForm(form, 'version') const composerUrl = valueFromForm(form, 'composerUrl') const rendererUrl = valueFromForm(form, 'rendererUrl') const imageUrl = valueFromForm(form, 'image') const coverImageUrl = valueFromForm(form, 'coverImage') const iconImageUrl = valueFromForm(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!')) setTimeout(() => { history.push(`/a/${app.id}`) }, 1000) } catch (err) { handleApiError(err, dispatch, history) } } const privateKeyDisplay = showPrivateKey ? app.privateKey : '********' return ( ) } export default EditApp