Skip to content

Commit b75c3d8

Browse files
committed
No need to have local state in dashi UI components
1 parent afc8e48 commit b75c3d8

File tree

3 files changed

+40
-22
lines changed

3 files changed

+40
-22
lines changed

dashi/src/actions/handleComponentPropertyChange.ts

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,28 @@ export default function handleComponentPropertyChange(
6161
}
6262
}
6363
});
64+
const originalChangeRequest: ChangeRequest = {
65+
contribPoint,
66+
contribIndex,
67+
changes: [
68+
{
69+
kind: "Component",
70+
id: componentId,
71+
property: componentPropertyName,
72+
value: componentPropertyValue,
73+
},
74+
],
75+
};
6476
console.debug("callRequests", callRequests);
65-
if (callRequests.length) {
77+
if (callRequests.length == 0) {
78+
applyChangeRequests([originalChangeRequest]);
79+
} else {
6680
fetchApiResult(fetchChangeRequests, callRequests).then(
6781
(changeRequestsResult) => {
6882
if (changeRequestsResult.data) {
69-
applyChangeRequests(changeRequestsResult.data);
83+
applyChangeRequests(
84+
[originalChangeRequest].concat(changeRequestsResult.data),
85+
);
7086
} else {
7187
console.error(
7288
"callback failed:",
@@ -81,23 +97,28 @@ export default function handleComponentPropertyChange(
8197
}
8298

8399
function applyChangeRequests(changeRequests: ChangeRequest[]) {
84-
console.log("processing call results", changeRequests);
100+
console.log("applying change requests", changeRequests);
85101
changeRequests.forEach(({ contribPoint, contribIndex, changes }) => {
86-
console.log("processing output of", contribPoint, contribIndex, changes);
102+
console.log(
103+
"processing change request",
104+
contribPoint,
105+
contribIndex,
106+
changes,
107+
);
87108
const contributionStatesRecord =
88109
appStore.getState().contributionStatesRecord;
89110
const contributionStates = contributionStatesRecord[contribPoint];
90111
const contributionState = contributionStates[contribIndex];
91112
const componentModelOld = contributionState.componentModelResult.data;
92113
let componentModel = componentModelOld;
93-
changes.forEach((output) => {
94-
if (componentModel && (!output.kind || output.kind === "Component")) {
95-
componentModel = updateComponentState(componentModel, output);
114+
changes.forEach((change) => {
115+
if (componentModel && (!change.kind || change.kind === "Component")) {
116+
componentModel = updateComponentState(componentModel, change);
96117
} else {
97118
// TODO: process other output kinds which may not require componentModel.
98119
console.warn(
99120
"processing of this kind of output not supported yet:",
100-
output,
121+
change,
101122
);
102123
}
103124
});
@@ -124,16 +145,16 @@ function applyChangeRequests(changeRequests: ChangeRequest[]) {
124145

125146
function updateComponentState(
126147
componentModel: ComponentModel,
127-
output: Change,
148+
change: Change,
128149
): ComponentModel {
129-
if (componentModel.id === output.id) {
130-
return { ...componentModel, [output.property]: output.value };
150+
if (componentModel.id === change.id) {
151+
return { ...componentModel, [change.property]: change.value };
131152
} else if (isContainerModel(componentModel)) {
132153
const containerModelOld: ContainerModel = componentModel;
133154
let containerModelNew: ContainerModel = containerModelOld;
134155
for (let i = 0; i < containerModelOld.components.length; i++) {
135156
const itemOld = containerModelOld.components[i];
136-
const itemNew = updateComponentState(itemOld, output);
157+
const itemNew = updateComponentState(itemOld, change);
137158
if (itemNew !== itemOld) {
138159
if (containerModelNew === containerModelOld) {
139160
containerModelNew = {

dashi/src/components/DashiDropdown.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import "react";
22
import { DropdownModel, PropertyChangeHandler } from "../model/component";
3-
import React from "react";
43
import {
54
// Box,
65
FormControl,
@@ -24,7 +23,6 @@ function DashiDropdown({
2423
label,
2524
onPropertyChange,
2625
}: DashiDropdownProps) {
27-
const [dropdownValue, setDropdownValue] = React.useState(value);
2826
const handleChange = (event: SelectChangeEvent) => {
2927
if (!id) {
3028
return;
@@ -34,7 +32,6 @@ function DashiDropdown({
3432
newValue = Number.parseInt(newValue);
3533
}
3634

37-
setDropdownValue(newValue);
3835
return onPropertyChange({
3936
componentType: "Dropdown",
4037
componentId: id,
@@ -49,7 +46,7 @@ function DashiDropdown({
4946
id={id}
5047
name={name}
5148
style={style}
52-
value={`${dropdownValue}`}
49+
value={`${value}`}
5350
disabled={disabled}
5451
onChange={handleChange}
5552
variant={"filled"}

dashipy/dashipy/server.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def post(self):
135135
context: Context = self.settings[DASHI_CONTEXT_KEY]
136136

137137
# TODO: assert correctness, set status code on error
138-
call_results: list[dict] = []
138+
change_requests: list[dict] = []
139139
for call_request in call_requests:
140140
contrib_point_name: str = call_request["contribPoint"]
141141
contrib_index: int = call_request["contribIndex"]
@@ -151,10 +151,10 @@ def post(self):
151151
if len(callback.outputs) == 1:
152152
output_values = (output_values,)
153153

154-
computed_outputs: list[dict] = []
154+
changes: list[dict] = []
155155
for output_index, output in enumerate(callback.outputs):
156156
output_value = output_values[output_index]
157-
computed_outputs.append(
157+
changes.append(
158158
{
159159
**output.to_dict(),
160160
"value": (
@@ -167,16 +167,16 @@ def post(self):
167167
}
168168
)
169169

170-
call_results.append(
170+
change_requests.append(
171171
{
172172
"contribPoint": contrib_point_name,
173173
"contribIndex": contrib_index,
174-
"computedOutputs": computed_outputs,
174+
"changes": changes,
175175
}
176176
)
177177

178178
self.set_header("Content-Type", "text/json")
179-
self.write({"result": call_results})
179+
self.write({"result": change_requests})
180180

181181

182182
def print_usage(app, port):

0 commit comments

Comments
 (0)