Skip to content

Commit f969374

Browse files
committed
feat: Add unit selection to Locations and Periods pages for improved filtering
1 parent 09f0e3c commit f969374

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

src/pages/Locations/LocationsPage.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const LocationsPage: React.FC = () => {
3030
// Sorting and filtering state
3131
const [sortOrder, setSortOrder] = useState<'asc' | 'desc'>('asc');
3232
const [selectedLocation, setSelectedLocation] = useState<Location | null>(null);
33-
const { units, loading: unitsLoading } = useUnit();
33+
const { units, loading: unitsLoading, selectedUnit, setSelectedUnit } = useUnit();
3434
const { selectedIncident } = useIncident();
3535

3636
const handleAdd = () => {
@@ -77,8 +77,11 @@ const LocationsPage: React.FC = () => {
7777
// Filter and sort locations before rendering
7878
const filteredLocations = (locations || [])
7979
.filter(l => {
80-
if (!selectedLocation) return true;
81-
return l.locationId === selectedLocation.locationId;
80+
// Filter by selected unit if set
81+
if (selectedUnit && l.unitId !== selectedUnit.unitId) return false;
82+
// Then filter by selected location if set
83+
if (selectedLocation && l.locationId !== selectedLocation.locationId) return false;
84+
return true;
8285
})
8386
.sort((a, b) => {
8487
const aName = a.name ? a.name.toLowerCase() : '';
@@ -95,6 +98,15 @@ const LocationsPage: React.FC = () => {
9598
<Row className="align-items-center">
9699
<Col><strong>Locations</strong></Col>
97100
<Col md="auto" className="d-flex align-items-center gap-2">
101+
<ContextSelect
102+
label="Unit"
103+
options={units}
104+
value={selectedUnit ? selectedUnit.unitId : null}
105+
onSelect={id => setSelectedUnit(id ? units.find(u => u.unitId === id) ?? null : null)}
106+
loading={unitsLoading}
107+
getOptionLabel={u => u.name}
108+
getOptionValue={u => u.unitId}
109+
/>
98110
<ContextSelect
99111
label="Location"
100112
options={locations}

src/pages/Periods/PeriodsPage.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const PeriodsPage: React.FC = () => {
3232
// Sorting and filtering state
3333
const [sortOrder, setSortOrder] = useState<'asc' | 'desc'>('asc');
3434
const { incidents, loading: incidentsLoading } = useIncident();
35-
const { units, loading: unitsLoading } = useUnit();
35+
const { units, loading: unitsLoading, selectedUnit, setSelectedUnit } = useUnit();
3636

3737
const handleAdd = () => {
3838
setEditPeriod(null);
@@ -111,8 +111,11 @@ const PeriodsPage: React.FC = () => {
111111
// Filter and sort periods before rendering
112112
const filteredPeriods = periods
113113
.filter(p => {
114-
if (!selectedPeriod) return true;
115-
return p.periodId === selectedPeriod.periodId;
114+
// Filter by selected unit if set
115+
if (selectedUnit && p.unitId !== selectedUnit.unitId) return false;
116+
// Then filter by selected period if set
117+
if (selectedPeriod && p.periodId !== selectedPeriod.periodId) return false;
118+
return true;
116119
})
117120
.sort((a, b) => {
118121
const aTime = new Date(a.startTime).getTime();
@@ -127,6 +130,15 @@ const PeriodsPage: React.FC = () => {
127130
<Row className="align-items-center">
128131
<Col><strong>Time Periods</strong></Col>
129132
<Col md="auto" className="d-flex align-items-center gap-2">
133+
<ContextSelect
134+
label="Unit"
135+
options={units}
136+
value={selectedUnit ? selectedUnit.unitId : null}
137+
onSelect={id => setSelectedUnit(id ? units.find(u => u.unitId === id) ?? null : null)}
138+
loading={unitsLoading}
139+
getOptionLabel={u => u.name}
140+
getOptionValue={u => u.unitId}
141+
/>
130142
<ContextSelect
131143
label="Period"
132144
options={periods}

0 commit comments

Comments
 (0)