Skip to content

Commit 3f81744

Browse files
committed
refactor: rename "widget" module to "plugin" and improve type safety
- Updated folder structure and imports to reflect the new module name. - Enhanced typing in validation functions and related utilities. - Standardized and cleaned up code formatting for consistency.
1 parent 9add210 commit 3f81744

File tree

23 files changed

+548
-415
lines changed

23 files changed

+548
-415
lines changed

src/Plugin.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@ import "./Plugin.css";
44
import { FullPageLoader, useInitApp } from "./shared";
55
import { DataStoreProvider } from "@dhis2/app-service-datastore";
66
import { RecoilRoot } from "recoil";
7-
import { WidgetRouter } from "./widget/modules/Router";
7+
import { WidgetRouter } from "./plugin/modules/Router";
88
import { CssReset } from "@dhis2/ui";
9-
import { DATASTORE_WIDGET_NAMESPACE } from "./widget/constants/scorecard";
10-
import { PluginProps } from "./widget/types";
11-
9+
import { DATASTORE_WIDGET_NAMESPACE } from "./plugin/constants/scorecard";
10+
import { PluginProps } from "./plugin/types";
1211

1312
const DashboardPlugin: FC<PluginProps> = (props: PluginProps) => {
1413
const { initializeState } = useInitApp();
1514

1615
return (
1716
<div className="plugin-container">
1817
<CssReset />
19-
<DataStoreProvider namespace={DATASTORE_WIDGET_NAMESPACE} loadingComponent={<FullPageLoader />}>
18+
<DataStoreProvider
19+
namespace={DATASTORE_WIDGET_NAMESPACE}
20+
loadingComponent={<FullPageLoader />}
21+
>
2022
<RecoilRoot initializeState={initializeState}>
2123
<Suspense fallback={<FullPageLoader small />}>
2224
<WidgetRouter props={props} />

src/modules/ScorecardList/hooks/data.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,12 @@ export function useScorecardListData() {
8888
const total =
8989
data?.keys?.filter((key) => !defaultKeys.includes(key)).length ?? 0;
9090
return {
91-
page: data?.list?.pager?.page ?? searchParams.get("page") ?? 1,
91+
page:
92+
data?.list?.pager?.page ??
93+
parseInt(searchParams.get("page") ?? "1"),
9294
pageSize:
9395
data?.list?.pager?.pageSize ??
94-
searchParams.get("pageSize") ??
95-
8,
96+
parseInt(searchParams.get("pageSize") ?? "8"),
9697
pageCount: Math.ceil(total / (data?.list?.pager?.pageSize ?? 8)),
9798
total,
9899
};

src/modules/ScorecardList/index.tsx

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
import i18n from "@dhis2/d2-i18n";
2-
import { Button, ButtonStrip, DropdownButton, IconQuestion24, Tooltip } from "@dhis2/ui";
2+
import {
3+
Button,
4+
ButtonStrip,
5+
DropdownButton,
6+
IconQuestion24,
7+
Tooltip,
8+
} from "@dhis2/ui";
39
import { IconAdd24, IconApps24, IconList24 } from "@dhis2/ui-icons";
4-
import { FullPageError, SCORECARD_LIST_HELP_STEPS, STEP_OPTIONS } from "../../shared";
10+
import {
11+
FullPageError,
12+
SCORECARD_LIST_HELP_STEPS,
13+
STEP_OPTIONS,
14+
} from "../../shared";
515
import { Steps } from "intro.js-react";
6-
import React, { useState } from "react";
16+
import { useState } from "react";
717
import HelpMenu from "./components/HelpMenu";
818
import { SearchArea } from "./components/SearchArea";
919
import { useSetting } from "@dhis2/app-service-datastore";
@@ -30,14 +40,14 @@ export default function ScorecardList() {
3040
} catch (e: any) {
3141
show({
3242
message: e.message ?? e.toString(),
33-
type: { critical: true }
43+
type: { critical: true },
3444
});
3545
}
3646
};
3747
const navigate = useNavigate();
3848

3949
const onAddClick = () => {
40-
navigate(`/add/general`);
50+
navigate("/add/general");
4151
};
4252

4353
const onHelpExit = () => {
@@ -77,7 +87,7 @@ export default function ScorecardList() {
7787
viewType:
7888
scorecardViewType === "grid"
7989
? i18n.t("list")
80-
: i18n.t("grid")
90+
: i18n.t("grid"),
8191
})}
8292
>
8393
<Button

src/widget/components/PluginConfigProvider.tsx renamed to src/plugin/components/PluginConfigProvider.tsx

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,28 @@ import { FullPageError, FullPageLoader } from "../../shared";
66

77
const PluginConfigContext = createContext<PluginConfig | null>(null);
88

9-
109
export function usePluginConfig() {
1110
const config = useContext(PluginConfigContext);
1211

1312
if (config == null) {
14-
throw Error("PluginConfig is not initialized. Make sure to wrap your widget in a PluginConfigProvider");
13+
throw Error(
14+
"PluginConfig is not initialized. Make sure to wrap your widget in a PluginConfigProvider"
15+
);
1516
}
1617

1718
return config;
1819
}
1920

20-
21-
export function PluginConfigProvider({ children, props }: { children: any, props: PluginProps }) {
22-
const { config, error, loading, refetch } = useGetPluginConfig(props.dashboardItemId);
21+
export function PluginConfigProvider({
22+
children,
23+
props,
24+
}: {
25+
children: any;
26+
props: PluginProps;
27+
}) {
28+
const { config, error, loading, refetch } = useGetPluginConfig(
29+
props.dashboardItemId
30+
);
2331
const navigate = useNavigate();
2432
const { id } = useParams<{ id: string }>();
2533

@@ -32,25 +40,21 @@ export function PluginConfigProvider({ children, props }: { children: any, props
3240
}, [config]);
3341

3442
if (loading) {
35-
return (
36-
<FullPageLoader small />
37-
);
43+
return <FullPageLoader small />;
3844
}
3945

4046
if (error && error.details.httpStatusCode !== 404) {
41-
return (
42-
<FullPageError resetErrorBoundary={refetch} error={error} />
43-
);
47+
return <FullPageError resetErrorBoundary={refetch} error={error} />;
4448
}
4549

4650
return (
4751
<PluginConfigContext.Provider
4852
value={{
4953
props,
50-
config
51-
}}>
54+
config,
55+
}}
56+
>
5257
{children}
5358
</PluginConfigContext.Provider>
5459
);
5560
}
56-
File renamed without changes.

src/widget/hooks/config.ts renamed to src/plugin/hooks/config.ts

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,71 +10,75 @@ export interface ScorecardPluginConfig {
1010
const query = {
1111
config: {
1212
resource: `dataStore/${DATASTORE_WIDGET_NAMESPACE}`,
13-
id: ({ id }: { id: string }) => id
14-
}
13+
id: ({ id }: { id: string }) => id,
14+
},
1515
};
1616

1717
type Response = {
1818
config: ScorecardPluginConfig;
19-
}
20-
19+
};
2120

2221
export function useGetPluginConfig(dashboardItemId: string) {
2322
//@ts-expect-error app-runtime query type errors
2423
const { data, ...rest } = useDataQuery<Response>(query, {
2524
variables: {
26-
id: dashboardItemId
27-
}
25+
id: dashboardItemId,
26+
},
2827
});
2928

3029
return {
3130
config: data?.config as ScorecardPluginConfig,
32-
...rest
31+
...rest,
3332
};
3433
}
3534

3635
function getCreateMutation(dashboardItemId: string) {
3736
return {
3837
type: "create",
3938
resource: `dataStore/${DATASTORE_WIDGET_NAMESPACE}/${dashboardItemId}`,
40-
data: ({ data }: { data: ScorecardPluginConfig }) => data
39+
data: ({ data }: { data: ScorecardPluginConfig }) => data,
4140
};
4241
}
4342

4443
const deleteMutation = {
4544
type: "delete",
4645
resource: `dataStore/${DATASTORE_WIDGET_NAMESPACE}`,
47-
id: ({ id }: { id: string }) => id
46+
id: ({ id }: { id: string }) => id,
4847
};
4948

5049
export function useManagePluginConfig(dashboardItemId: string) {
5150
const engine = useDataEngine();
52-
const addConfig = useCallback(async (config: ScorecardPluginConfig) => {
53-
try {
54-
const mutation = getCreateMutation(dashboardItemId);
55-
//@ts-expect-error app runtime mutation errors
56-
return await engine.mutate(mutation, {
57-
variables: {
58-
data: config
59-
}
60-
});
61-
} catch (error) {
62-
console.error(`Could not save configuration for dashboardItemId ${dashboardItemId}`);
63-
throw error;
64-
}
65-
}, [engine]);
51+
const addConfig = useCallback(
52+
async (config: ScorecardPluginConfig) => {
53+
try {
54+
const mutation = getCreateMutation(dashboardItemId);
55+
//@ts-expect-error app runtime mutation errors
56+
return await engine.mutate(mutation, {
57+
variables: {
58+
data: config,
59+
},
60+
});
61+
} catch (error) {
62+
console.error(
63+
`Could not save configuration for dashboardItemId ${dashboardItemId}`
64+
);
65+
throw error;
66+
}
67+
},
68+
[engine]
69+
);
6670

6771
const deleteConfig = useCallback(async () => {
6872
//@ts-expect-error app runtime mutation errors
6973
await engine.mutate(deleteMutation, {
7074
variables: {
71-
id: dashboardItemId
72-
}
75+
id: dashboardItemId,
76+
},
7377
});
7478
}, [engine]);
7579

7680
return {
7781
addConfig,
78-
deleteConfig
82+
deleteConfig,
7983
};
8084
}

src/widget/hooks/data.ts renamed to src/plugin/hooks/data.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,26 @@ import { SCORECARD_NAMESPACE } from "../constants/scorecard";
22
import { useDataQuery } from "@dhis2/app-runtime";
33
import { ScorecardConfig } from "@hisptz/dhis2-scorecard";
44

5-
65
const query: any = {
76
config: {
87
resource: `dataStore/${SCORECARD_NAMESPACE}`,
9-
id: ({ id }: { id: string }) => id
10-
}
8+
id: ({ id }: { id: string }) => id,
9+
},
1110
};
1211

1312
export function usePluginScorecard(scorecardId: string) {
14-
const { loading, data, error, ...rest } = useDataQuery<{ config: ScorecardConfig }>(query, {
13+
const { loading, data, error, ...rest } = useDataQuery<{
14+
config: ScorecardConfig;
15+
}>(query, {
1516
variables: {
16-
id: scorecardId
17-
}
17+
id: scorecardId,
18+
},
1819
});
1920

2021
return {
2122
loading,
2223
error,
2324
scorecard: data?.config,
24-
...rest
25+
...rest,
2526
};
2627
}
27-
28-

src/widget/hooks/scorecard.ts renamed to src/plugin/hooks/scorecard.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { DATASTORE_NAMESPACE } from "../../shared";
66
const query: any = {
77
config: {
88
resource: `dataStore/${DATASTORE_NAMESPACE}`,
9-
id: ({ id }: { id: string }) => id
10-
}
9+
id: ({ id }: { id: string }) => id,
10+
},
1111
};
1212

1313
type ConfigQueryResponse = {
@@ -22,6 +22,6 @@ export function usePluginScorecardConfig() {
2222
config: scorecard,
2323
loading,
2424
error,
25-
...rest
25+
...rest,
2626
};
2727
}

src/widget/modules/Router.tsx renamed to src/plugin/modules/Router.tsx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import { ScorecardView } from "./ScorecardView/ScorecardView";
44
import { PluginConfigProvider } from "../components/PluginConfigProvider";
55
import { PluginProps } from "../types";
66

7-
87
export function WidgetRouter({ props }: { props: PluginProps }) {
9-
108
return (
119
<HashRouter>
1210
<PluginConfigProvider props={props}>
@@ -17,7 +15,4 @@ export function WidgetRouter({ props }: { props: PluginProps }) {
1715
</PluginConfigProvider>
1816
</HashRouter>
1917
);
20-
2118
}
22-
23-
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import { PluginScorecardList } from "./components/PluginScorecardList";
22

33
export function ScorecardList() {
4-
54
return (
6-
<div style={{
7-
width: "100%",
8-
height: "100%",
9-
display: "flex",
10-
flexDirection: "column",
11-
gap: 16
12-
}}>
5+
<div
6+
style={{
7+
width: "100%",
8+
height: "100%",
9+
display: "flex",
10+
flexDirection: "column",
11+
gap: 16,
12+
}}
13+
>
1314
<PluginScorecardList />
1415
</div>
1516
);
16-
1717
}

0 commit comments

Comments
 (0)