Skip to content

Commit 075c278

Browse files
committed
refactor: modernize GroupList component to TypeScript
- Convert GroupList from JavaScript to TypeScript with proper types - Replace connect() with modern useSelector hook for Redux integration - Extract GroupListElement as typed functional sub-component - Add comprehensive unit test suite
1 parent 35553cb commit 075c278

6 files changed

Lines changed: 351 additions & 55 deletions

File tree

src/components/DeleteConfirmation.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { useSelector, useDispatch } from 'react-redux';
55

66
import UnitStatusSummary from './UnitStatusSummary';
77
import { RootState } from '../reducers/types';
8+
import { LocalizedText } from '../types';
89

910
import * as actions from '../actions/index';
1011

@@ -87,7 +88,7 @@ const DeleteConfirmation: React.FC = () => {
8788
<h6>Oletko varma että haluat poistaa liikuntapaikalta seuraavan tekstitiedotteen?</h6>
8889
</div>
8990
<div className="panel-body">
90-
<TextualDescription text={observation.value.fi} />
91+
<TextualDescription text={(observation.value as LocalizedText).fi} />
9192
{buttonRow}
9293
<div className="row">
9394
<div className="col-xs-12">

src/components/GroupList.js

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/components/GroupList.tsx

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import React from 'react';
2+
import { Link } from 'react-router-dom';
3+
import { useSelector } from 'react-redux';
4+
import _ from 'lodash';
5+
import { calculateGroups } from './utils';
6+
import { RootState } from '../reducers/types';
7+
8+
interface GroupListElementProps {
9+
id: string;
10+
name: string;
11+
}
12+
13+
function GroupListElement({ id, name }: GroupListElementProps): React.ReactElement {
14+
const url = `/group/${id}`;
15+
return (
16+
<Link to={url} className="list-group-item">
17+
<span className="action-icon glyphicon glyphicon-chevron-right"></span>
18+
{name}
19+
</Link>
20+
);
21+
}
22+
23+
interface Groups {
24+
[groupId: string]: any;
25+
}
26+
27+
const GroupList: React.FC = () => {
28+
const groups = useSelector((state: RootState): Groups =>
29+
calculateGroups(state.data.unit, state.auth.maintenance_organization)
30+
);
31+
32+
const hasRequiredData = (groups: Groups): boolean => {
33+
return (groups && Object.keys(groups).length > 0);
34+
};
35+
36+
if (!hasRequiredData(groups)) {
37+
return <div>Ladataan...</div>;
38+
}
39+
40+
const elements = _.map(groups, (group, groupId) => {
41+
return <GroupListElement key={groupId} id={groupId} name={groupId} />;
42+
});
43+
44+
return (
45+
<div className="row">
46+
<div className="col-xs-12">
47+
<h5>Alueet</h5>
48+
<div className="list-group facility-drilldown">
49+
{elements}
50+
</div>
51+
</div>
52+
</div>
53+
);
54+
};
55+
56+
export default GroupList;

src/components/UnitHistory.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,9 @@ import ObservationItem from './ObservationItem';
77
import { UnitObservation } from '../types';
88
import { RootState } from '../reducers/types';
99

10-
interface Params {
11-
unitId: string;
12-
}
1310

1411
const UnitHistory: React.FC = () => {
15-
const params = useParams<Params>();
12+
const params = useParams<{ unitId: string }>();
1613
const dispatch = useDispatch();
1714

1815
const unitId = useMemo(() => Number(params.unitId), [params.unitId]);
@@ -28,7 +25,7 @@ const UnitHistory: React.FC = () => {
2825

2926
useEffect(() => {
3027
if (params.unitId) {
31-
dispatch(actions.fetchUnitObservations(params.unitId));
28+
dispatch(actions.fetchUnitObservations(params.unitId) as any);
3229
}
3330
}, [dispatch, params.unitId]);
3431

src/components/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ export function getQualityObservation(unit?: Unit): UnitObservation | undefined
5252
});
5353
}
5454

55-
export function calculateGroups(units: Unit[], maintenanceOrg: string): Record<string, number[]> {
55+
export function calculateGroups(units: Record<string, Unit> | Unit[], maintenanceOrg: string): Record<string, number[]> {
5656
let result: Record<string, number[]> = {};
57-
_.each(units, (u) => {
57+
_.each(units, (u: Unit) => {
5858
if (u.extensions.maintenance_organization == maintenanceOrg) {
5959
const group = result[u.extensions.maintenance_group] || [];
6060
result[u.extensions.maintenance_group] = group.concat(u.id);

0 commit comments

Comments
 (0)