Display a nicer error message when a file cannot be opened
This commit is contained in:
parent
e6a61fbe9b
commit
88374de38c
|
@ -1,17 +1,30 @@
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import PageContentBlock from '@/components/elements/PageContentBlock';
|
import PageContentBlock from '@/components/elements/PageContentBlock';
|
||||||
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
||||||
|
import { faArrowLeft } from '@fortawesome/free-solid-svg-icons/faArrowLeft';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
title?: string;
|
title?: string;
|
||||||
message: string;
|
message: string;
|
||||||
|
onBack?: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default ({ title, message }: Props) => (
|
export default ({ title, message, onBack }: Props) => (
|
||||||
<PageContentBlock>
|
<PageContentBlock>
|
||||||
<div className={'flex justify-center'}>
|
<div className={'flex justify-center'}>
|
||||||
<div className={'w-full sm:w-3/4 md:w-1/2 p-12 md:p-20 bg-neutral-100 rounded-lg shadow-lg text-center'}>
|
<div className={'w-full sm:w-3/4 md:w-1/2 p-12 md:p-20 bg-neutral-100 rounded-lg shadow-lg text-center relative'}>
|
||||||
|
{typeof onBack === 'function' &&
|
||||||
|
<div className={'absolute pin-l pin-t ml-4 mt-4'}>
|
||||||
|
<button
|
||||||
|
onClick={() => onBack()}
|
||||||
|
className={'rounded-full btn btn-primary w-8 h-8 flex items-center justify-center'}
|
||||||
|
>
|
||||||
|
<FontAwesomeIcon icon={faArrowLeft}/>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
<img src={'/assets/svgs/server_error.svg'} className={'w-2/3 h-auto select-none'}/>
|
<img src={'/assets/svgs/server_error.svg'} className={'w-2/3 h-auto select-none'}/>
|
||||||
<h2 className={'mt-6 text-neutral-900 font-bold'}>{ title || 'Something went wrong!' }</h2>
|
<h2 className={'mt-6 text-neutral-900 font-bold'}>{title || 'Something went wrong!'}</h2>
|
||||||
<p className={'text-sm text-neutral-700 mt-2'}>
|
<p className={'text-sm text-neutral-700 mt-2'}>
|
||||||
{message}
|
{message}
|
||||||
</p>
|
</p>
|
||||||
|
|
|
@ -2,7 +2,7 @@ import React, { lazy, useEffect, useState } from 'react';
|
||||||
import { ServerContext } from '@/state/server';
|
import { ServerContext } from '@/state/server';
|
||||||
import getFileContents from '@/api/server/files/getFileContents';
|
import getFileContents from '@/api/server/files/getFileContents';
|
||||||
import useRouter from 'use-react-router';
|
import useRouter from 'use-react-router';
|
||||||
import { Actions, useStoreActions, useStoreState } from 'easy-peasy';
|
import { Actions, useStoreActions } from 'easy-peasy';
|
||||||
import { ApplicationStore } from '@/state';
|
import { ApplicationStore } from '@/state';
|
||||||
import { httpErrorToHuman } from '@/api/http';
|
import { httpErrorToHuman } from '@/api/http';
|
||||||
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
||||||
|
@ -13,10 +13,12 @@ import FileNameModal from '@/components/server/files/FileNameModal';
|
||||||
import Can from '@/components/elements/Can';
|
import Can from '@/components/elements/Can';
|
||||||
import FlashMessageRender from '@/components/FlashMessageRender';
|
import FlashMessageRender from '@/components/FlashMessageRender';
|
||||||
import PageContentBlock from '@/components/elements/PageContentBlock';
|
import PageContentBlock from '@/components/elements/PageContentBlock';
|
||||||
|
import ServerError from '@/components/screens/ServerError';
|
||||||
|
|
||||||
const LazyAceEditor = lazy(() => import(/* webpackChunkName: "editor" */'@/components/elements/AceEditor'));
|
const LazyAceEditor = lazy(() => import(/* webpackChunkName: "editor" */'@/components/elements/AceEditor'));
|
||||||
|
|
||||||
export default () => {
|
export default () => {
|
||||||
|
const [ error, setError ] = useState('');
|
||||||
const { action } = useParams();
|
const { action } = useParams();
|
||||||
const { history, location: { hash } } = useRouter();
|
const { history, location: { hash } } = useRouter();
|
||||||
const [ loading, setLoading ] = useState(action === 'edit');
|
const [ loading, setLoading ] = useState(action === 'edit');
|
||||||
|
@ -31,12 +33,12 @@ export default () => {
|
||||||
if (action !== 'new') {
|
if (action !== 'new') {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
clearFlashes('files:view');
|
setError('');
|
||||||
getFileContents(uuid, hash.replace(/^#/, ''))
|
getFileContents(uuid, hash.replace(/^#/, ''))
|
||||||
.then(setContent)
|
.then(setContent)
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
addError({ key: 'files:view', message: httpErrorToHuman(error) });
|
setError(httpErrorToHuman(error));
|
||||||
})
|
})
|
||||||
.then(() => setLoading(false));
|
.then(() => setLoading(false));
|
||||||
}, [ uuid, hash ]);
|
}, [ uuid, hash ]);
|
||||||
|
@ -49,9 +51,10 @@ export default () => {
|
||||||
|
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
clearFlashes('files:view');
|
clearFlashes('files:view');
|
||||||
fetchFileContent().then(content => {
|
fetchFileContent()
|
||||||
return saveFileContents(uuid, name || hash.replace(/^#/, ''), content);
|
.then(content => {
|
||||||
})
|
return saveFileContents(uuid, name || hash.replace(/^#/, ''), content);
|
||||||
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
if (name) {
|
if (name) {
|
||||||
history.push(`/server/${id}/files/edit#/${name}`);
|
history.push(`/server/${id}/files/edit#/${name}`);
|
||||||
|
@ -67,6 +70,15 @@ export default () => {
|
||||||
.then(() => setLoading(false));
|
.then(() => setLoading(false));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
return (
|
||||||
|
<ServerError
|
||||||
|
message={error}
|
||||||
|
onBack={() => history.goBack()}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<PageContentBlock>
|
<PageContentBlock>
|
||||||
<FlashMessageRender byKey={'files:view'} className={'mb-4'}/>
|
<FlashMessageRender byKey={'files:view'} className={'mb-4'}/>
|
||||||
|
|
Loading…
Reference in New Issue