import { useContext, useEffect, useState } from 'react'; import { Link, useNavigate, useParams } from 'react-router-dom'; import { Add, Upload, Download, TrashCan, Favorite, Edit, // @ts-ignore } from '@carbon/icons-react'; import { Accordion, AccordionItem, Button, Grid, Column, Stack, ButtonSet, Modal, FileUploader, Table, TableHead, TableHeader, TableRow, TableCell, TableBody, // @ts-ignore } from '@carbon/react'; import ProcessBreadcrumb from '../components/ProcessBreadcrumb'; import HttpService from '../services/HttpService'; import ErrorContext from '../contexts/ErrorContext'; import { modifyProcessModelPath, unModifyProcessModelPath } from '../helpers'; import { ProcessFile, ProcessModel, RecentProcessModel } from '../interfaces'; import ButtonWithConfirmation from '../components/ButtonWithConfirmation'; const storeRecentProcessModelInLocalStorage = ( processModelForStorage: ProcessModel ) => { // All values stored in localStorage are strings. // Grab our recentProcessModels string from localStorage. const stringFromLocalStorage = window.localStorage.getItem( 'recentProcessModels' ); // adapted from https://stackoverflow.com/a/59424458/6090676 // If that value is null (meaning that we've never saved anything to that spot in localStorage before), use an empty array as our array. Otherwise, use the value we parse out. let array: RecentProcessModel[] = []; if (stringFromLocalStorage !== null) { // Then parse that string into an actual value. array = JSON.parse(stringFromLocalStorage); } // Here's the value we want to add const value = { processModelIdentifier: processModelForStorage.id, processModelDisplayName: processModelForStorage.display_name, }; // anything with a processGroupIdentifier is old and busted. leave it behind. array = array.filter((item) => item.processGroupIdentifier === undefined); // If our parsed/empty array doesn't already have this value in it... const matchingItem = array.find( (item) => item.processModelIdentifier === value.processModelIdentifier ); if (matchingItem === undefined) { // add the value to the beginning of the array array.unshift(value); // Keep the array to 3 items if (array.length > 3) { array.pop(); } } // once the old and busted serializations are gone, we can put these two statements inside the above if statement // turn the array WITH THE NEW VALUE IN IT into a string to prepare it to be stored in localStorage const stringRepresentingArray = JSON.stringify(array); // and store it in localStorage as "recentProcessModels" window.localStorage.setItem('recentProcessModels', stringRepresentingArray); }; export default function ProcessModelShow() { const params = useParams(); const setErrorMessage = (useContext as any)(ErrorContext)[1]; const [processModel, setProcessModel] = useState(null); const [processInstanceResult, setProcessInstanceResult] = useState(null); const [reloadModel, setReloadModel] = useState(false); const [filesToUpload, setFilesToUpload] = useState(null); const [showFileUploadModal, setShowFileUploadModal] = useState(false); const navigate = useNavigate(); const modifiedProcessModelId = modifyProcessModelPath( `${params.process_model_id}` ); useEffect(() => { const processResult = (result: ProcessModel) => { setProcessModel(result); setReloadModel(false); storeRecentProcessModelInLocalStorage(result); }; HttpService.makeCallToBackend({ path: `/process-models/${modifiedProcessModelId}`, successCallback: processResult, }); }, [reloadModel, modifiedProcessModelId]); const processModelRun = (processInstance: any) => { setErrorMessage(null); HttpService.makeCallToBackend({ path: `/process-instances/${processInstance.id}/run`, successCallback: setProcessInstanceResult, failureCallback: setErrorMessage, httpMethod: 'POST', }); }; const processInstanceCreateAndRun = () => { HttpService.makeCallToBackend({ path: `/process-models/${modifiedProcessModelId}/process-instances`, successCallback: processModelRun, httpMethod: 'POST', }); }; const processInstanceResultTag = () => { if (processModel && processInstanceResult) { let takeMeToMyTaskBlurb = null; // FIXME: ensure that the task is actually for the current user as well const processInstanceId = (processInstanceResult as any).id; const nextTask = (processInstanceResult as any).next_task; if (nextTask && nextTask.state === 'READY') { takeMeToMyTaskBlurb = ( You have a task to complete. Go to{' '} my task . ); } return (

Process Instance {processInstanceId} kicked off ( view ). {takeMeToMyTaskBlurb}

); } return null; }; const onUploadedCallback = () => { setReloadModel(true); }; const reloadModelOhYeah = (_httpResult: any) => { setReloadModel(!reloadModel); }; // Remove this code from const onDeleteFile = (fileName: string) => { const url = `/process-models/${modifiedProcessModelId}/files/${fileName}`; const httpMethod = 'DELETE'; HttpService.makeCallToBackend({ path: url, successCallback: reloadModelOhYeah, httpMethod, }); }; const onSetPrimaryFile = (fileName: string) => { const url = `/process-models/${modifiedProcessModelId}`; const httpMethod = 'PUT'; const processModelToPass = { primary_file_name: fileName, }; HttpService.makeCallToBackend({ path: url, successCallback: onUploadedCallback, httpMethod, postBody: processModelToPass, }); }; const handleProcessModelFileResult = (processModelFile: ProcessFile) => { if ( !('file_contents' in processModelFile) || processModelFile.file_contents === undefined ) { setErrorMessage({ message: `Could not file file contents for file: ${processModelFile.name}`, }); return; } let contentType = 'application/xml'; if (processModelFile.type === 'json') { contentType = 'application/json'; } const element = document.createElement('a'); const file = new Blob([processModelFile.file_contents], { type: contentType, }); const downloadFileName = processModelFile.name; element.href = URL.createObjectURL(file); element.download = downloadFileName; document.body.appendChild(element); element.click(); }; const downloadFile = (fileName: string) => { setErrorMessage(null); const processModelPath = `process-models/${modifiedProcessModelId}`; HttpService.makeCallToBackend({ path: `/${processModelPath}/files/${fileName}`, successCallback: handleProcessModelFileResult, }); }; const profileModelFileEditUrl = (processModelFile: ProcessFile) => { if (processModel) { if (processModelFile.name.match(/\.(dmn|bpmn)$/)) { return `/admin/process-models/${modifiedProcessModelId}/files/${processModelFile.name}`; } if (processModelFile.name.match(/\.(json|md)$/)) { return `/admin/process-models/${modifiedProcessModelId}/form/${processModelFile.name}`; } } return null; }; const navigateToFileEdit = (processModelFile: ProcessFile) => { const url = profileModelFileEditUrl(processModelFile); if (url) { navigate(url); } }; const renderButtonElements = ( processModelFile: ProcessFile, isPrimaryBpmnFile: boolean ) => { const elements = []; elements.push( } >
{processModelFileList()} ); }; if (processModel) { return ( <> {fileUploadModal()}

Process Model: {processModel.display_name}

{processModel.description}



{processInstanceResultTag()} {processModelButtons()}

Process Instances

{processInstancesUl()} ); } return null; }