-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: show icon for situation or notice (#57)
- Loading branch information
1 parent
6ca8716
commit 92c3b2a
Showing
10 changed files
with
175 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { SituationOrNoticeIcon } from './situation-or-notice-icon'; | ||
export * from './types'; | ||
export * from './utils'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React from 'react'; | ||
import { getIconForMostCriticalSituationOrNotice } from './utils'; | ||
import { NoticeFragment, SituationFragment } from './types'; | ||
import { ColorIcon } from '@atb/components/icon'; | ||
|
||
type SituationOrNoticeIconProps = { | ||
accessibilityLabel?: string; | ||
notices?: NoticeFragment[]; | ||
cancellation?: boolean; | ||
} & ({ situations: SituationFragment[] } | { situation: SituationFragment }); | ||
|
||
export const SituationOrNoticeIcon = ({ | ||
accessibilityLabel, | ||
notices, | ||
cancellation, | ||
...props | ||
}: SituationOrNoticeIconProps) => { | ||
const situations = | ||
'situation' in props ? [props.situation] : props.situations; | ||
|
||
// TODO: It might be needed to check if the transport is flexible and shows a yellow icon (warning) | ||
const icon = getIconForMostCriticalSituationOrNotice( | ||
situations, | ||
notices, | ||
cancellation, | ||
); | ||
if (!icon) return null; | ||
|
||
return <ColorIcon icon={icon} alt={accessibilityLabel} />; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export type NoticeFragment = { | ||
id: string; | ||
text: string | null; | ||
}; | ||
|
||
export type SituationFragment = { | ||
situationNumber: string | null; | ||
reportType: 'general' | 'incident' | null; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { NoticeFragment, SituationFragment } from './types'; | ||
import { Statuses } from '@atb-as/theme'; | ||
import { ColorIcons } from '@atb/components/icon'; | ||
export const getMessageTypeForSituation = (situation: SituationFragment) => | ||
situation.reportType === 'incident' ? 'warning' : 'info'; | ||
|
||
export const getMsgTypeForMostCriticalSituationOrNotice = ( | ||
situations: SituationFragment[], | ||
notices?: NoticeFragment[], | ||
cancellation: boolean = false, | ||
): Statuses | undefined => { | ||
if (cancellation) return 'error'; | ||
if (!situations.length) { | ||
return notices?.length ? 'info' : undefined; | ||
} | ||
return situations | ||
.map(getMessageTypeForSituation) | ||
.reduce( | ||
(mostCritical, msgType) => | ||
msgType === 'warning' ? 'warning' : mostCritical, | ||
'info', | ||
); | ||
}; | ||
|
||
export const getIconForMostCriticalSituationOrNotice = ( | ||
situations: SituationFragment[], | ||
notices?: NoticeFragment[], | ||
cancellation: boolean = false, | ||
) => { | ||
const msgType = getMsgTypeForMostCriticalSituationOrNotice( | ||
situations, | ||
notices, | ||
cancellation, | ||
); | ||
return msgType && messageTypeToIcon(msgType); | ||
}; | ||
|
||
const messageTypeToIcon = (messageType: Statuses): ColorIcons => { | ||
switch (messageType) { | ||
case 'warning': | ||
return 'status/Warning'; | ||
case 'error': | ||
return 'status/Error'; | ||
case 'valid': | ||
return 'status/Check'; | ||
default: | ||
return 'status/Info'; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/page-modules/assistant/trip/trip-pattern/trip-pattern-header/rail-replacement-bus.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { TripPattern } from '../../../server/journey-planner/validators'; | ||
import { useTranslation, PageText } from '@atb/translations'; | ||
import { ColorIcon } from '@atb/components/icon'; | ||
|
||
export const RailReplacementBusMessage = ({ | ||
tripPattern, | ||
}: { | ||
tripPattern: TripPattern; | ||
}) => { | ||
const { t } = useTranslation(); | ||
|
||
if ( | ||
!tripPattern.legs.some( | ||
(leg) => leg.transportSubmode === 'railReplacementBus', | ||
) | ||
) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<ColorIcon | ||
icon="status/Warning" | ||
alt={t( | ||
PageText.Assistant.trip.tripPattern.tripIncludesRailReplacementBus, | ||
)} | ||
/> | ||
); | ||
}; |
3 changes: 2 additions & 1 deletion
3
...ge-modules/assistant/trip/trip-pattern/trip-pattern-header/trip-pattern-header.module.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
.header { | ||
display: flex; | ||
justify-content: space-between; | ||
gap: var(--spacings-small); | ||
padding: var(--spacings-small) var(--spacings-medium); | ||
} | ||
|
||
.header__duration { | ||
color: var(--text-colors-secondary); | ||
margin-left: auto; | ||
} |
23 changes: 23 additions & 0 deletions
23
src/page-modules/assistant/trip/trip-pattern/trip-pattern-header/utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { NoticeFragment } from '@atb/modules/situations/types'; | ||
import { onlyUniquesBasedOnField } from '@atb/utils/only-uniques'; | ||
import { Leg } from '../../../server/journey-planner/validators'; | ||
|
||
/** | ||
* Filter notices by removing duplicates (by id), removing those without text, | ||
* and also sorting them since the order from Entur may change on each request. | ||
*/ | ||
export const filterNotices = ( | ||
notices: NoticeFragment[], | ||
): Required<NoticeFragment>[] => | ||
notices | ||
.filter((n): n is Required<NoticeFragment> => !!n.text) | ||
.filter(onlyUniquesBasedOnField('id')) | ||
.sort((s1, s2) => s1.id.localeCompare(s2.id)); | ||
|
||
export const getNoticesForLeg = (leg: Leg) => | ||
filterNotices([ | ||
...(leg.line?.notices || []), | ||
...(leg.serviceJourney?.notices || []), | ||
...(leg.serviceJourney?.journeyPattern?.notices || []), | ||
...(leg.fromEstimatedCall?.notices || []), | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const onlyUniques = <T>(value: T, index: number, self: T[]) => | ||
self.indexOf(value) === index; | ||
|
||
export const onlyUniquesBasedOnField = | ||
<T>(field: keyof T) => | ||
(element: T, index: number, array: T[]) => | ||
array.findIndex((el) => el[field] === element[field]) === index; |