Skip to content

Commit 3bbe5dd

Browse files
committed
refactoring 2 actions
1 parent 9138cf2 commit 3bbe5dd

File tree

3 files changed

+61
-60
lines changed

3 files changed

+61
-60
lines changed

dashi/src/actions/hidePanel.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
import appStore, { ContribPoint } from "../store/appStore";
2-
import { updateContributionState } from "./updateContributionState";
1+
import { setComponentVisibility } from "./setComponentVisibility";
32

43
export function hidePanel(panelIndex: number) {
5-
const contribPoint: ContribPoint = "panels";
6-
const panelStates =
7-
appStore.getState().contributionStatesRecord[contribPoint];
8-
if (panelStates && panelStates[panelIndex].visible) {
9-
updateContributionState(contribPoint, panelIndex, { visible: false });
10-
}
4+
setComponentVisibility("panels", panelIndex, false);
115
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import appStore, { ContribPoint } from "../store/appStore";
2+
import fetchApiResult from "../utils/fetchApiResult";
3+
import { fetchComponentModel } from "../api";
4+
import { updateContributionState } from "./updateContributionState";
5+
6+
export function setComponentVisibility(
7+
contribPoint: ContribPoint,
8+
panelIndex: number,
9+
visible: boolean,
10+
) {
11+
const getState = appStore.getState;
12+
const contributionStates = getState().contributionStatesRecord[contribPoint];
13+
const contributionState = contributionStates[panelIndex];
14+
if (contributionState.visible === visible) {
15+
return; // nothing to do
16+
}
17+
if (contributionState.componentModelResult.status) {
18+
updateContributionState(contribPoint, panelIndex, { visible });
19+
} else {
20+
// No status yet, so we must load the component
21+
updateContributionState(contribPoint, panelIndex, {
22+
visible,
23+
componentModelResult: { status: "pending" },
24+
});
25+
const inputValues = getLayoutInputValues(contribPoint, panelIndex);
26+
fetchApiResult(
27+
fetchComponentModel,
28+
contribPoint,
29+
panelIndex,
30+
inputValues,
31+
).then((componentModelResult) => {
32+
updateContributionState(contribPoint, panelIndex, {
33+
componentModelResult,
34+
});
35+
});
36+
}
37+
}
38+
39+
function getLayoutInputValues(
40+
contribPoint: ContribPoint,
41+
contribIndex: number,
42+
): unknown[] {
43+
const contributionModels =
44+
appStore.getState().contributionsRecordResult.data![contribPoint];
45+
const contributionModel = contributionModels[contribIndex];
46+
const inputs = contributionModel.layout!.inputs;
47+
if (inputs && inputs.length > 0) {
48+
// TODO: get inputValue from sources specified by input.kind.
49+
// For time being we use null as it is JSON-serializable
50+
return inputs.map((input) => {
51+
console.warn(`layout input not supported yet`, input);
52+
return null;
53+
});
54+
} else {
55+
return [];
56+
}
57+
}

dashi/src/actions/showPanel.ts

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,5 @@
1-
import appStore, { ContribPoint } from "../store/appStore";
2-
import fetchApiResult from "../utils/fetchApiResult";
3-
import { fetchComponentModel } from "../api";
4-
import { updateContributionState } from "./updateContributionState";
1+
import { setComponentVisibility } from "./setComponentVisibility";
52

63
export function showPanel(panelIndex: number) {
7-
const contribPoint = "panels";
8-
const getState = appStore.getState;
9-
const panelStates = getState().contributionStatesRecord[contribPoint];
10-
const panelState = panelStates[panelIndex];
11-
if (panelState.visible) {
12-
return; // nothing to do
13-
}
14-
if (panelState.componentModelResult.status) {
15-
// If we have some status, the component is loaded or is being loaded
16-
updateContributionState(contribPoint, panelIndex, { visible: true });
17-
} else {
18-
// No status yet, so we must load the component
19-
updateContributionState(contribPoint, panelIndex, {
20-
visible: true,
21-
componentModelResult: { status: "pending" },
22-
});
23-
const inputValues = getLayoutInputValues(contribPoint, panelIndex);
24-
fetchApiResult(
25-
fetchComponentModel,
26-
contribPoint,
27-
panelIndex,
28-
inputValues,
29-
).then((componentModelResult) => {
30-
updateContributionState(contribPoint, panelIndex, {
31-
componentModelResult,
32-
});
33-
});
34-
}
35-
}
36-
37-
function getLayoutInputValues(
38-
contribPoint: ContribPoint,
39-
contribIndex: number,
40-
): unknown[] {
41-
const contributionModels =
42-
appStore.getState().contributionsRecordResult.data![contribPoint];
43-
const contributionModel = contributionModels[contribIndex];
44-
const inputs = contributionModel.layout!.inputs;
45-
if (inputs && inputs.length > 0) {
46-
// TODO: get inputValue from sources specified by input.kind.
47-
// For time being we use null as it is JSON-serializable
48-
return inputs.map((input) => {
49-
console.warn(`layout input not supported yet`, input);
50-
return null;
51-
});
52-
} else {
53-
return [];
54-
}
4+
setComponentVisibility("panels", panelIndex, true);
555
}

0 commit comments

Comments
 (0)