|
1 | | -import appStore, { ContribPoint } from "../store/appStore"; |
2 | | -import { PropertyChangeEvent } from "../model/component"; |
3 | | -import { CallbackCallRequest, CallbackCallResult } from "../model/callback"; |
| 1 | +import appStore, { ContribPoint, ContributionState } from "../store/appStore"; |
| 2 | +import { |
| 3 | + ComponentModel, |
| 4 | + ContainerModel, |
| 5 | + isContainerModel, |
| 6 | + PropertyChangeEvent, |
| 7 | +} from "../model/component"; |
| 8 | +import { |
| 9 | + CallbackCallRequest, |
| 10 | + CallbackCallResult, |
| 11 | + ComputedOutput, |
| 12 | +} from "../model/callback"; |
4 | 13 | import fetchApiResult from "../utils/fetchApiResult"; |
5 | 14 | import { fetchCallbackCallResults } from "../api"; |
| 15 | +import { updateArray } from "../utils/updateArray"; |
6 | 16 |
|
7 | 17 | export default function handleComponentPropertyChange( |
8 | 18 | contribPoint: ContribPoint, |
@@ -68,24 +78,75 @@ export default function handleComponentPropertyChange( |
68 | 78 | } |
69 | 79 |
|
70 | 80 | function applyCallbackCallResults(callResults: CallbackCallResult[]) { |
71 | | - // TODO: process call result --> modify any targets in appState. |
72 | | - console.warn( |
73 | | - "processing of callback results not implemented yet:", |
74 | | - callResults, |
75 | | - ); |
76 | | - //const contributionPoints = appStore.getState().contributionPointsResult.data!; |
77 | | - callResults.forEach( |
78 | | - ({ contribPoint: _1, contribIndex: _2, computedOutputs }) => { |
79 | | - //const contributions = contributionPoints[contribPoint]; |
80 | | - //const contributionModel = contributions[contribIndex]; |
81 | | - //const contributionState = { ...appStore.getState().panelStates[c] }; |
82 | | - computedOutputs.forEach((output) => { |
83 | | - if (!output.kind || output.kind === "Component") { |
84 | | - //const componentId = output.id; |
85 | | - //const propertyName = output.property; |
86 | | - //const propertyValue = output.value; |
87 | | - } |
| 81 | + console.log("processing call results", callResults); |
| 82 | + callResults.forEach(({ contribPoint, contribIndex, computedOutputs }) => { |
| 83 | + console.log( |
| 84 | + "processing output of", |
| 85 | + contribPoint, |
| 86 | + contribIndex, |
| 87 | + computedOutputs, |
| 88 | + ); |
| 89 | + const contributionStatesRecord = |
| 90 | + appStore.getState().contributionStatesRecord; |
| 91 | + const contributionStates = contributionStatesRecord[contribPoint]; |
| 92 | + const contributionState = contributionStates[contribIndex]; |
| 93 | + const componentModelOld = contributionState.componentModelResult.data; |
| 94 | + let componentModel = componentModelOld; |
| 95 | + computedOutputs.forEach((output) => { |
| 96 | + if (componentModel && (!output.kind || output.kind === "Component")) { |
| 97 | + componentModel = updateComponentState(componentModel, output); |
| 98 | + } else { |
| 99 | + // TODO: process other output kinds which may not require componentModel. |
| 100 | + console.warn( |
| 101 | + "processing of this kind of output not supported yet:", |
| 102 | + output, |
| 103 | + ); |
| 104 | + } |
| 105 | + }); |
| 106 | + if (componentModel && componentModel !== componentModelOld) { |
| 107 | + appStore.setState({ |
| 108 | + contributionStatesRecord: { |
| 109 | + ...contributionStatesRecord, |
| 110 | + [contribPoint]: updateArray<ContributionState>( |
| 111 | + contributionStates, |
| 112 | + contribIndex, |
| 113 | + { |
| 114 | + ...contributionState, |
| 115 | + componentModelResult: { |
| 116 | + ...contributionState.componentModelResult, |
| 117 | + data: componentModel, |
| 118 | + }, |
| 119 | + }, |
| 120 | + ), |
| 121 | + }, |
88 | 122 | }); |
89 | | - }, |
90 | | - ); |
| 123 | + } |
| 124 | + }); |
| 125 | +} |
| 126 | + |
| 127 | +function updateComponentState( |
| 128 | + componentModel: ComponentModel, |
| 129 | + output: ComputedOutput, |
| 130 | +): ComponentModel { |
| 131 | + if (componentModel.id === output.id) { |
| 132 | + return { ...componentModel, [output.property]: output.value }; |
| 133 | + } else if (isContainerModel(componentModel)) { |
| 134 | + const containerModelOld: ContainerModel = componentModel; |
| 135 | + let containerModelNew: ContainerModel = containerModelOld; |
| 136 | + for (let i = 0; i < containerModelOld.components.length; i++) { |
| 137 | + const itemOld = containerModelOld.components[i]; |
| 138 | + const itemNew = updateComponentState(itemOld, output); |
| 139 | + if (itemNew !== itemOld) { |
| 140 | + if (containerModelNew === containerModelOld) { |
| 141 | + containerModelNew = { |
| 142 | + ...containerModelOld, |
| 143 | + components: [...containerModelOld.components], |
| 144 | + }; |
| 145 | + } |
| 146 | + containerModelNew.components[i] = itemNew; |
| 147 | + } |
| 148 | + } |
| 149 | + return containerModelNew; |
| 150 | + } |
| 151 | + return componentModel; |
91 | 152 | } |
0 commit comments