Skip to content

Commit b765c18

Browse files
committed
ContributionState.state --> ContributionState.container; using it in the demo as such
1 parent 75dadf8 commit b765c18

File tree

14 files changed

+75
-77
lines changed

14 files changed

+75
-77
lines changed
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { updateContributionState } from "@/lib";
1+
import { updateContributionContainer } from "@/lib";
22
import type { PanelState } from "@/demo/types";
33

44
export function hidePanel(panelIndex: number) {
5-
updateContributionState<PanelState>("panels", panelIndex, { visible: false });
5+
updateContributionContainer<PanelState>("panels", panelIndex, {
6+
visible: false,
7+
});
68
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { updateContributionState } from "@/lib";
1+
import { updateContributionContainer } from "@/lib";
22
import type { PanelState } from "@/demo/types";
33

44
export function showPanel(panelIndex: number) {
5-
updateContributionState<PanelState>("panels", panelIndex, { visible: true });
5+
updateContributionContainer<PanelState>("panels", panelIndex, {
6+
visible: true,
7+
});
68
}

dashi/src/demo/components/Panel.tsx

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import type { CSSProperties, ReactElement } from "react";
22
import CircularProgress from "@mui/material/CircularProgress";
3-
4-
import {
5-
type ComponentChangeHandler,
6-
type ContributionState,
7-
Component,
8-
} from "@/lib";
3+
import { Component } from "@/lib";
4+
import type { ComponentState, ComponentChangeHandler } from "@/lib";
95
import type { PanelState } from "@/demo/types";
106

117
const panelContainerStyle: CSSProperties = {
@@ -32,39 +28,43 @@ const panelContentStyle: CSSProperties = {
3228
padding: 2,
3329
};
3430

35-
interface PanelProps extends ContributionState<PanelState> {
31+
interface PanelProps extends PanelState {
32+
componentProps?: ComponentState;
33+
componentStatus?: string;
34+
componentError?: { message: string };
3635
onChange: ComponentChangeHandler;
3736
}
3837

3938
function Panel({
40-
name,
41-
state,
42-
component,
43-
componentResult,
39+
title,
40+
visible,
41+
componentProps,
42+
componentStatus,
43+
componentError,
4444
onChange,
4545
}: PanelProps) {
46-
if (!state.visible) {
46+
if (!visible) {
4747
return null;
4848
}
4949
let panelElement: ReactElement | null = null;
50-
if (component) {
51-
panelElement = <Component {...component} onChange={onChange} />;
52-
} else if (componentResult.error) {
50+
if (componentProps) {
51+
panelElement = <Component {...componentProps} onChange={onChange} />;
52+
} else if (componentError) {
5353
panelElement = (
5454
<span>
55-
Error loading {name}: {componentResult.error.message}
55+
Error loading panel {title}: {componentError.message}
5656
</span>
5757
);
58-
} else if (componentResult.status === "pending") {
58+
} else if (componentStatus === "pending") {
5959
panelElement = (
6060
<span>
61-
<CircularProgress size={30} color="secondary" /> Loading {name}...
61+
<CircularProgress size={30} color="secondary" /> Loading {title}...
6262
</span>
6363
);
6464
}
6565
return (
6666
<div style={panelContainerStyle}>
67-
<div style={panelHeaderStyle}>{state.title}</div>
67+
<div style={panelHeaderStyle}>{title}</div>
6868
<div style={panelContentStyle}>{panelElement}</div>
6969
</div>
7070
);

dashi/src/demo/components/PanelsControl.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@ function PanelsControl() {
1717
<FormGroup sx={{ display: "flex", flexDirection: "row" }}>
1818
{panelStates.map((panelState, panelIndex) => {
1919
const id = `panels.${panelIndex}`;
20+
const { title, visible } = panelState.container;
2021
return (
2122
<FormControlLabel
2223
key={panelIndex}
23-
label={panelState.state.title}
24+
label={title}
2425
control={
2526
<Checkbox
2627
color="secondary"
2728
id={id}
28-
checked={panelState.state.visible || false}
29+
checked={visible || false}
2930
value={panelIndex}
3031
onChange={(e) => {
3132
if (e.currentTarget.checked) {

dashi/src/demo/components/PanelsRow.tsx

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import type { JSX } from "react";
2-
31
import { type ComponentChangeEvent, handleComponentChange } from "@/lib";
42
import { usePanelStates } from "@/demo/hooks";
53
import Panel from "./Panel";
@@ -17,23 +15,21 @@ function PanelsRow() {
1715
) => {
1816
handleComponentChange("panels", panelIndex, panelEvent);
1917
};
20-
const visiblePanels: JSX.Element[] = [];
21-
panelStates.forEach((panelState, panelIndex) => {
22-
if (panelState.state.visible) {
23-
visiblePanels.push(
24-
<Panel
25-
key={panelIndex}
26-
{...panelState}
27-
onChange={(e) => handlePanelChange(panelIndex, e)}
28-
/>,
29-
);
30-
}
18+
const panels = panelStates.map((panelState, panelIndex) => {
19+
const { container, component, componentResult } = panelState;
20+
return (
21+
<Panel
22+
key={panelIndex}
23+
{...container}
24+
componentProps={component}
25+
componentStatus={componentResult.status}
26+
componentError={componentResult.error}
27+
onChange={(e) => handlePanelChange(panelIndex, e)}
28+
/>
29+
);
3130
});
32-
const panelElements = <>{visiblePanels}</>;
3331
return (
34-
<div style={{ display: "flex", gap: 5, paddingTop: 10 }}>
35-
{panelElements}
36-
</div>
32+
<div style={{ display: "flex", gap: 5, paddingTop: 10 }}>{panels}</div>
3733
);
3834
}
3935

dashi/src/lib/actions/helpers/applyStateChangeRequests.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe("Test that applyContributionChangeRequests()", () => {
3535
name: "",
3636
extension: "",
3737
componentResult: { status: "ok" },
38-
state: { visible: true },
38+
container: { visible: true },
3939
component: componentTree,
4040
},
4141
],

dashi/src/lib/actions/helpers/applyStateChangeRequests.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,30 +69,27 @@ export function applyContributionChangeRequests(
6969
stateChangeRequests.forEach(
7070
({ contribPoint, contribIndex, stateChanges }) => {
7171
const contribution = contributionsRecord[contribPoint][contribIndex];
72-
const state = applyStateChanges(
73-
contribution.state,
74-
stateChanges.filter(
75-
(stateChange) => stateChange.link === "container" && !stateChange.id,
76-
),
72+
const container = applyStateChanges(
73+
contribution.container,
74+
stateChanges.filter((stateChange) => stateChange.link === "container"),
7775
);
7876
const component = applyComponentStateChanges(
7977
contribution.component,
8078
stateChanges.filter(
8179
(stateChange) =>
82-
(!stateChange.link || stateChange.link === "component") &&
83-
stateChange.id,
80+
!stateChange.link || stateChange.link === "component",
8481
),
8582
);
8683
if (
87-
state !== contribution.state ||
84+
container !== contribution.container ||
8885
component !== contribution.component
8986
) {
9087
contributionsRecord = {
9188
...contributionsRecord,
9289
[contribPoint]: updateArray<ContributionState>(
9390
contributionsRecord[contribPoint],
9491
contribIndex,
95-
{ ...contribution, state, component },
92+
{ ...contribution, container, component },
9693
),
9794
};
9895
}

dashi/src/lib/actions/helpers/getInputValues.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { describe, it, expect } from "vitest";
22

33
import type { ComponentState, PlotState } from "@/lib/types/state/component";
44
import {
5-
getComponentStateValue,
5+
getInputValueFromComponent,
66
getInputValueFromState,
77
} from "./getInputValues";
88

@@ -23,10 +23,10 @@ const componentState: ComponentState = {
2323
value: 14,
2424
};
2525

26-
describe("Test that getComponentStateValue()", () => {
26+
describe("Test that getInputValueFromComponent()", () => {
2727
it("works on 1st level", () => {
2828
expect(
29-
getComponentStateValue(componentState, {
29+
getInputValueFromComponent(componentState, {
3030
link: "component",
3131
id: "b1",
3232
property: "value",
@@ -36,7 +36,7 @@ describe("Test that getComponentStateValue()", () => {
3636

3737
it("works on 2nd level", () => {
3838
expect(
39-
getComponentStateValue(componentState, {
39+
getInputValueFromComponent(componentState, {
4040
link: "component",
4141
id: "p1",
4242
property: "chart",
@@ -46,15 +46,15 @@ describe("Test that getComponentStateValue()", () => {
4646

4747
it("works on 3rd level", () => {
4848
expect(
49-
getComponentStateValue(componentState, {
49+
getInputValueFromComponent(componentState, {
5050
link: "component",
5151
id: "cb1",
5252
property: "value",
5353
}),
5454
).toEqual(true);
5555

5656
expect(
57-
getComponentStateValue(componentState, {
57+
getInputValueFromComponent(componentState, {
5858
link: "component",
5959
id: "dd1",
6060
property: "value",

dashi/src/lib/actions/helpers/getInputValues.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ export function getInputValue<S extends object = object>(
2525
const dataSource = input.link || "component";
2626
if (dataSource === "component" && contributionState.component) {
2727
// Return value of a property of some component in the tree
28-
inputValue = getComponentStateValue(contributionState.component, input);
29-
} else if (dataSource === "container" && contributionState.state) {
28+
inputValue = getInputValueFromComponent(contributionState.component, input);
29+
} else if (dataSource === "container" && contributionState.container) {
3030
inputValue = getInputValueFromState(input, hostState);
3131
} else if (dataSource === "app" && hostState) {
3232
inputValue = getInputValueFromState(input, hostState);
@@ -42,7 +42,7 @@ export function getInputValue<S extends object = object>(
4242
}
4343

4444
// we export for testing only
45-
export function getComponentStateValue(
45+
export function getInputValueFromComponent(
4646
componentState: ComponentState,
4747
input: Input,
4848
): unknown {
@@ -53,7 +53,7 @@ export function getComponentStateValue(
5353
} else if (isContainerState(componentState)) {
5454
for (let i = 0; i < componentState.components.length; i++) {
5555
const item = componentState.components[i];
56-
const itemValue = getComponentStateValue(item, input);
56+
const itemValue = getInputValueFromComponent(item, input);
5757
if (itemValue !== noValue) {
5858
return itemValue;
5959
}

dashi/src/lib/actions/initializeContributions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function newContributionState(
4949
): ContributionState {
5050
return {
5151
...rawContribution,
52-
state: { ...rawContribution.initialState },
52+
container: { ...rawContribution.initialState },
5353
componentResult: {},
5454
};
5555
}

0 commit comments

Comments
 (0)