Skip to content

Commit

Permalink
feat: extend travel mode filter selection to use selected_as_default …
Browse files Browse the repository at this point in the history
…property (#293)

* set tranposrt mode filters using selected_as_default flag from firestore.

* extend filter.fixture with selected_as_default property.

* add .env.nfk to gitignore.

* remove exessive useEffect.
  • Loading branch information
jonasbrunvoll authored May 14, 2024
1 parent a4453ce commit bc036ab
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ yarn-error.log*
.env*.local
.env*.atb
.env*.fram
.env*.nfk

# vercel
.vercel
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"dependencies": {
"@apollo/client": "^3.9.7",
"@atb-as/config-specs": "^3.16.0",
"@atb-as/config-specs": "^3.23.0",
"@atb-as/theme": "^8.1.0",
"@github/combobox-nav": "^2.3.1",
"@isaacs/ttlcache": "^1.4.1",
Expand Down
7 changes: 7 additions & 0 deletions src/modules/transport-mode/filter/__tests__/filter.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const filter: TransportModeFilterOptionType[] = [
transportMode: 'trolleybus',
},
],
selectedAsDefault: true,
},
{
id: 'rail',
Expand Down Expand Up @@ -79,6 +80,7 @@ export const filter: TransportModeFilterOptionType[] = [
transportSubModes: ['railReplacementBus'],
},
],
selectedAsDefault: false,
},
{
id: 'expressboat',
Expand Down Expand Up @@ -112,6 +114,7 @@ export const filter: TransportModeFilterOptionType[] = [
],
},
],
selectedAsDefault: true,
},
{
id: 'ferry',
Expand Down Expand Up @@ -143,6 +146,7 @@ export const filter: TransportModeFilterOptionType[] = [
],
},
],
selectedAsDefault: true,
},
{
id: 'airportbus',
Expand All @@ -169,6 +173,7 @@ export const filter: TransportModeFilterOptionType[] = [
transportSubModes: ['airportLinkBus'],
},
],
selectedAsDefault: true,
},
{
id: 'air',
Expand All @@ -194,6 +199,7 @@ export const filter: TransportModeFilterOptionType[] = [
transportMode: 'air',
},
],
selectedAsDefault: false,
},
{
id: 'other',
Expand Down Expand Up @@ -248,5 +254,6 @@ export const filter: TransportModeFilterOptionType[] = [
transportMode: 'lift',
},
],
selectedAsDefault: true,
},
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { describe, expect, it } from 'vitest';
import { setTransportModeFilters } from '../utils';
import { TravelSearchFiltersType } from '@atb-as/config-specs';

describe('setTransportModeFilters', () => {
it('should only return transport mode filters where `selectedAsDefault` is true.', () => {
const tmfInput = [
{ id: 'bus', selectedAsDefault: true },
{ id: 'air', selectedAsDefault: false },
] as TravelSearchFiltersType['transportModes'];

const transportModeFilters = setTransportModeFilters(tmfInput);
expect(transportModeFilters).toEqual(['bus']);
});

it('should not return transport mode filters where `selectedAsDefault` is undefined.', () => {
const tmfInput = [
{ id: 'bus', selectedAsDefault: true },
{ id: 'air' },
{ id: 'rail', selectedAsDefault: undefined },
] as TravelSearchFiltersType['transportModes'];

const transportModeFilters = setTransportModeFilters(tmfInput);
expect(transportModeFilters).toEqual(['bus']);
});
});
25 changes: 4 additions & 21 deletions src/page-modules/assistant/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,10 @@ import { PageText, useTranslation } from '@atb/translations';
import { FocusScope } from '@react-aria/focus';
import { AnimatePresence, motion } from 'framer-motion';
import { useRouter } from 'next/router';
import {
FormEventHandler,
PropsWithChildren,
useEffect,
useState,
} from 'react';
import { FormEventHandler, PropsWithChildren, useState } from 'react';
import style from './assistant.module.css';
import { FromToTripQuery } from './types';
import { createTripQuery } from './utils';
import { createTripQuery, setTransportModeFilters } from './utils';
import { TabLink } from '@atb/components/tab-link';
import { logSpecificEvent } from '@atb/modules/firebase';
import { getOrgData } from '@atb/modules/org-data';
Expand Down Expand Up @@ -122,20 +117,8 @@ function AssistantLayout({ children, tripQuery }: AssistantLayoutProps) {
const { urls, orgId } = getOrgData();
const { isDarkMode } = useTheme();

/*
* Temporary solution until firebase configuration is in place.
*/
useEffect(() => {
if (tripQuery.transportModeFilter === null)
onTransportFilterChanged(
transportModeFilter
?.filter(
(filter) =>
!filter.modes.some((mode) => mode.transportMode === 'air'),
)
.map((filter) => filter.id) ?? null,
);
}, [transportModeFilter]); //eslint-disable-line react-hooks/exhaustive-deps
if (tripQuery.transportModeFilter === null)
onTransportFilterChanged(setTransportModeFilters(transportModeFilter));

return (
<div>
Expand Down
11 changes: 11 additions & 0 deletions src/page-modules/assistant/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { SearchMode } from '@atb/modules/search-time';
import { searchTimeToQueryString } from '@atb/modules/search-time';
import { GeocoderFeature } from '@atb/page-modules/departures';
import { FromToTripQuery, TripData, TripQuery, TripQuerySchema } from './types';
import { TravelSearchFiltersType } from '@atb-as/config-specs';

export function filterOutDuplicates(
arrayToFilter: TripData['tripPatterns'],
Expand Down Expand Up @@ -122,3 +123,13 @@ export function tripQueryToQueryString(input: TripQuery): string {
)
.join('&');
}

export function setTransportModeFilters(
transportModes: TravelSearchFiltersType['transportModes'],
) {
return (
transportModes
?.filter((filter) => filter.selectedAsDefault)
.map((filter) => filter.id) ?? null
);
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@
dependencies:
node-fetch "^2.6.1"

"@atb-as/config-specs@^3.16.0":
version "3.16.0"
resolved "https://registry.yarnpkg.com/@atb-as/config-specs/-/config-specs-3.16.0.tgz#c0693176ad894283a99eca421c9d158436e9f9bb"
integrity sha512-+xmB4zKpAWumMjmbwu2Mpyzm5gkejZC2+f+dc1hFQrQnyCcv+6BOIfKfqUs52ziJqzeK2l4YSUlaqqB8lkrrkQ==
"@atb-as/config-specs@^3.23.0":
version "3.23.0"
resolved "https://registry.yarnpkg.com/@atb-as/config-specs/-/config-specs-3.23.0.tgz#f915ac43df1d7d59d7e964feb27106c4940647ed"
integrity sha512-p3n8E38r2mXggZE6HuTD/Nz/3KOnRnxux5mwqw2xeuClYuP6HnxaC6k7FUo1sAouHZTqA64C+C2Ld0hWMI2dlQ==
dependencies:
ajv "^8.12.0"
ajv-formats "^2.1.1"
Expand Down

0 comments on commit bc036ab

Please sign in to comment.