import { useContext, useEffect, useState } from 'react'; import { Link, useNavigate, useParams } from 'react-router-dom'; import { Add, Upload, Download, TrashCan, Favorite, Edit, View, ArrowRight, // @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 { Can } from '@casl/react'; import ProcessBreadcrumb from '../components/ProcessBreadcrumb'; import HttpService from '../services/HttpService'; import ErrorContext from '../contexts/ErrorContext'; import { getGroupFromModifiedModelId, modifyProcessIdentifierForPathParam, } from '../helpers'; import { PermissionsToCheck, ProcessFile, ProcessInstance, ProcessModel, } from '../interfaces'; import ButtonWithConfirmation from '../components/ButtonWithConfirmation'; import ProcessInstanceListTable from '../components/ProcessInstanceListTable'; import { usePermissionFetcher } from '../hooks/PermissionService'; import { useUriListForPermissions } from '../hooks/UriListForPermissions'; import ProcessInstanceRun from '../components/ProcessInstanceRun'; import { Notification } from '../components/Notification'; export default function ProcessModelShow() { const params = useParams(); const setErrorMessage = (useContext as any)(ErrorContext)[1]; const [processModel, setProcessModel] = useState(null); const [processInstance, setProcessInstance] = useState(null); const [reloadModel, setReloadModel] = useState(false); const [filesToUpload, setFilesToUpload] = useState(null); const [showFileUploadModal, setShowFileUploadModal] = useState(false); const [processModelPublished, setProcessModelPublished] = useState(null); const [publishDisabled, setPublishDisabled] = useState(false); const navigate = useNavigate(); const { targetUris } = useUriListForPermissions(); const permissionRequestData: PermissionsToCheck = { [targetUris.processModelShowPath]: ['PUT', 'DELETE'], [targetUris.processModelPublishPath]: ['POST'], [targetUris.processInstanceListPath]: ['GET'], [targetUris.processInstanceCreatePath]: ['POST'], [targetUris.processModelFileCreatePath]: ['POST', 'PUT', 'GET', 'DELETE'], }; const { ability, permissionsLoaded } = usePermissionFetcher( permissionRequestData ); const modifiedProcessModelId = modifyProcessIdentifierForPathParam( `${params.process_model_id}` ); useEffect(() => { const processResult = (result: ProcessModel) => { setProcessModel(result); setReloadModel(false); }; HttpService.makeCallToBackend({ path: `/process-models/${modifiedProcessModelId}`, successCallback: processResult, }); }, [reloadModel, modifiedProcessModelId]); const processInstanceRunResultTag = () => { if (processInstance) { return ( setProcessInstance(null)} > view ); } 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 navigateToProcessModels = (_result: any) => { navigate( `/admin/process-groups/${getGroupFromModifiedModelId( modifiedProcessModelId )}` ); }; const deleteProcessModel = () => { HttpService.makeCallToBackend({ path: `/process-models/${modifiedProcessModelId}`, successCallback: navigateToProcessModels, httpMethod: 'DELETE', }); }; const postPublish = (value: any) => { setPublishDisabled(false); setProcessModelPublished(value); }; const publishProcessModel = () => { setPublishDisabled(true); setProcessModelPublished(null); HttpService.makeCallToBackend({ path: `/process-models/${modifiedProcessModelId}/publish`, successCallback: postPublish, httpMethod: 'POST', }); }; const navigateToFileEdit = (processModelFile: ProcessFile) => { const url = profileModelFileEditUrl(processModelFile); if (url) { navigate(url); } }; const renderButtonElements = ( processModelFile: ProcessFile, isPrimaryBpmnFile: boolean ) => { const elements = []; let icon = View; let actionWord = 'View'; if (ability.can('PUT', targetUris.processModelFileCreatePath)) { icon = Edit; actionWord = 'Edit'; } elements.push( } >
{processModelFileList()} ); }; const processInstanceListTableButton = () => { if (processModel) { return (

Process Instances

{processModel.description}

<>

{processModelFilesSection()} {processInstanceListTableButton()} ); } return null; }