-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1304 from opentripplanner/trip-sharing
Trip sharing
- Loading branch information
Showing
19 changed files
with
449 additions
and
93 deletions.
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
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 |
---|---|---|
|
@@ -52,5 +52,6 @@ | |
"low-vision", | ||
"legally-blind" | ||
] | ||
} | ||
}, | ||
"ignoredIds": ["otpUi.TripDetails.title"] | ||
} |
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
95 changes: 95 additions & 0 deletions
95
lib/components/user/mobility-profile/companion-selector.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,95 @@ | ||
import { connect } from 'react-redux' | ||
import React, { lazy, Suspense, useCallback } from 'react' | ||
|
||
import { AppReduxState } from '../../../util/state-types' | ||
import { CompanionInfo, User } from '../types' | ||
import StatusBadge from '../../util/status-badge' | ||
|
||
export interface Option { | ||
label: string | ||
value: CompanionInfo | ||
} | ||
|
||
// @ts-expect-error: No types for react-select. | ||
const Select = lazy(() => import('react-select')) | ||
|
||
function notNull(item: unknown) { | ||
return !!item | ||
} | ||
|
||
function makeOption(companion?: CompanionInfo) { | ||
return { | ||
label: companion?.nickname || companion?.email, | ||
value: companion | ||
} | ||
} | ||
|
||
function isConfirmed({ status }: CompanionInfo) { | ||
return status === 'CONFIRMED' | ||
} | ||
|
||
function formatOptionLabel(option: Option) { | ||
if (!isConfirmed(option.value)) { | ||
return ( | ||
<> | ||
{option.label} <StatusBadge status={option.value.status} /> | ||
</> | ||
) | ||
} else { | ||
return option.label | ||
} | ||
} | ||
|
||
const CompanionSelector = ({ | ||
disabled, | ||
excludedUsers = [], | ||
loggedInUser, | ||
multi = false, | ||
onChange, | ||
selectedCompanions | ||
}: { | ||
disabled?: boolean | ||
excludedUsers?: (CompanionInfo | undefined)[] | ||
loggedInUser?: User | ||
multi?: boolean | ||
onChange: (e: Option | Option[]) => void | ||
selectedCompanions?: (CompanionInfo | undefined)[] | ||
}): JSX.Element => { | ||
const companionOptions = (loggedInUser?.relatedUsers || []) | ||
.filter(notNull) | ||
.filter(isConfirmed) | ||
.map(makeOption) | ||
const companionValues = multi | ||
? selectedCompanions?.filter(notNull).map(makeOption) | ||
: selectedCompanions?.[0] | ||
? makeOption(selectedCompanions[0]) | ||
: null | ||
|
||
const isOptionDisabled = useCallback( | ||
(option: Option) => excludedUsers.includes(option?.value), | ||
[excludedUsers] | ||
) | ||
|
||
return ( | ||
<Suspense fallback={<span>...</span>}> | ||
<Select | ||
formatOptionLabel={formatOptionLabel} | ||
isClearable | ||
isDisabled={disabled} | ||
isMulti={multi} | ||
isOptionDisabled={isOptionDisabled} | ||
onChange={onChange} | ||
options={companionOptions} | ||
value={companionValues} | ||
/> | ||
</Suspense> | ||
) | ||
} | ||
|
||
const mapStateToProps = (state: AppReduxState) => { | ||
return { | ||
loggedInUser: state.user.loggedInUser | ||
} | ||
} | ||
|
||
export default connect(mapStateToProps)(CompanionSelector) |
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
114 changes: 114 additions & 0 deletions
114
lib/components/user/mobility-profile/trip-companions-pane.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,114 @@ | ||
import { connect } from 'react-redux' | ||
import { FormattedMessage, IntlShape, useIntl } from 'react-intl' | ||
import { FormikProps } from 'formik' | ||
import React, { useCallback, useEffect } from 'react' | ||
|
||
import * as userActions from '../../../actions/user' | ||
import { AppReduxState } from '../../../util/state-types' | ||
import { getDependentName } from '../../../util/user' | ||
import { MonitoredTrip, User } from '../types' | ||
|
||
import CompanionSelector, { Option } from './companion-selector' | ||
|
||
type Props = FormikProps<MonitoredTrip> & { | ||
getDependentUserInfo: (userIds: string[], intl: IntlShape) => void | ||
isReadOnly: boolean | ||
loggedInUser: User | ||
} | ||
|
||
function optionValue(option: Option | null) { | ||
if (!option) return null | ||
return option?.value | ||
} | ||
|
||
/** | ||
* Pane for showing/setting trip companions and observers. | ||
*/ | ||
const TripCompanions = ({ | ||
getDependentUserInfo, | ||
isReadOnly, | ||
loggedInUser, | ||
setFieldValue, | ||
values: trip | ||
}: Props): JSX.Element => { | ||
const handleCompanionChange = useCallback( | ||
(option: Option | Option[] | null) => { | ||
if (!option || 'label' in option) { | ||
setFieldValue('companion', optionValue(option)) | ||
} | ||
}, | ||
[setFieldValue] | ||
) | ||
|
||
const handleObserversChange = useCallback( | ||
(options: Option | Option[] | null) => { | ||
if (!options || 'length' in options) { | ||
setFieldValue('observers', (options || []).map(optionValue)) | ||
} | ||
}, | ||
[setFieldValue] | ||
) | ||
|
||
const intl = useIntl() | ||
const dependents = loggedInUser?.dependents | ||
|
||
useEffect(() => { | ||
if (dependents && dependents.length > 0) { | ||
getDependentUserInfo(dependents, intl) | ||
} | ||
}, [dependents, getDependentUserInfo, intl]) | ||
|
||
const { companion, observers, primary } = trip | ||
|
||
const iAmThePrimaryTraveler = | ||
(!primary && trip.userId === loggedInUser?.id) || | ||
primary?.userId === loggedInUser?.id | ||
|
||
const primaryTraveler = iAmThePrimaryTraveler | ||
? intl.formatMessage({ id: 'components.MobilityProfile.myself' }) | ||
: primary | ||
? primary.name || primary.email | ||
: getDependentName( | ||
loggedInUser?.dependentsInfo?.find((d) => d.userId === trip.userId) | ||
) | ||
|
||
return ( | ||
<div> | ||
<p> | ||
<FormattedMessage id="components.TripCompanionsPane.primaryLabel" /> | ||
<strong>{primaryTraveler}</strong> | ||
</p> | ||
<p> | ||
<FormattedMessage id="components.TripCompanionsPane.companionLabel" /> | ||
<CompanionSelector | ||
disabled={isReadOnly || !iAmThePrimaryTraveler} | ||
excludedUsers={observers} | ||
onChange={handleCompanionChange} | ||
selectedCompanions={[companion]} | ||
/> | ||
</p> | ||
<p> | ||
<FormattedMessage id="components.TripCompanionsPane.observersLabel" /> | ||
<CompanionSelector | ||
disabled={isReadOnly} | ||
excludedUsers={[companion]} | ||
multi | ||
onChange={handleObserversChange} | ||
selectedCompanions={observers} | ||
/> | ||
</p> | ||
</div> | ||
) | ||
} | ||
|
||
// connect to the redux store | ||
|
||
const mapStateToProps = (state: AppReduxState) => ({ | ||
loggedInUser: state.user.loggedInUser | ||
}) | ||
|
||
const mapDispatchToProps = { | ||
getDependentUserInfo: userActions.getDependentUserInfo | ||
} | ||
|
||
export default connect(mapStateToProps, mapDispatchToProps)(TripCompanions) |
Oops, something went wrong.