|
| 1 | +import { ProcessDefinitionData, PropertiesType, ScenarioGraph, ScenarioGraphWithName, ValidationResult } from "../../types"; |
| 2 | +import { alignFragmentWithSchema } from "../../components/graph/utils/fragmentSchemaAligner"; |
| 3 | +import { fetchProcessDefinition } from "./processDefinitionData"; |
| 4 | +import { Scenario } from "../../components/Process/types"; |
| 5 | +import HttpService from "../../http/HttpService"; |
| 6 | +import { ThunkAction } from "../reduxTypes"; |
| 7 | + |
| 8 | +type EditPropertiesAction = { |
| 9 | + type: "EDIT_PROPERTIES"; |
| 10 | + validationResult: ValidationResult; |
| 11 | + scenarioGraphAfterChange: ScenarioGraph; |
| 12 | +}; |
| 13 | + |
| 14 | +type RenameProcessAction = { |
| 15 | + type: "PROCESS_RENAME"; |
| 16 | + name: string; |
| 17 | +}; |
| 18 | + |
| 19 | +export type PropertiesActions = EditPropertiesAction | RenameProcessAction; |
| 20 | + |
| 21 | +// TODO: We synchronize fragment changes with a scenario in case of properties changes. We need to find a better way to hande it |
| 22 | +function alignFragmentsNodeWithSchema(scenarioGraph: ScenarioGraph, processDefinitionData: ProcessDefinitionData): ScenarioGraph { |
| 23 | + return { |
| 24 | + ...scenarioGraph, |
| 25 | + nodes: scenarioGraph.nodes.map((node) => { |
| 26 | + return node.type === "FragmentInput" ? alignFragmentWithSchema(processDefinitionData, node) : node; |
| 27 | + }), |
| 28 | + }; |
| 29 | +} |
| 30 | + |
| 31 | +const calculateProperties = (scenario: Scenario, changedProperties: PropertiesType): ThunkAction<Promise<ScenarioGraphWithName>> => { |
| 32 | + return async (dispatch) => { |
| 33 | + const processDefinitionData = await dispatch(fetchProcessDefinition(scenario.processingType, scenario.isFragment)); |
| 34 | + const processWithNewFragmentSchema = alignFragmentsNodeWithSchema(scenario.scenarioGraph, processDefinitionData); |
| 35 | + |
| 36 | + if (scenario.name !== changedProperties.name) { |
| 37 | + dispatch({ type: "PROCESS_RENAME", name: changedProperties.name }); |
| 38 | + } |
| 39 | + |
| 40 | + return { |
| 41 | + processName: changedProperties.name, |
| 42 | + scenarioGraph: { ...processWithNewFragmentSchema, properties: changedProperties }, |
| 43 | + }; |
| 44 | + }; |
| 45 | +}; |
| 46 | + |
| 47 | +export function editProperties(scenario: Scenario, changedProperties: PropertiesType): ThunkAction { |
| 48 | + return async (dispatch) => { |
| 49 | + const { processName, scenarioGraph } = await dispatch(calculateProperties(scenario, changedProperties)); |
| 50 | + const response = await HttpService.validateProcess(scenario.name, processName, scenarioGraph); |
| 51 | + |
| 52 | + dispatch({ |
| 53 | + type: "EDIT_PROPERTIES", |
| 54 | + validationResult: response.data, |
| 55 | + scenarioGraphAfterChange: scenarioGraph, |
| 56 | + }); |
| 57 | + }; |
| 58 | +} |
0 commit comments