diff --git a/src/pages/VisProgPage/visualProgrammingUI/components/Plan.tsx b/src/pages/VisProgPage/visualProgrammingUI/components/Plan.tsx index e8161c1..0043c2b 100644 --- a/src/pages/VisProgPage/visualProgrammingUI/components/Plan.tsx +++ b/src/pages/VisProgPage/visualProgrammingUI/components/Plan.tsx @@ -1,4 +1,4 @@ -import { GoalReduce, type GoalNode } from "../nodes/GoalNode" +import { GoalReduce } from "../nodes/GoalNode" import { type Node } from "@xyflow/react" export type Plan = { @@ -35,6 +35,8 @@ export function PlanReduce(_nodes: Node[], plan?: Plan, ) { // Extract the wanted information from a plan element. function StepReduce(planElement: PlanElement, _nodes: Node[]) : Record { // We have different types of plan elements, requiring differnt types of output + const nodes = _nodes + const thisNode = _nodes.find((x) => x.id === planElement.id) switch (planElement.type) { case ("speech"): return { @@ -55,8 +57,6 @@ function StepReduce(planElement: PlanElement, _nodes: Node[]) : Record x.id === planElement.id) return thisNode ? GoalReduce(thisNode, nodes) : {} } } @@ -94,28 +94,4 @@ export function GetActionValue(action: Action) { return returnAction.goal; default: } -} - -/** - * Inserts a goal into a plan - * @param plan: plan to insert goal into - * @param goalNode: the goal node to insert into the plan. - * @returns: a new plan with the goal inside. - */ -export function insertGoalInPlan(plan: Plan, goalNode: GoalNode): Plan { - const planElement : Goal = { - id: goalNode.id, - type: "goal", - } - - return { - ...plan, - steps: [...plan.steps, planElement], - } -} - -export function deleteGoalInPlanByID(plan: Plan, goalID: string): Plan { - return {...plan, - steps: plan.steps.filter((x) => x.id !== goalID) - } } \ No newline at end of file diff --git a/src/pages/VisProgPage/visualProgrammingUI/components/PlanEditingFunctions.tsx b/src/pages/VisProgPage/visualProgrammingUI/components/PlanEditingFunctions.tsx new file mode 100644 index 0000000..19bf62a --- /dev/null +++ b/src/pages/VisProgPage/visualProgrammingUI/components/PlanEditingFunctions.tsx @@ -0,0 +1,34 @@ +// This file is to avoid sharing both functions and components which eslint dislikes. :) +import type { GoalNode } from "../nodes/GoalNode" +import type { Goal, Plan } from "./Plan" + +/** + * Inserts a goal into a plan + * @param plan: plan to insert goal into + * @param goalNode: the goal node to insert into the plan. + * @returns: a new plan with the goal inside. + */ +export function insertGoalInPlan(plan: Plan, goalNode: GoalNode): Plan { + const planElement : Goal = { + id: goalNode.id, + type: "goal", + } + + return { + ...plan, + steps: [...plan.steps, planElement], + } +} + + +/** + * Deletes a goal from a plan + * @param plan: plan to delete goal from + * @param goalID: the goal node to delete. + * @returns: a new plan with the goal removed. + */ +export function deleteGoalInPlanByID(plan: Plan, goalID: string): Plan { + return {...plan, + steps: plan.steps.filter((x) => x.id !== goalID) + } +} \ No newline at end of file diff --git a/src/pages/VisProgPage/visualProgrammingUI/nodes/GoalNode.tsx b/src/pages/VisProgPage/visualProgrammingUI/nodes/GoalNode.tsx index ad0a820..601126a 100644 --- a/src/pages/VisProgPage/visualProgrammingUI/nodes/GoalNode.tsx +++ b/src/pages/VisProgPage/visualProgrammingUI/nodes/GoalNode.tsx @@ -9,10 +9,11 @@ import { TextField } from '../../../../components/TextField'; import {MultiConnectionHandle} from "../components/RuleBasedHandle.tsx"; import {allowOnlyConnectionsFromHandle, allowOnlyConnectionsFromType} from "../HandleRules.ts"; import useFlowStore from '../VisProgStores'; -import { deleteGoalInPlanByID, DoesPlanIterate, insertGoalInPlan, PlanReduce, type Plan, type PlanElement } from '../components/Plan'; +import {DoesPlanIterate, PlanReduce, type Plan } from '../components/Plan'; import PlanEditorDialog from '../components/PlanEditor'; import { MultilineTextField } from '../../../../components/MultilineTextField'; import { defaultPlan } from '../components/Plan.default.ts'; +import { deleteGoalInPlanByID, insertGoalInPlan } from '../components/PlanEditingFunctions.tsx'; /** * The default data dot a phase node diff --git a/src/pages/VisProgPage/visualProgrammingUI/nodes/TriggerNode.tsx b/src/pages/VisProgPage/visualProgrammingUI/nodes/TriggerNode.tsx index e6b778a..aa44cef 100644 --- a/src/pages/VisProgPage/visualProgrammingUI/nodes/TriggerNode.tsx +++ b/src/pages/VisProgPage/visualProgrammingUI/nodes/TriggerNode.tsx @@ -10,11 +10,12 @@ import styles from '../../VisProg.module.css'; import {MultiConnectionHandle, SingleConnectionHandle} from "../components/RuleBasedHandle.tsx"; import {allowOnlyConnectionsFromHandle, allowOnlyConnectionsFromType} from "../HandleRules.ts"; import useFlowStore from '../VisProgStores'; -import { deleteGoalInPlanByID, insertGoalInPlan, PlanReduce, type Plan } from '../components/Plan'; +import {PlanReduce, type Plan } from '../components/Plan'; import PlanEditorDialog from '../components/PlanEditor'; import { BasicBeliefReduce } from './BasicBeliefNode'; import type { GoalNode } from './GoalNode.tsx'; import { defaultPlan } from '../components/Plan.default.ts'; +import { deleteGoalInPlanByID, insertGoalInPlan } from '../components/PlanEditingFunctions.tsx'; /** * The default data structure for a Trigger node