Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get parent id of steps? #49

Closed
lechuhuuha opened this issue Jun 5, 2023 · 4 comments
Closed

How to get parent id of steps? #49

lechuhuuha opened this issue Jun 5, 2023 · 4 comments

Comments

@lechuhuuha
Copy link

Hi,
I want to get the parent id of each step in the sequence. Like in this example sequence :

{
  "properties": {},
  "sequence": [
    {
      "id": "3e3c74a32774273fa5fbbe9ee2467be0",
      "componentType": "largeTask",
      "type": "welcome-new-subscriber",
      "name": "Welcome new subscriber",
      "properties": {
        "draggable": false,
        "deletable": true,
        "type": "trigger"
      }
    },
    {
      "id": "28429992da9bc6f5902a440b69fe1743",
      "componentType": "switch",
      "type": "if",
      "name": "Open email ?",
      "branches": {
        "yes": [
          {
            "id": "bd10bcc08104adec84b821dca3697e9b",
            "componentType": "largeTask",
            "type": "add-tag",
            "name": "Add tag",
            "properties": {
              "draggable": true,
              "deletable": true,
              "type": "action"
            }
          }
        ],
        "no": []
      },
      "properties": {
        "draggable": true,
        "type": "condition"
      }
    }
  ]
}

is there any built in function to get the parent id and convert the json into this

{
  "properties": {},
  "sequence": [
    {
      "id": "3e3c74a32774273fa5fbbe9ee2467be0",
      "componentType": "largeTask",
      "type": "welcome-new-subscriber",
      "name": "Welcome new subscriber",
      "properties": {
        "draggable": false,
        "deletable": true,
        "type": "trigger"
      },
      "child" : "28429992da9bc6f5902a440b69fe1743"
    },
    {
      "id": "28429992da9bc6f5902a440b69fe1743",
      "componentType": "switch",
      "type": "if",
      "name": "Open email ?",
      "branches": {
        "yes": [
          {
            "id": "bd10bcc08104adec84b821dca3697e9b",
            "componentType": "largeTask",
            "type": "add-tag",
            "name": "Add tag",
            "properties": {
              "draggable": true,
              "deletable": true,
              "type": "action"
            }
          }
        ],
        "no": []
      },
      "properties": {
        "draggable": true,
        "type": "condition"
      },
      "child" : null
    }
  ]
}

have the properties child corresponding for each step
thanks

@b4rtaz
Copy link
Collaborator

b4rtaz commented Jun 5, 2023

Check the getStepParents method of the Designer class.

const step: Step = ...;
const parents = designer.getStepParents(step);
const parent = parents[parents.length - 1];

This method returns a path to a passed step. To get parent you need to read one before last item.

@b4rtaz
Copy link
Collaborator

b4rtaz commented Jun 5, 2023

BTW: your suggestion would cause that the model would contain data redundancy. What is completely wrong. Now the model contains all information what you need. You can easily get an access to previous/next/parent/child step, but it requires a definition search.

@b4rtaz
Copy link
Collaborator

b4rtaz commented Jun 9, 2023

@lechuhuuha is the problem solved?

@lechuhuuha
Copy link
Author

lechuhuuha commented Jun 9, 2023

@lechuhuuha is the problem solved?

Thanks
i just loop in the sequence and get the next index of step then its work perfect

Here is the code

designer = sequentialWorkflowDesigner.Designer.create(placeholder, startDefinition, configuration);
designer.onDefinitionChanged.subscribe(newDefinition => {
	refreshValidationStatus();
	if (!newDefinition && !newDefinition.sequence) {
		return;
	}

	const modifiedArray = [];
	processBranches(newDefinition.sequence, modifiedArray);

	console.clear();
	console.log(JSON.stringify(modifiedArray, null, 2));
	// console.log('the definition has changed', newDefinition.sequence);
});

function processBranches(branches, modifiedArray) {
	branches.forEach((element, index) => {
		const modifiedElement = { ...element };
		const childIndex = `${index + 1}`;

		if (branches[childIndex]) {
			modifiedElement.child = branches[childIndex].id;
		}

		modifiedArray.push(modifiedElement);

		if (modifiedElement.componentType === 'switch') {
			const yesBranch = modifiedElement.branches.yes;
			const noBranch = modifiedElement.branches.no;

			if (yesBranch.length > 0) {
				processBranches(yesBranch, modifiedArray);
			}
			if (noBranch.length > 0) {
				processBranches(noBranch, modifiedArray);
			}
		}
	});
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants