Skip to content

Commit 32ccc19

Browse files
samsharatnagorra
authored andcommitted
fix: replace inline functions, resolve PR comments
1 parent e6b490e commit 32ccc19

File tree

7 files changed

+78
-45
lines changed

7 files changed

+78
-45
lines changed

app/src/components/domain/PerAssessmentSummary/i18n.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"namespace": "common",
2+
"namespace": "perAssessmentSummary",
33
"strings": {
4-
"multiImageArea": "Area {areaId}",
4+
"perArea": "Area {areaId}",
55
"benchmarksAssessed": "{totalQuestionCount} benchmarks assessed",
66
"perAssessmentSummaryHeading": "Summary",
77
"benchmarksAssessedTitle": "benchmarks assessed : {allAnsweredResponses} / {totalQuestionCount}"

app/src/components/domain/PerAssessmentSummary/index.tsx

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ import {
2222
} from '@togglecorp/fujs';
2323
import { PartialForm } from '@togglecorp/toggle-form';
2424

25+
import {
26+
PER_FALLBACK_COLOR,
27+
perAreaColorMap,
28+
perRatingColorMap,
29+
} from '#utils/domain/per';
2530
import { type GoApiResponse } from '#utils/restRequest';
2631

2732
import i18n from './i18n.json';
@@ -43,27 +48,29 @@ interface Props {
4348
areaIdToTitleMap: Record<number, string | undefined>;
4449
}
4550

46-
const perRatingColors: {
47-
[key: string]: string;
48-
} = {
49-
5: 'var(--go-ui-color-dark-blue-40)',
50-
4: 'var(--go-ui-color-dark-blue-30)',
51-
3: 'var(--go-ui-color-dark-blue-20)',
52-
2: 'var(--go-ui-color-dark-blue-10)',
53-
1: 'var(--go-ui-color-gray-40)',
54-
0: 'var(--go-ui-color-gray-30)',
55-
};
56-
57-
const areaColorShades: { [key: string]: string } = {
58-
1: 'var(--go-ui-color-purple-per)',
59-
2: 'var(--go-ui-color-orange-per)',
60-
3: 'var(--go-ui-color-blue-per)',
61-
4: 'var(--go-ui-color-teal-per)',
62-
5: 'var(--go-ui-color-red-per)',
63-
};
64-
6551
const progressBarColor = 'var(--go-ui-color-dark-blue-40)';
6652

53+
function numberOfComponentsSelector({ components } : { components: unknown[]}) {
54+
return components.length;
55+
}
56+
57+
function perRatingColorSelector({ ratingValue }: { ratingValue: number | undefined }) {
58+
if (isDefined(ratingValue)) {
59+
return perRatingColorMap[ratingValue];
60+
}
61+
return PER_FALLBACK_COLOR;
62+
}
63+
64+
function perRatingLabelSelector({
65+
ratingValue,
66+
ratingDisplay,
67+
}: {
68+
ratingValue?: number;
69+
ratingDisplay?: string;
70+
}): string {
71+
return `${ratingValue}-${ratingDisplay}`;
72+
}
73+
6774
function PerAssessmentSummary(props: Props) {
6875
const {
6976
className,
@@ -156,7 +163,7 @@ function PerAssessmentSummary(props: Props) {
156163
// components?
157164
const filteredComponents = areaResponse?.component_responses?.filter(
158165
(component) => isDefined(component?.rating_details?.value)
159-
&& component.rating_details?.value !== 0,
166+
&& component.rating_details.value !== 0,
160167
) ?? [];
161168

162169
if (filteredComponents.length === 0) {
@@ -185,7 +192,7 @@ function PerAssessmentSummary(props: Props) {
185192
areaId,
186193
rating: averageRatingByAreaMap[Number(areaId)] ?? 0,
187194
areaDisplay: resolveToString(
188-
strings.multiImageArea,
195+
strings.perArea,
189196
{ areaId },
190197
),
191198
}),
@@ -241,20 +248,9 @@ function PerAssessmentSummary(props: Props) {
241248
<StackedProgressBar
242249
className={styles.componentRating}
243250
data={statusGroupedComponentList ?? []}
244-
// FIXME: don't use inline selectors
245-
valueSelector={
246-
(statusGroupedComponent) => (
247-
statusGroupedComponent.components.length
248-
)
249-
}
250-
// FIXME: don't use inline selectors
251-
colorSelector={(statusGroupedComponent) => (
252-
perRatingColors[statusGroupedComponent.ratingValue ?? 0]
253-
)}
254-
// FIXME: don't use inline selectors
255-
labelSelector={
256-
(statusGroupedComponent) => `${statusGroupedComponent.ratingValue}-${statusGroupedComponent.ratingDisplay}`
257-
}
251+
valueSelector={numberOfComponentsSelector}
252+
colorSelector={perRatingColorSelector}
253+
labelSelector={perRatingLabelSelector}
258254
/>
259255
<div className={styles.avgComponentRating}>
260256
{averageRatingByAreaList.map(
@@ -272,7 +268,7 @@ function PerAssessmentSummary(props: Props) {
272268
className={styles.filledBar}
273269
style={{
274270
height: `${getPercentage(rating.rating, averageRatingByAreaList.length)}%`,
275-
backgroundColor: areaColorShades[rating.areaId],
271+
backgroundColor: perAreaColorMap[Number(rating.areaId)],
276272
}}
277273
/>
278274
</div>

app/src/utils/domain/per.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,24 @@ export const perBenchmarkColorMap: {
8181
5: 'var(--go-ui-color-dark-blue-10)',
8282
};
8383

84+
export const PER_FALLBACK_COLOR = 'var(--go-ui-color-gray-40)';
85+
8486
export function perRatingColorSelector(item: {
85-
value: number;
87+
value: number | undefined;
8688
}) {
87-
return perRatingColorMap[item.value];
89+
if (isDefined(item.value)) {
90+
return perRatingColorMap[item.value];
91+
}
92+
return PER_FALLBACK_COLOR;
93+
}
94+
95+
export function perAreaColorSelector(item: {
96+
value: number | undefined;
97+
}) {
98+
if (isDefined(item.value)) {
99+
return perRatingColorMap[item.value];
100+
}
101+
return PER_FALLBACK_COLOR;
88102
}
89103

90104
export function perBenchmarkColorSelector(item: {
@@ -94,5 +108,3 @@ export function perBenchmarkColorSelector(item: {
94108
}) {
95109
return perBenchmarkColorMap[item.id];
96110
}
97-
98-
export const PER_FALLBACK_COLOR = 'var(--go-ui-color-gray-40)';
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"namespace": "ratingByAreaChart",
3+
"strings": {
4+
"perArea": "Area {areaNumber}: {areaTitle}"
5+
}
6+
}

app/src/views/CountryPreparedness/RatingByAreaChart/index.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@ import {
22
ChartAxes,
33
ChartContainer,
44
} from '@ifrc-go/ui';
5-
import { DEFAULT_INVALID_TEXT } from '@ifrc-go/ui/utils';
5+
import { useTranslation } from '@ifrc-go/ui/hooks';
6+
import {
7+
DEFAULT_INVALID_TEXT,
8+
resolveToString,
9+
} from '@ifrc-go/ui/utils';
610
import { listToMap } from '@togglecorp/fujs';
711

812
import useNumericChartData from '#hooks/useNumericChartData';
913
import { defaultChartMargin } from '#utils/constants';
1014
import { type GoApiResponse } from '#utils/restRequest';
1115

16+
import i18n from './i18n.json';
1217
import styles from './styles.module.css';
1318

1419
type PerOptionsResponse = GoApiResponse<'/api/v2/per-options/'>;
@@ -34,6 +39,8 @@ function RatingByAreaChart(props: Props) {
3439
formAreaOptions,
3540
} = props;
3641

42+
const strings = useTranslation(i18n);
43+
3744
const ratingTitleMap = listToMap(
3845
ratingOptions,
3946
(option) => option.value,
@@ -43,7 +50,13 @@ function RatingByAreaChart(props: Props) {
4350
const formAreaMap = listToMap(
4451
formAreaOptions,
4552
(option) => option.area_num ?? DEFAULT_INVALID_TEXT,
46-
(option) => `Area ${option.area_num}: ${option.title}`,
53+
(option) => resolveToString(
54+
strings.perArea,
55+
{
56+
areaNumber: option.area_num,
57+
areaTitle: option.title,
58+
},
59+
),
4760
);
4861

4962
const chartData = useNumericChartData(

app/src/views/PerExport/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ export function Component() {
547547
return null;
548548
}
549549

550-
const color = perAreaColorMap?.[perFormArea?.area_num];
550+
const color = perAreaColorMap?.[perFormArea.area_num];
551551
return (
552552
<LegendItem
553553
key={perFormArea.id}

app/src/views/PerExport/styles.module.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@
8888
.legend {
8989
display: flex;
9090
gap: var(--go-ui-spacing-sm);
91+
92+
.separator {
93+
align-self: stretch;
94+
background-color: var(--go-ui-color-separator);
95+
width: var(--go-ui-width-separator-thin);
96+
}
9197
}
9298
}
9399
}

0 commit comments

Comments
 (0)