Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom GraphQL and Mobility Profile Routing #1292

Merged
merged 44 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
a38eb68
feat(apiV2): Pass user's mobilityProfile (if present) to plan queries.
binh-dam-ibigroup Dec 28, 2023
3418d81
Merge branch 'dev' into mobility-profile-routing
binh-dam-ibigroup Jan 19, 2024
7c26245
Merge branch 'dev' into mobility-profile-routing
binh-dam-ibigroup Jan 25, 2024
49c0e67
chore(deps): Add reference to OTP-UI branch for mobility profile
binh-dam-ibigroup Jan 29, 2024
ad6c453
Merge branch 'dev' into mobility-profile-routing
binh-dam-ibigroup Feb 2, 2024
5036045
Merge branch 'dev' into mobility-profile-routing
binh-dam-ibigroup Feb 8, 2024
12d0911
Merge branch 'dev' into mobility-profile-routing
binh-dam-ibigroup Mar 4, 2024
9cd8840
Merge branch 'dev' into mobility-profile-routing
binh-dam-ibigroup Mar 14, 2024
9032b75
Merge branch 'dev' into mobility-profile-routing
binh-dam-ibigroup Mar 22, 2024
d876e2c
Merge branch 'dev' into mobility-profile-routing
binh-dam-ibigroup Mar 28, 2024
dbcdd46
Merge branch 'dev' into mobility-profile-routing
binh-dam-ibigroup Apr 8, 2024
b3a6135
fix(actions/user): Replan with profile after fetching user data.
binh-dam-ibigroup Apr 10, 2024
f082068
fix(actions/user): Don't replan if within the account screens.
binh-dam-ibigroup Apr 11, 2024
981040b
Merge branch 'dev' into mobility-profile-routing
binh-dam-ibigroup Apr 24, 2024
d9b7b55
Merge branch 'dev' into mobility-profile-routing
binh-dam-ibigroup Apr 26, 2024
7354a9d
refactor(MobilityPane): Use new Link component to navigate to mobilit…
binh-dam-ibigroup Apr 26, 2024
0c6b24c
Merge branch 'dev' into mobility-profile-routing
binh-dam-ibigroup May 1, 2024
e0cfb11
Merge branch 'dev' into mobility-profile-routing
binh-dam-ibigroup May 9, 2024
ceeea83
fix(NavLoginButton): Make help link valid when running locally
binh-dam-ibigroup May 16, 2024
c60c4e4
Merge branch 'dev' into mobility-profile-routing
binh-dam-ibigroup May 16, 2024
41e7f79
Merge branch 'dev' into mobility-profile-routing
binh-dam-ibigroup May 17, 2024
def95b6
Merge branch 'dev' into mobility-profile-routing
binh-dam-ibigroup May 20, 2024
4857ae5
Merge branch 'dev' into mobility-profile-routing
binh-dam-ibigroup Jul 17, 2024
f6cb60e
Merge branch 'dev' into mobility-profile-routing
binh-dam-ibigroup Jul 26, 2024
87eed37
chore(deps): Downgrade some packages
binh-dam-ibigroup Jul 26, 2024
fdd9c7a
chore(deps): Update core-utils to 11.4.2-mobility-profile
binh-dam-ibigroup Jul 29, 2024
cbd0f67
Merge branch 'dev' into mobility-profile-routing
binh-dam-ibigroup Aug 14, 2024
26fde56
chore(deps): Update core-utils to 1.4.3-mobility-profile
binh-dam-ibigroup Aug 14, 2024
49826a2
Merge branch 'dev' into mobility-profile-routing
binh-dam-ibigroup Sep 9, 2024
78be055
Merge branch 'dev' into mobility-profile-routing
binh-dam-ibigroup Sep 25, 2024
4ff7e50
refactor(apiV2): Pass mobility profile param to OTP based on config.
binh-dam-ibigroup Sep 25, 2024
e71a995
Merge branch 'dev' into mobility-profile-routing-2
binh-dam-ibigroup Oct 11, 2024
ad9ce17
chore(deps): Update core-utils to 12.0.1.
binh-dam-ibigroup Oct 11, 2024
76c8339
Merge branch 'middleware-graphql' into mobility-profile-routing-2
binh-dam-ibigroup Oct 11, 2024
0bac0e1
Merge branch 'dev' into mobility-profile-routing-2
binh-dam-ibigroup Oct 15, 2024
cc48fac
Merge branch 'trip-preview' into mobility-profile-routing-2
binh-dam-ibigroup Oct 15, 2024
450b772
Merge branch 'dev' into mobility-profile-routing-2
binh-dam-ibigroup Oct 16, 2024
788f1cb
chore(deps): Add webpack raw-loader
binh-dam-ibigroup Oct 16, 2024
9d606ba
build(craco): Include local graphql file in build.
binh-dam-ibigroup Oct 16, 2024
1f44061
feat(main): Use plan GraphQL template from config
binh-dam-ibigroup Oct 16, 2024
c403cf0
chore(example-config): Remove custom plan query
binh-dam-ibigroup Oct 17, 2024
3726007
chore(craco): Update comment for graphql files
binh-dam-ibigroup Nov 1, 2024
e60f694
refactor(main): Update comment regarding custom graphql query.
binh-dam-ibigroup Nov 1, 2024
f5074c8
Merge branch 'dev' into mobility-profile-routing-2
binh-dam-ibigroup Nov 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion craco.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ module.exports = {
findBackwardsCompatibleEnvVar('JS_CONFIG')
backwardsCompatibleEnv.HTML_FILE =
findBackwardsCompatibleEnvVar('HTML_FILE')
backwardsCompatibleEnv.PLAN_QUERY_RESOURCE_URI =
findBackwardsCompatibleEnvVar('PLAN_QUERY_RESOURCE_URI')
backwardsCompatibleEnv.CUSTOM_CSS =
findBackwardsCompatibleEnvVar('CUSTOM_CSS')

Expand All @@ -65,6 +67,13 @@ module.exports = {
}
addBeforeLoader(webpackConfig, loaderByName('file-loader'), yamlLoader)

// Support import of raw GraphQL files
const graphqlLoader = {
loader: ['raw-loader'],
test: /\.graphql$/
}
addBeforeLoader(webpackConfig, loaderByName('file-loader'), graphqlLoader)

// Support webfonts (for font awesome)
const webfontLoader = {
loader: ['url-loader'],
Expand All @@ -82,7 +91,7 @@ module.exports = {
loader.exclude = /node_modules/
})

// Gather the CSS, HTML, YAML, and JS override files.
// Gather the CSS, HTML, YAML, GraphQL, and JS override files.
const CUSTOM_CSS =
(process.env && process.env.CUSTOM_CSS) ||
backwardsCompatibleEnv.CUSTOM_CSS ||
Expand All @@ -91,6 +100,22 @@ module.exports = {
(process.env && process.env.HTML_FILE) ||
backwardsCompatibleEnv.HTML_FILE ||
'lib/index.tpl.html'
// resolve the custom GraphQL file. If it is present, copy the file to a
// temporary folder within this project so that it can be bundled and loaded at runtime.
let customPlanGraphQLFile = './planQuery.graphql'
const PLAN_QUERY_RESOURCE_URI =
(process.env && process.env.PLAN_QUERY_RESOURCE_URI) ||
backwardsCompatibleEnv.PLAN_QUERY_RESOURCE_URI ||
'node_modules/@opentripplanner/core-utils/src/planQuery.graphql'
if (PLAN_QUERY_RESOURCE_URI) {
const splitPath = PLAN_QUERY_RESOURCE_URI.split(path.sep)
customPlanGraphQLFile = `../tmp/${splitPath[splitPath.length - 1]}`
// copy location is relative to root, while js file for app is relative to lib
fs.copySync(
PLAN_QUERY_RESOURCE_URI,
`./tmp/${splitPath[splitPath.length - 1]}`
)
}
const YAML_CONFIG =
(process.env && process.env.YAML_CONFIG) ||
backwardsCompatibleEnv.YAML_CONFIG ||
Expand Down Expand Up @@ -143,6 +168,7 @@ module.exports = {
new webpack.DefinePlugin({
CSS: JSON.stringify(CUSTOM_CSS),
JS_CONFIG: JSON.stringify(customJsFile),
PLAN_QUERY_RESOURCE: JSON.stringify(customPlanGraphQLFile),
// Optionally override the default config files with some other
// files.
YAML_CONFIG: JSON.stringify(YAML_CONFIG)
Expand Down
7 changes: 6 additions & 1 deletion lib/actions/apiV2.js
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,8 @@ export function routingQuery(searchId = null, updateSearchInReducer) {
return function (dispatch, getState) {
const state = getState()
const { config, currentQuery, modeSettingDefinitions } = state.otp
const { planQuery } = config.api
const { loggedInUser } = state.user
const persistenceMode = getPersistenceMode(config.persistence)
const activeItinerary =
getActiveItinerary(state) ||
Expand Down Expand Up @@ -1047,6 +1049,9 @@ export function routingQuery(searchId = null, updateSearchInReducer) {
...currentQuery,
numItineraries: numItineraries || getDefaultNumItineraries(config)
}
if (config.mobilityProfile) {
baseQuery.mobilityProfile = loggedInUser?.mobilityProfile?.mobilityMode
}
Comment on lines +1052 to +1054
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a GMAP specific branch for otp-rr? Do we want to bring this into dev?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not yet, although try to move specific stuff to config would be best.

// Generate combinations if the modes for query are not specified in the query
// FIXME: BICYCLE_RENT does not appear in this list unless TRANSIT is also enabled.
// This is likely due to the fact that BICYCLE_RENT is treated as a transit submode.
Expand All @@ -1070,7 +1075,7 @@ export function routingQuery(searchId = null, updateSearchInReducer) {
const query = generateOtp2Query(combo)
dispatch(
createGraphQLQueryAction(
query.query,
planQuery || query.query,
query.variables,
(response) => {
const dispatchedRoutingResponse = routingResponse(response)
Expand Down
20 changes: 19 additions & 1 deletion lib/actions/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ function setUser(user, fetchTrips) {
return function (dispatch, getState) {
positionHomeAndWorkFirst(user)
// If mobility profile is enabled, set a default selection for "no mobility devices".
if (getState().otp.config.mobilityProfile) {
const hasMobilityProfile = !!getState().otp.config.mobilityProfile
if (hasMobilityProfile) {
setAtLeastNoMobilityDevice(user)
}
dispatch(setCurrentUser(user))
Expand All @@ -287,6 +288,23 @@ function setUser(user, fetchTrips) {
if (!isBlank(preferredLocale)) {
dispatch(setLocale(preferredLocale))
}

// Also replan itinerary search for the current user profile.
if (
hasMobilityProfile &&
!getState().router.location.pathname.startsWith('/account')
) {
// TODO: Refactor below.
// This prevents some kind of race condition whose origin I can't figure
// out. Unless this is called after redux catches up with routing to the '/'
// path, then the old path will be used and the screen won't change.
// Therefore, this timeout occurs so that the view of the homepage has time
// to render itself.
// FIXME: remove hack
setTimeout(() => {
dispatch(routingQuery())
}, 300)
}
}
}

Expand Down
27 changes: 8 additions & 19 deletions lib/components/user/mobility-profile/mobility-pane.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
import { Button } from 'react-bootstrap'
import { connect } from 'react-redux'
import { FormattedList, FormattedMessage } from 'react-intl'
import { FormikProps } from 'formik'
import React, { useCallback } from 'react'
import React from 'react'

import * as uiActions from '../../../actions/ui'
import { EditedUser } from '../types'
import { NONE_SINGLETON } from '../../../util/user'

interface Props extends FormikProps<EditedUser> {
routeTo: (url: string) => void
}
import Link from '../../util/link'

/**
* Renders a button to show the mobility profile settings.
*/
const MobilityPane = ({ routeTo, values: userData }: Props): JSX.Element => {
const handleClick = useCallback(() => {
routeTo('/account/mobilityProfile/')
}, [routeTo])
const MobilityPane = ({
values: userData
}: FormikProps<EditedUser>): JSX.Element => {
const {
isMobilityLimited,
mobilityDevices = [],
Expand Down Expand Up @@ -54,15 +47,11 @@ const MobilityPane = ({ routeTo, values: userData }: Props): JSX.Element => {
id={`components.MobilityProfile.LimitationsPane.visionLimitations.${visionLimitation}`}
/>
</p>
<Button bsStyle="primary" onClick={handleClick}>
<Link className="btn btn-primary" to="/account/mobilityProfile/">
<FormattedMessage id="components.MobilityProfile.MobilityPane.button" />
</Button>
</Link>
</div>
)
}

const mapDispatchToProps = {
routeTo: uiActions.routeTo
}

export default connect(null, mapDispatchToProps)(MobilityPane)
export default MobilityPane
1 change: 1 addition & 0 deletions lib/components/user/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export type VisionLimitation = typeof visionLimitations[number]
export interface MobilityProfile {
isMobilityLimited: boolean
mobilityDevices: string[]
mobilityMode: string
visionLimitation: VisionLimitation
}

Expand Down
12 changes: 12 additions & 0 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ import(CSS)
// eslint-disable-next-line no-undef
const otpConfig = require(YAML_CONFIG)

// Loads a JavaScript file which is set in the webpack section of the craco.config.js file.
// This setting is defined from a custom environment setting passed into webpack or
// defaults to ./config.js
// defined in webpack config:
// The JS_CONFIG variable is passed to this file by webpack's `DefinePlugin` that replaces the variable
// with its content at compile time (like C's `#define` preprocessor directive).
// eslint-disable-next-line no-undef
const jsConfig = require(JS_CONFIG).configure(otpConfig)

// If defined, plug custom plan query into the redux config, so it is available from actions.
otpConfig.api.planQuery = jsConfig.planQuery

const history = createHashHistory()

const middleware = [
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@
"pinst": "^2.1.6",
"prettier": "^2.3.2",
"puppeteer": "^10.2.0",
"raw-loader": "^4.0.2",
"react-refresh": "^0.10.0",
"react-scripts": "^4.0.3",
"redux-mock-store": "^1.5.3",
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15163,6 +15163,14 @@ [email protected]:
iconv-lite "0.4.24"
unpipe "1.0.0"

raw-loader@^4.0.2:
version "4.0.2"
resolved "https://registry.yarnpkg.com/raw-loader/-/raw-loader-4.0.2.tgz#1aac6b7d1ad1501e66efdac1522c73e59a584eb6"
integrity sha512-ZnScIV3ag9A4wPX/ZayxL/jZH+euYb6FcUinPcgiQW0+UBtEv0O6Q3lGd3cqJ+GHH+rksEv3Pj99oxJ3u3VIKA==
dependencies:
loader-utils "^2.0.0"
schema-utils "^3.0.0"

rc@^1.0.1, rc@^1.1.6, rc@^1.2.8:
version "1.2.8"
resolved "https://registry.yarnpkg.com/rc/-/rc-1.2.8.tgz#cd924bf5200a075b83c188cd6b9e211b7fc0d3ed"
Expand Down
Loading