Skip to content

Commit 0742b18

Browse files
committed
trim filterStore to whoStore
1 parent 684a5e4 commit 0742b18

8 files changed

Lines changed: 72 additions & 84 deletions

File tree

src/components/filter/CardFilterWho.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,16 @@ import '../../styles/accordion.css'
1010
const CardFilterWho = observer(() => {
1111
const { t } = useTranslation();
1212
const { filterStore } = useStore();
13-
const { isValidWho, genderTypes, updateGenderType, } = filterStore;
14-
const { ageTypes, updateAgeType, populationTypes, updatePopulationType, setFormCardKey } = filterStore;
13+
const { whoStore, setFormCardKey } = filterStore;
14+
const {
15+
isValidWho,
16+
genderTypes,
17+
updateGenderType,
18+
ageTypes,
19+
updateAgeType,
20+
populationTypes,
21+
updatePopulationType,
22+
} = whoStore;
1523

1624
return (
1725
<Card>
@@ -48,4 +56,4 @@ const CardFilterWho = observer(() => {
4856
);
4957
});
5058

51-
export default CardFilterWho;
59+
export default CardFilterWho;

src/components/molecules/InfoPanel.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ const WhereTitle: React.FC<{}> = observer(() => {
4646

4747
const WhoTitle: React.FC<{}> = observer(() => {
4848
const { t } = useTranslation();
49-
const { filterStore } = useStore();
50-
const { genderTypes, ageTypes, populationTypes } = filterStore;
49+
const { filterStore: { whoStore: { genderTypes, ageTypes, populationTypes } } } = useStore();
5150
let res = (genderTypes.text !== '') ? `, ${genderTypes.text}` : '';
5251
if (populationTypes.text !== '') res += `, ${populationTypes.text}`;
5352
if (ageTypes.text !== '') res += `, ${t('Age')}: ${ageTypes.text}`;

src/components/organisms/ChartDataFilterSlider.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ import { useTranslation } from 'react-i18next';
55
import { useSelector } from 'react-redux';
66
import { RootState } from '../../stores/store';
77
import { ItemCount } from '../../types';
8+
import { EchartId } from '../types';
89

910
interface IProps {
10-
id: string;
11+
id: EchartId;
1112
data: ItemCount[];
1213
}
1314

1415
const ChartDataFilterSlider: FC<IProps> = observer(({ id, data }) => {
1516
const { filterStore } = useStore();
16-
const { chartDataRanges, setChartDataRange, chartHideOutOfRange, setChartHideOutOfRange, chartOutOfRangeCounts } = filterStore;
17+
const { chartDataRanges, setChartDataRange, chartHideOutOfRange, setChartHideOutOfRange, getChartOutOfRangeCount } = filterStore;
1718
const { t } = useTranslation();
1819
const direction = useSelector((state: RootState) => state.appUi.direction);
1920
const isRtl = direction === 'rtl';
@@ -60,7 +61,7 @@ const ChartDataFilterSlider: FC<IProps> = observer(({ id, data }) => {
6061

6162
if (!data || data.length <= 1) return null;
6263

63-
const outOfRangeCount = chartOutOfRangeCounts.get(id) ?? 0;
64+
const outOfRangeCount = getChartOutOfRangeCount(id);
6465
const hideOutOfRange = chartHideOutOfRange.get(id) ?? false;
6566

6667
const leftPosStart = isRtl ? `${100 - (localRange.start / maxVal) * 100}%` : `${(localRange.start / maxVal) * 100}%`;

src/lng/en/translation.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
"Road": "Road",
8585
"several-roads": "Several Roads",
8686
"RoadSegment": "Road Segment",
87+
"Select road segment": "Select road segment",
8788
"Who": "Who",
8889
"Severity": "Severity",
8990
"dead": "Dead",

src/lng/he/translation.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
"Road": "כביש",
8585
"several-roads": "מספר כבישים",
8686
"RoadSegment": "קטע כביש",
87+
"Select road segment": "בחר מקטע כביש",
8788
"Who": "מי",
8889
"Severity": "חומרה",
8990
"dead": "הרוג",

src/stores/filter/FilterStore.ts

Lines changed: 42 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ class FilterStore implements IFilterStore {
7878
dataByYears: observable,
7979
chartDataRanges: observable,
8080
chartHideOutOfRange: observable,
81-
chartOutOfRangeCounts: observable,
8281
dataSource: observable
8382
});
8483
// where
@@ -251,37 +250,6 @@ class FilterStore implements IFilterStore {
251250
this.dataSource = sourceVal;
252251
}
253252

254-
// who (delegated to WhoFilterStore)
255-
// ///////////////////////////////////////////////////////////////////////////////////////////////
256-
257-
get genderTypes() {
258-
return this.whoStore.genderTypes;
259-
}
260-
261-
updateGenderType = (aType: number, val: boolean) => {
262-
this.whoStore.updateGenderType(aType, val);
263-
}
264-
265-
get ageTypes() {
266-
return this.whoStore.ageTypes;
267-
}
268-
269-
updateAgeType = (aType: number, val: boolean) => {
270-
this.whoStore.updateAgeType(aType, val);
271-
}
272-
273-
get populationTypes() {
274-
return this.whoStore.populationTypes;
275-
}
276-
277-
updatePopulationType = (aType: number, val: boolean) => {
278-
this.whoStore.updatePopulationType(aType, val);
279-
}
280-
281-
@computed get isValidWho() {
282-
return this.whoStore.isValidWho;
283-
}
284-
285253
// ///////////////////////////////////////////////////////////////////////////////////////////////
286254
// What (delegated to WhatFilterStore)
287255
// ///////////////////////////////////////////////////////////////////////////////////////////////
@@ -405,7 +373,7 @@ class FilterStore implements IFilterStore {
405373
}
406374

407375
@computed get isValidAllFilters() {
408-
const res = this.severityStore.isValidSeverity && this.timeStore.isValidWhen && this.isValidWho
376+
const res = this.severityStore.isValidSeverity && this.timeStore.isValidWhen && this.whoStore.isValidWho
409377
&& this.isValidWhere && this.isValidWhat && this.vehicleStore.isValidWhatVehicle && this.isValidWhatRoad;
410378
return res;
411379
}
@@ -698,9 +666,9 @@ class FilterStore implements IFilterStore {
698666
query += this.roads.getFilter();
699667
query += this.roadSegment.getFilter();
700668
query += this.vehicleStore.injTypes.getFilter();
701-
query += this.genderTypes.getFilter();
702-
query += this.ageTypes.getFilter();
703-
query += this.populationTypes.getFilter();
669+
query += this.whoStore.genderTypes.getFilter();
670+
query += this.whoStore.ageTypes.getFilter();
671+
query += this.whoStore.populationTypes.getFilter();
704672
query += this.accidentType.getFilter();
705673
query += this.vehicleStore.vehicleType.getFilter();
706674
query += this.vehicleStore.involvedVehicle.getFilter();
@@ -724,9 +692,7 @@ class FilterStore implements IFilterStore {
724692
this.timeStore.endYear.setText();
725693
this.vehicleStore.injTypes.setText(ignoreIfAll);
726694
this.timeStore.dayNight.setText(ignoreIfAll);
727-
this.genderTypes.setText(ignoreIfAll);
728-
this.ageTypes.setText(ignoreIfAll);
729-
this.populationTypes.setText(ignoreIfAll);
695+
this.whoStore.setText(ignoreIfAll);
730696
this.locationAccuracy.setText(ignoreIfAll);
731697
this.roadTypes.setText(ignoreIfAll);
732698
const cityNamesArr = getCitiesNames(this.cities.arrValues);
@@ -752,9 +718,7 @@ class FilterStore implements IFilterStore {
752718
this.severityStore.injurySeverity.setBrowserQueryString(params, false);
753719
this.roadTypes.setBrowserQueryString(params);
754720
this.vehicleStore.injTypes.setBrowserQueryString(params);
755-
this.genderTypes.setBrowserQueryString(params);
756-
this.ageTypes.setBrowserQueryString(params);
757-
this.populationTypes.setBrowserQueryString(params);
721+
this.whoStore.setBrowserQueryString(params);
758722
this.cities.setBrowserQueryString(params);
759723
this.streets.setBrowserQueryString(params);
760724
this.roads.setBrowserQueryString(params);
@@ -793,9 +757,7 @@ class FilterStore implements IFilterStore {
793757
this.locationAccuracy.setValuesByQuery(params);
794758
this.roadTypes.setValuesByQuery(params);
795759
this.vehicleStore.injTypes.setValuesByQuery(params);
796-
this.genderTypes.setValuesByQuery(params);
797-
this.ageTypes.setValuesByQuery(params);
798-
this.populationTypes.setValuesByQuery(params);
760+
this.whoStore.setValuesByQuery(params);
799761
this.accidentType.setValuesByQuery(params);
800762
this.vehicleStore.vehicleType.setValuesByQuery(params);
801763
this.vehicleStore.involvedVehicle.setValuesByQuery(params);
@@ -853,9 +815,6 @@ class FilterStore implements IFilterStore {
853815
@observable
854816
chartHideOutOfRange: Map<string, boolean> = new Map();
855817

856-
@observable
857-
chartOutOfRangeCounts: Map<string, number> = new Map();
858-
859818
@action
860819
setChartDataRange = (id: string, start: number, end: number) => {
861820
this.chartDataRanges.set(id, { start, end });
@@ -874,7 +833,41 @@ class FilterStore implements IFilterStore {
874833
resetChartRanges = () => {
875834
this.chartDataRanges.clear();
876835
this.chartHideOutOfRange.clear();
877-
this.chartOutOfRangeCounts.clear();
836+
}
837+
838+
getChartOutOfRangeCount = (id: EchartId) => {
839+
let data: any[] = [];
840+
let metaData: any[] | undefined;
841+
842+
switch (id) {
843+
case EchartId.Group_1:
844+
data = this.dataFilterd;
845+
break;
846+
case EchartId.Group_2:
847+
data = this.dataGroupby2;
848+
metaData = (this.group2Dict.groupBy as GroupBy2).getBars();
849+
break;
850+
case EchartId.Years:
851+
data = this.dataFilterdByYears;
852+
break;
853+
default:
854+
return 0;
855+
}
856+
857+
const getItemValue = (item: any) => {
858+
if (item.count !== undefined) return Number(item.count);
859+
if (metaData) return Math.max(...metaData.map(m => Number(item[m.key]) || 0));
860+
return 0;
861+
};
862+
const maxVal = data.reduce((max, item) => Math.max(max, getItemValue(item)), 0);
863+
const range = this.chartDataRanges.get(id) || { start: 0, end: maxVal };
864+
const outsideItem = sliceDataWithAggregation(data, range, metaData)
865+
.find((item: any) => item._id === 'outside_range');
866+
867+
if (!outsideItem) return 0;
868+
return metaData
869+
? metaData.reduce((sum, m) => sum + (Number(outsideItem[m.key]) || 0), 0)
870+
: Number(outsideItem.count) || 0;
878871
}
879872

880873
getChartData = (id: EchartId) => {
@@ -909,15 +902,6 @@ class FilterStore implements IFilterStore {
909902
const maxVal = data.reduce((max, item) => Math.max(max, getItemValue(item)), 0);
910903
const range = this.chartDataRanges.get(id) || { start: 0, end: maxVal };
911904
let sliced = sliceDataWithAggregation(data, range, metaData);
912-
const outsideItem = sliced.find((item: any) => item._id === 'outside_range');
913-
const outOfRangeCount = outsideItem
914-
? (metaData
915-
? metaData.reduce((sum, m) => sum + (Number(outsideItem[m.key]) || 0), 0)
916-
: Number(outsideItem.count) || 0)
917-
: 0;
918-
runInAction(() => {
919-
this.chartOutOfRangeCounts.set(id, outOfRangeCount);
920-
});
921905
if (this.chartHideOutOfRange.get(id)) {
922906
sliced = sliced.filter((item: any) => item._id !== 'outside_range');
923907
}

src/stores/filter/LocalDBFilterStore.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ class LocalDBFilterStore {
5656
this.getFilterFromArrayIDb(arrFilters, "road_segment_name", this.rootStore.filterStore.roadSegment.arrValues);
5757
this.getMultiplefilterIDB(arrFilters, this.rootStore.filterStore.roadTypes);
5858
this.getMultiplefilterIDB(arrFilters, this.rootStore.filterStore.injTypes);
59-
this.getMultiplefilterIDB(arrFilters, this.rootStore.filterStore.genderTypes);
60-
this.getMultiplefilterIDB(arrFilters, this.rootStore.filterStore.ageTypes);
61-
this.getMultiplefilterIDB(arrFilters, this.rootStore.filterStore.populationTypes);
59+
this.getMultiplefilterIDB(arrFilters, this.rootStore.filterStore.whoStore.genderTypes);
60+
this.getMultiplefilterIDB(arrFilters, this.rootStore.filterStore.whoStore.ageTypes);
61+
this.getMultiplefilterIDB(arrFilters, this.rootStore.filterStore.whoStore.populationTypes);
6262
this.getMultiplefilterIDB(arrFilters, this.rootStore.filterStore.accidentType);
6363
this.getMultiplefilterIDB(arrFilters, this.rootStore.filterStore.vehicleType);
6464
this.getMultiplefilterIDB(arrFilters, this.rootStore.filterStore.speedLimit);
@@ -87,9 +87,9 @@ class LocalDBFilterStore {
8787
this.getFilterFromArrayIDb(arrFilters, "road_segment_name", this.rootStore.filterStore.roadSegment.arrValues);
8888
this.getMultiplefilterIDB(arrFilters, this.rootStore.filterStore.roadTypes);
8989
this.getMultiplefilterIDB(arrFilters, this.rootStore.filterStore.injTypes);
90-
this.getMultiplefilterIDB(arrFilters, this.rootStore.filterStore.genderTypes);
91-
this.getMultiplefilterIDB(arrFilters, this.rootStore.filterStore.ageTypes);
92-
this.getMultiplefilterIDB(arrFilters, this.rootStore.filterStore.populationTypes);
90+
this.getMultiplefilterIDB(arrFilters, this.rootStore.filterStore.whoStore.genderTypes);
91+
this.getMultiplefilterIDB(arrFilters, this.rootStore.filterStore.whoStore.ageTypes);
92+
this.getMultiplefilterIDB(arrFilters, this.rootStore.filterStore.whoStore.populationTypes);
9393
this.getMultiplefilterIDB(arrFilters, this.rootStore.filterStore.accidentType);
9494
this.getMultiplefilterIDB(arrFilters, this.rootStore.filterStore.vehicleType);
9595
this.getMultiplefilterIDB(arrFilters, this.rootStore.filterStore.speedLimit);

src/utils/filterStoreUtils.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ export function getFilterQueryString(fs: any, bounds: any, useBounds: boolean =
3838
query += fs.roads.getFilter();
3939
query += fs.roadSegment.getFilter();
4040
query += fs.injTypes.getFilter();
41-
query += fs.genderTypes.getFilter();
42-
query += fs.ageTypes.getFilter();
43-
query += fs.populationTypes.getFilter();
41+
query += fs.whoStore.genderTypes.getFilter();
42+
query += fs.whoStore.ageTypes.getFilter();
43+
query += fs.whoStore.populationTypes.getFilter();
4444
query += fs.accidentType.getFilter();
4545
query += fs.vehicleType.getFilter();
4646
query += fs.involvedVehicle.getFilter();
@@ -60,9 +60,7 @@ export function setFiltersText(fs: any, ignoreIfAll: boolean) {
6060
fs.endYear.setText();
6161
fs.injTypes.setText(ignoreIfAll);
6262
fs.dayNight.setText(ignoreIfAll);
63-
fs.genderTypes.setText(ignoreIfAll);
64-
fs.ageTypes.setText(ignoreIfAll);
65-
fs.populationTypes.setText(ignoreIfAll);
63+
fs.whoStore.setText(ignoreIfAll);
6664
fs.locationAccuracy.setText(ignoreIfAll);
6765
fs.roadTypes.setText(ignoreIfAll);
6866
const cityNamesArr = getCitiesNames(fs.cities.arrValues);
@@ -84,9 +82,7 @@ export function setBrowserQueryString(fs: any) {
8482
fs.injurySeverity.setBrowserQueryString(params, false);
8583
fs.roadTypes.setBrowserQueryString(params);
8684
fs.injTypes.setBrowserQueryString(params);
87-
fs.genderTypes.setBrowserQueryString(params);
88-
fs.ageTypes.setBrowserQueryString(params);
89-
fs.populationTypes.setBrowserQueryString(params);
85+
fs.whoStore.setBrowserQueryString(params);
9086
fs.cities.setBrowserQueryString(params);
9187
fs.streets.setBrowserQueryString(params);
9288
fs.roads.setBrowserQueryString(params);
@@ -117,9 +113,7 @@ export function setStoreByQuery(fs: any, params: URLSearchParams, defCity?: stri
117113
fs.locationAccuracy.setValuesByQuery(params);
118114
fs.roadTypes.setValuesByQuery(params);
119115
fs.injTypes.setValuesByQuery(params);
120-
fs.genderTypes.setValuesByQuery(params);
121-
fs.ageTypes.setValuesByQuery(params);
122-
fs.populationTypes.setValuesByQuery(params);
116+
fs.whoStore.setValuesByQuery(params);
123117
fs.accidentType.setValuesByQuery(params);
124118
fs.vehicleType.setValuesByQuery(params);
125119
fs.involvedVehicle.setValuesByQuery(params);

0 commit comments

Comments
 (0)