Skip to content

Commit 78efef2

Browse files
committed
We now create an initial contribution state from contribution
1 parent 377884d commit 78efef2

File tree

7 files changed

+79
-15
lines changed

7 files changed

+79
-15
lines changed

dashi/src/actions/initAppStore.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@ export function initAppStore() {
2020
(contribPoint) => {
2121
const contributions: Contribution[] =
2222
contributionsRecordResult.data![contribPoint];
23-
contributionStatesRecord[contribPoint] = contributions.map(() => ({
24-
componentModelResult: {},
25-
}));
23+
contributionStatesRecord[contribPoint] = contributions.map(
24+
(contribution) => ({
25+
title: contribution.title,
26+
visible: contribution.visible,
27+
componentStateResult: {},
28+
}),
29+
);
2630
},
2731
);
2832
set({ contributionsRecordResult, contributionStatesRecord });

dashi/src/actions/setComponentVisibility.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ export function setComponentVisibility(
1616
if (contributionState.visible === visible) {
1717
return; // nothing to do
1818
}
19-
if (contributionState.componentModelResult.status) {
19+
if (contributionState.componentStateResult.status) {
2020
updateContributionState(contribPoint, panelIndex, { visible });
2121
} else {
2222
// No status yet, so we must load the component
2323
updateContributionState(contribPoint, panelIndex, {
2424
visible,
25-
componentModelResult: { status: "pending" },
25+
componentStateResult: { status: "pending" },
2626
});
2727
const inputValues = getLayoutInputValues(contribPoint, panelIndex);
2828
fetchApiResult(
@@ -33,7 +33,7 @@ export function setComponentVisibility(
3333
).then((componentModelResult) => {
3434
const componentState = componentModelResult?.data;
3535
updateContributionState(contribPoint, panelIndex, {
36-
componentModelResult,
36+
componentStateResult: componentModelResult,
3737
componentState,
3838
});
3939
});

dashi/src/app/Panel.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { CSSProperties, ReactElement } from "react";
2-
3-
import DashiComponent from "../components/DashiComponent";
42
import { CircularProgress } from "@mui/material";
5-
import { PanelModel } from "../state/panel";
6-
import { ContributionState } from "../state/contribution";
3+
74
import { PropertyChangeHandler } from "../model/event";
5+
import { Contribution } from "../model/contribution";
6+
import { ContributionState } from "../state/contribution";
7+
import DashiComponent from "../components/DashiComponent";
88

99
const panelContainerStyle: CSSProperties = {
1010
display: "flex",
@@ -31,7 +31,7 @@ const panelContentStyle: CSSProperties = {
3131
};
3232

3333
interface PanelProps {
34-
panelModel: PanelModel;
34+
panelModel: Contribution;
3535
panelState: ContributionState;
3636
onPropertyChange: PropertyChangeHandler;
3737
}
@@ -42,7 +42,7 @@ function Panel({ panelModel, panelState, onPropertyChange }: PanelProps) {
4242
}
4343
const componentState = panelState.componentState;
4444
let panelElement: ReactElement | null = null;
45-
const componentModelResult = panelState.componentModelResult;
45+
const componentModelResult = panelState.componentStateResult;
4646
if (componentModelResult.data && componentState) {
4747
panelElement = (
4848
<DashiComponent {...componentState} onPropertyChange={onPropertyChange} />
@@ -63,7 +63,7 @@ function Panel({ panelModel, panelState, onPropertyChange }: PanelProps) {
6363
}
6464
return (
6565
<div style={panelContainerStyle}>
66-
<div style={panelHeaderStyle}>{panelModel.title}</div>
66+
<div style={panelHeaderStyle}>{panelState.title}</div>
6767
<div style={panelContentStyle}>{panelElement}</div>
6868
</div>
6969
);

dashi/src/app/PanelsRow.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function PanelsRow() {
1818
let panelElements: ReactElement | null = null;
1919
if (contributionsRecordResult.data) {
2020
// TODO: Validate that PanelModel contains a title (It should be valid).
21-
// It can be done in one central place and not everytime we need to render a panel
21+
// It can be done in one central place and not everytime we need to render a panelModel
2222
const panelModels = contributionsRecordResult.data[
2323
contribPoint
2424
] as PanelModel[];

dashi/src/model/contribution.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@ export interface Contribution {
55
extension: string;
66
layout?: Callback;
77
callbacks?: Callback[];
8+
// The following properties will become the
9+
// initial contribution state
10+
title?: string;
11+
visible?: boolean;
812
}

dashi/src/state/contribution.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { ApiResult } from "../utils/fetchApiResult";
22
import { ComponentState } from "./component";
33

44
export interface ContributionState {
5+
title?: string;
56
visible?: boolean;
6-
componentModelResult: ApiResult<ComponentState>;
7+
componentStateResult: ApiResult<ComponentState>;
8+
// componentStateResult.data
79
componentState?: ComponentState;
810
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from typing import Any, Callable
2+
from abc import ABC
3+
4+
from .callback import Callback, Output, Input
5+
6+
7+
class Contribution(ABC):
8+
# noinspection PyShadowingBuiltins
9+
def __init__(self, name: str, title: str | None = None):
10+
self.name = name
11+
self.title = title
12+
self.extension: str | None = None
13+
self.layout_callback: Callback | None = None
14+
self.callbacks: list[Callback] = []
15+
16+
def to_dict(self) -> dict[str, Any]:
17+
d = dict(name=self.name)
18+
if self.title is not None:
19+
d.update(title=self.title)
20+
if self.extension is not None:
21+
d.update(extension=self.extension)
22+
if self.layout_callback is not None:
23+
d.update(layout=self.layout_callback.to_dict())
24+
if self.callbacks:
25+
d.update(callbacks=[cb.to_dict() for cb in self.callbacks])
26+
return d
27+
28+
def layout(self, *args) -> Callable:
29+
"""Decorator."""
30+
31+
def decorator(function: Callable) -> Callable:
32+
self.layout_callback = Callback.from_decorator(
33+
"layout", args, function, outputs_allowed=False
34+
)
35+
return function
36+
37+
return decorator
38+
39+
def callback(self, *args: Input | Output) -> Callable[[Callable], Callable]:
40+
"""Decorator."""
41+
42+
def decorator(function: Callable) -> Callable:
43+
self.callbacks.append(
44+
Callback.from_decorator(
45+
"callback", args, function, outputs_allowed=True
46+
)
47+
)
48+
return function
49+
50+
return decorator
51+
52+
def __str__(self):
53+
return self.name
54+

0 commit comments

Comments
 (0)