import { useContext, useEffect, useState } from 'react';
import { Link, useNavigate, useParams } from 'react-router-dom';
import Form from '@rjsf/core';
import { Button, Stack } from 'react-bootstrap';
import HttpService from '../services/HttpService';
import ErrorContext from '../contexts/ErrorContext';
export default function TaskShow() {
const [task, setTask] = useState(null);
const [userTasks, setUserTasks] = useState(null);
const params = useParams();
const navigate = useNavigate();
const setErrorMessage = (useContext as any)(ErrorContext)[1];
useEffect(() => {
setErrorMessage('');
HttpService.makeCallToBackend({
path: `/tasks/${params.process_instance_id}/${params.task_id}`,
successCallback: setTask,
failureCallback: setErrorMessage,
});
HttpService.makeCallToBackend({
path: `/process-instance/${params.process_instance_id}/tasks`,
successCallback: setUserTasks,
});
}, [params, setErrorMessage]);
const processSubmitResult = (result: any) => {
setErrorMessage('');
if (result.ok) {
navigate(`/tasks`);
} else if (result.process_instance_id) {
navigate(`/tasks/${result.process_instance_id}/${result.id}`);
} else {
setErrorMessage(`Received unexpected error: ${result.message}`);
}
};
const handleFormSubmit = (event: any) => {
setErrorMessage('');
const dataToSubmit = event.formData;
delete dataToSubmit.isManualTask;
HttpService.makeCallToBackend({
path: `/tasks/${params.process_instance_id}/${params.task_id}`,
successCallback: processSubmitResult,
failureCallback: setErrorMessage,
httpMethod: 'PUT',
postBody: dataToSubmit,
});
};
const buildTaskNavigation = () => {
let userTasksElement;
if (userTasks) {
userTasksElement = (userTasks as any).map(function getUserTasksElement(
userTask: any
) {
const taskUrl = `/tasks/${params.process_instance_id}/${userTask.id}`;
if (userTask.id === params.task_id) {
return {userTask.name};
}
if (userTask.state === 'COMPLETED') {
return (
{userTask.name}
);
}
if (userTask.state === 'FUTURE') {
return {userTask.name};
}
if (userTask.state === 'READY') {
return (
{userTask.name} - Current
);
}
return null;
});
}
return (