Skip to content

Commit abc6323

Browse files
committed
refactor: improve formatting, hooks, and typing in ScorecardList components
- Standardized code formatting across multiple components for better readability. - Refactored `useScorecardListData` to enhance query handling and integrate better with URL parameters. - Streamlined imports by removing redundant `React` imports. - Improved typing and applied cleaner parameter handling in components like `ListScorecardDisplay` and `ScorecardListCard`.
1 parent 4a0465d commit abc6323

File tree

6 files changed

+170
-106
lines changed

6 files changed

+170
-106
lines changed

src/modules/ScorecardList/components/Cards/ScorecardListCard.tsx

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,49 @@
11
import { colors } from "@dhis2/ui";
2-
import { getUserAuthority, ScorecardCardImage as holderImage, truncateDescription, UserState } from "../../../../shared";
3-
import React, { useState } from "react";
2+
import {
3+
getUserAuthority,
4+
ScorecardCardImage as holderImage,
5+
truncateDescription,
6+
UserState,
7+
} from "../../../../shared";
8+
import { useState } from "react";
49
import { ScorecardListItem } from "../../types";
510
import { ScorecardListCardActions } from "./ScorecardListCardActions";
611
import { getSharingSettingsFromOldConfiguration } from "../../../../utils/sharing";
712
import { useRecoilValue } from "recoil";
813
import { useNavigateToScorecardView } from "../../../../hooks/navigate";
914

1015
export default function ScorecardListCard({
11-
scorecard,
12-
grid
13-
}: {
16+
scorecard,
17+
grid,
18+
}: {
1419
scorecard: ScorecardListItem;
1520
grid?: boolean;
1621
}) {
1722
const user = useRecoilValue(UserState);
1823
const { title, description } = scorecard ?? {};
1924
const [showFullDescription, setShowFullDescription] = useState(false);
2025

21-
const accessConfig = getUserAuthority(user, scorecard.sharing ?? getSharingSettingsFromOldConfiguration(scorecard as any));
26+
const accessConfig = getUserAuthority(
27+
user,
28+
scorecard.sharing ??
29+
getSharingSettingsFromOldConfiguration(scorecard as any)
30+
);
2231
const { read } = accessConfig;
2332

2433
const navigateToView = useNavigateToScorecardView();
2534

26-
2735
const onView = () => {
2836
if (read) {
2937
navigateToView(scorecard);
3038
}
3139
};
3240

33-
const styles = { margin: 16, background: "white", opacity: read ? 1 : 0.4, cursor: read ? "pointer" : "not-allowed" };
41+
const styles = {
42+
margin: 16,
43+
background: "white",
44+
opacity: read ? 1 : 0.4,
45+
cursor: read ? "pointer" : "not-allowed",
46+
};
3447

3548
return grid ? (
3649
<div

src/modules/ScorecardList/components/ListScorecardDisplay.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import React from "react";
21
import ScorecardListCard from "./Cards/ScorecardListCard";
32
import { ScorecardListItem } from "../types";
43

5-
6-
export default function ListScorecardDisplay({ scorecards }: { scorecards: ScorecardListItem[] }) {
4+
export default function ListScorecardDisplay({
5+
scorecards,
6+
}: {
7+
scorecards: ScorecardListItem[];
8+
}) {
79
return (
810
<div className="column">
911
{scorecards?.map((scorecard) => (

src/modules/ScorecardList/components/PaginatedDisplay.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ import GridScorecardDisplay from "./GridScorecardDisplay";
55
import ListScorecardDisplay from "./ListScorecardDisplay";
66

77
export default function PaginatedDisplay({
8-
scorecards = []
9-
}: {
8+
scorecards = [],
9+
}: {
1010
scorecards: ScorecardListItem[];
1111
}) {
12-
13-
1412
const [scorecardViewType] = useSetting("scorecardViewType");
1513

1614
return (
1715
<div className="p-16 scorecard-list">
1816
{scorecardViewType === "list" ? (
19-
<ListScorecardDisplay scorecards={scorecards} />) : <GridScorecardDisplay scorecards={scorecards} />
20-
}
17+
<ListScorecardDisplay scorecards={scorecards} />
18+
) : (
19+
<GridScorecardDisplay scorecards={scorecards} />
20+
)}
2121
</div>
2222
);
2323
}
Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useScorecardListData } from "../hooks/data";
22
import { FullPageError, FullPageLoader } from "../../../shared";
3-
import React, { Suspense } from "react";
3+
import { Suspense } from "react";
44
import { isEmpty } from "lodash";
55
import EmptySearchList from "./EmptySearchList";
66
import EmptyScoreCardList from "./EmptyScoreCardList";
@@ -20,45 +20,55 @@ export function ScorecardListArea() {
2020
);
2121
}
2222

23-
2423
return (
25-
<div style={{ gap: 16, justifyContent: "space-between", height: "100%" }} className="column ">
24+
<div
25+
style={{ gap: 16, justifyContent: "space-between", height: "100%" }}
26+
className="column "
27+
>
2628
<Suspense fallback={<FullPageLoader />}>
2729
<div style={{ flex: 1 }}>
28-
{
29-
loading ? <div style={{ width: "100%", height: "100%", display: "flex", alignItems: "center", justifyContent: "center" }}>
30+
{loading ? (
31+
<div
32+
style={{
33+
width: "100%",
34+
height: "100%",
35+
display: "flex",
36+
alignItems: "center",
37+
justifyContent: "center",
38+
}}
39+
>
3040
<FullPageLoader />
31-
</div> : (isEmpty(scorecards) ? (
32-
keyword ? (
33-
<div className="flex-1">
34-
<EmptySearchList keyword={keyword} />
35-
</div>
36-
) : (
37-
<div className="flex-1 h-100 column center align-center">
38-
<EmptyScoreCardList />
39-
</div>
40-
)
41+
</div>
42+
) : isEmpty(scorecards) ? (
43+
keyword ? (
44+
<div className="flex-1">
45+
<EmptySearchList keyword={keyword} />
46+
</div>
4147
) : (
42-
<div className="column h-100">
43-
{scorecards ? (
44-
<PaginatedDisplay
45-
scorecards={scorecards}
46-
/>
47-
) : <EmptyScoreCardList />}
48+
<div className="flex-1 h-100 column center align-center">
49+
<EmptyScoreCardList />
4850
</div>
49-
))
50-
}
51+
)
52+
) : (
53+
<div className="column h-100">
54+
{scorecards ? (
55+
<PaginatedDisplay scorecards={scorecards} />
56+
) : (
57+
<EmptyScoreCardList />
58+
)}
59+
</div>
60+
)}
5161
</div>
52-
{
53-
!isEmpty(scorecards) && (<div className="p-16">
54-
<Pagination
55-
disabled={pager.pageCount === 1 || isEmpty(scorecards)}
56-
{...pager}
57-
pageSizes={Array.from(new Array(10).keys()).map((i) => ((i + 1) * 4).toString())}
58-
/>
59-
</div>)
60-
}
6162
</Suspense>
63+
<div className="p-16">
64+
<Pagination
65+
disabled={pager.pageCount === 1 || isEmpty(scorecards)}
66+
{...pager}
67+
pageSizes={Array.from(new Array(10).keys()).map((i) =>
68+
((i + 1) * 4).toString()
69+
)}
70+
/>
71+
</div>
6272
</div>
6373
);
6474
}

src/modules/ScorecardList/components/SearchArea.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import i18n from "@dhis2/d2-i18n";
22
import { Button, Input } from "@dhis2/ui";
3-
import React, { useMemo } from "react";
43
import { useSearchParams } from "react-router-dom";
54
import { IconCross24 } from "@dhis2/ui-icons";
65

@@ -13,21 +12,17 @@ export function SearchArea() {
1312
return updatedParams;
1413
});
1514
};
16-
const value = useMemo(
17-
() => searchParams.get("query") ?? undefined,
18-
[searchParams.get("query")],
19-
);
2015

2116
return (
2217
<div style={{ gap: 8, display: "flex" }} className="row">
2318
<div className="flex-1">
2419
<Input
2520
className="search-input"
26-
value={value}
21+
value={searchParams.get("query") ?? ""}
2722
onChange={({ value }: { value?: string }) => {
2823
setSearchParams((prev) => {
2924
const updatedSearchParams = new URLSearchParams(
30-
prev,
25+
prev
3126
);
3227
if (value) {
3328
updatedSearchParams.set("query", value);

0 commit comments

Comments
 (0)