show the error and success notifications when appropriate when editing xml for json w/ burnettk

This commit is contained in:
jasquat 2023-01-10 14:43:05 -05:00
parent bd2ec3d148
commit 9eb705ca76

View File

@ -1,4 +1,4 @@
import { useEffect, useState } from 'react'; import { useContext, useEffect, useState } from 'react';
import Editor from '@monaco-editor/react'; import Editor from '@monaco-editor/react';
import { useNavigate, useParams, useSearchParams } from 'react-router-dom'; import { useNavigate, useParams, useSearchParams } from 'react-router-dom';
// @ts-ignore // @ts-ignore
@ -8,18 +8,25 @@ import HttpService from '../services/HttpService';
import ButtonWithConfirmation from '../components/ButtonWithConfirmation'; import ButtonWithConfirmation from '../components/ButtonWithConfirmation';
import { modifyProcessIdentifierForPathParam } from '../helpers'; import { modifyProcessIdentifierForPathParam } from '../helpers';
import { ProcessFile } from '../interfaces'; import { ProcessFile } from '../interfaces';
import ErrorContext from '../contexts/ErrorContext';
import { Notification } from '../components/Notification';
// NOTE: This is mostly the same as ProcessModelEditDiagram and if we go this route could // NOTE: This is mostly the same as ProcessModelEditDiagram and if we go this route could
// possibly be merged into it. I'm leaving as a separate file now in case it does // possibly be merged into it. I'm leaving as a separate file now in case it does
// end up diverging greatly // end up diverging greatly
export default function ReactFormEditor() { export default function ReactFormEditor() {
const params = useParams(); const params = useParams();
const setErrorObject = (useContext as any)(ErrorContext)[1];
const [showFileNameEditor, setShowFileNameEditor] = useState(false); const [showFileNameEditor, setShowFileNameEditor] = useState(false);
const [newFileName, setNewFileName] = useState(''); const [newFileName, setNewFileName] = useState('');
const searchParams = useSearchParams()[0]; const searchParams = useSearchParams()[0];
const handleShowFileNameEditor = () => setShowFileNameEditor(true); const handleShowFileNameEditor = () => setShowFileNameEditor(true);
const navigate = useNavigate(); const navigate = useNavigate();
const [displaySaveFileMessage, setDisplaySaveFileMessage] =
useState<boolean>(false);
const [processModelFile, setProcessModelFile] = useState<ProcessFile | null>( const [processModelFile, setProcessModelFile] = useState<ProcessFile | null>(
null null
); );
@ -70,6 +77,7 @@ export default function ReactFormEditor() {
}, [params, modifiedProcessModelId]); }, [params, modifiedProcessModelId]);
const navigateToProcessModelFile = (_result: any) => { const navigateToProcessModelFile = (_result: any) => {
setDisplaySaveFileMessage(true);
if (!params.file_name) { if (!params.file_name) {
const fileNameWithExtension = `${newFileName}.${fileExtension}`; const fileNameWithExtension = `${newFileName}.${fileExtension}`;
navigate( navigate(
@ -79,6 +87,9 @@ export default function ReactFormEditor() {
}; };
const saveFile = () => { const saveFile = () => {
setErrorObject(null);
setDisplaySaveFileMessage(false);
let url = `/process-models/${modifiedProcessModelId}/files`; let url = `/process-models/${modifiedProcessModelId}/files`;
let httpMethod = 'PUT'; let httpMethod = 'PUT';
let fileNameWithExtension = params.file_name; let fileNameWithExtension = params.file_name;
@ -105,6 +116,7 @@ export default function ReactFormEditor() {
HttpService.makeCallToBackend({ HttpService.makeCallToBackend({
path: url, path: url,
successCallback: navigateToProcessModelFile, successCallback: navigateToProcessModelFile,
failureCallback: setErrorObject,
httpMethod, httpMethod,
postBody: formData, postBody: formData,
}); });
@ -162,6 +174,20 @@ export default function ReactFormEditor() {
); );
}; };
const saveFileMessage = () => {
if (displaySaveFileMessage) {
return (
<Notification
title="File Saved: "
onClose={() => setDisplaySaveFileMessage(false)}
>
Changes to the file were saved.
</Notification>
);
}
return null;
};
if (processModelFile || !params.file_name) { if (processModelFile || !params.file_name) {
const processModelFileName = processModelFile ? processModelFile.name : ''; const processModelFileName = processModelFile ? processModelFile.name : '';
return ( return (
@ -182,6 +208,7 @@ export default function ReactFormEditor() {
{processModelFileName} {processModelFileName}
</h1> </h1>
{newFileNameBox()} {newFileNameBox()}
{saveFileMessage()}
<Button onClick={saveFile} variant="danger" data-qa="file-save-button"> <Button onClick={saveFile} variant="danger" data-qa="file-save-button">
Save Save
</Button> </Button>