-
Notifications
You must be signed in to change notification settings - Fork 0
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
Wire up redirect from legacy map to new map #936
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
01b7e40
Wire up redirect from legacy map to new map
jernestmyers 70ab2ee
Wire up modal on legacy map redirect
jernestmyers 6d77cff
Tweak redirect popup message
jernestmyers 5d822c4
Prevent duplicate analysis in redirect handler
jernestmyers 6419191
Invoke setter earlier
jernestmyers 520f88c
Merge branch 'main' into SAM-map-redirects
jernestmyers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
149 changes: 149 additions & 0 deletions
149
packages/libs/web-common/src/controllers/LegacyMapRedirectHandler.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,149 @@ | ||
import React, { useCallback, useMemo, useState } from 'react'; | ||
import QueryString from 'querystring'; | ||
import { edaServiceUrl } from '../config'; | ||
import { useConfiguredAnalysisClient } from '@veupathdb/eda/lib/core/hooks/client'; | ||
import { createComputation } from '@veupathdb/eda/lib/core/components/computations/Utils'; | ||
import { makeNewAnalysis } from '@veupathdb/eda/lib/core'; | ||
import { RouteComponentProps } from 'react-router'; | ||
import { LegacyRedirectState } from '@veupathdb/eda/lib/map/analysis/MapAnalysis'; | ||
|
||
// Define constants to create new computations and analyses | ||
const MEGA_STUDY_ID = 'DS_480c976ef9'; | ||
const MEGA_STUDIES_ENTITY_ID = 'EUPATH_0000605'; | ||
const POPBIO_ID_VARIABLE_ID = 'POPBIO_8000215'; | ||
const DESCRIPTOR_TYPE = 'stringSet'; | ||
const REDIRECT_ANALYSIS_DESCRIPTION = | ||
'This map was created from a legacy PopBio map link.'; | ||
const DEFAULT_COMPUTATION = createComputation( | ||
'pass', | ||
undefined, | ||
[], | ||
[], | ||
undefined, | ||
'Unnamed computation' | ||
); | ||
|
||
export function LegacyMapRedirectHandler({ | ||
history, | ||
match, | ||
location, | ||
}: RouteComponentProps) { | ||
const analysisClient = useConfiguredAnalysisClient(edaServiceUrl); | ||
const queryParams = useMemo( | ||
() => QueryString.parse(location.search.slice(1)), | ||
[location.search] | ||
); | ||
const [hasCreatedAnalysis, setHasCreatedAnalysis] = useState(false); | ||
|
||
const handleLegacyMapRedirect = useCallback( | ||
async (computation, additionalConfig = {}, legacyMapRedirectState) => { | ||
if (hasCreatedAnalysis) return; | ||
setHasCreatedAnalysis(true); | ||
const { analysisId } = await analysisClient.createAnalysis( | ||
makeNewAnalysis(MEGA_STUDY_ID, computation, additionalConfig) | ||
); | ||
history.push({ | ||
pathname: `${match.url.replace( | ||
'/legacy-redirect-handler', | ||
'' | ||
)}/${MEGA_STUDY_ID}/${analysisId}`, | ||
state: legacyMapRedirectState as LegacyRedirectState, | ||
}); | ||
}, | ||
[analysisClient, history, match, hasCreatedAnalysis, setHasCreatedAnalysis] | ||
); | ||
|
||
const baseAdditionalAnalysisConfig = { | ||
description: REDIRECT_ANALYSIS_DESCRIPTION, | ||
}; | ||
|
||
const paramKeys = Object.keys(queryParams); | ||
|
||
if (paramKeys.length) { | ||
if ('projectID' in queryParams) { | ||
const descriptorConfig = { | ||
descriptor: { | ||
subset: { | ||
descriptor: [ | ||
{ | ||
entityId: MEGA_STUDIES_ENTITY_ID, | ||
variableId: POPBIO_ID_VARIABLE_ID, | ||
type: DESCRIPTOR_TYPE, | ||
[DESCRIPTOR_TYPE]: [queryParams['projectID']], | ||
}, | ||
], | ||
}, | ||
}, | ||
}; | ||
|
||
if (paramKeys.length === 1) { | ||
// We know we have only the projectID param, so make new analysis and redirect | ||
const additionalAnalysisConfig = { | ||
...baseAdditionalAnalysisConfig, | ||
...descriptorConfig, | ||
}; | ||
handleLegacyMapRedirect( | ||
DEFAULT_COMPUTATION, | ||
additionalAnalysisConfig, | ||
undefined | ||
); | ||
} else { | ||
// Here we have a projectID and other param(s), so populate the Notes -> Analysis Details info | ||
// with the additional param(s) and pass along the legacyMapRedirectState object | ||
const notes = composeParamListForNotesString(queryParams); | ||
const additionalAnalysisConfig = { | ||
...descriptorConfig, | ||
...baseAdditionalAnalysisConfig, | ||
notes, | ||
}; | ||
const legacyMapRedirectState = { | ||
showLegacyMapRedirectModal: true, | ||
projectId: queryParams['projectID'], | ||
}; | ||
handleLegacyMapRedirect( | ||
DEFAULT_COMPUTATION, | ||
additionalAnalysisConfig, | ||
legacyMapRedirectState | ||
); | ||
} | ||
} else { | ||
// Here we have query params but no projectID, so populate the Notes -> Analysis Description info | ||
// with the additional param(s) and pass along the legacyMapRedirectState object | ||
const notes = composeParamListForNotesString(queryParams); | ||
const additionalAnalysisConfig = { | ||
...baseAdditionalAnalysisConfig, | ||
notes, | ||
}; | ||
const legacyMapRedirectState = { | ||
showLegacyMapRedirectModal: true, | ||
projectId: undefined, | ||
}; | ||
handleLegacyMapRedirect( | ||
DEFAULT_COMPUTATION, | ||
additionalAnalysisConfig, | ||
legacyMapRedirectState | ||
); | ||
} | ||
} else { | ||
// no query params, so create a default map analysis with only the baseAdditionalAnalysisConfig and redirect | ||
handleLegacyMapRedirect( | ||
DEFAULT_COMPUTATION, | ||
baseAdditionalAnalysisConfig, | ||
undefined | ||
); | ||
} | ||
return null; | ||
} | ||
|
||
function composeParamListForNotesString( | ||
queryParams: QueryString.ParsedUrlQuery | ||
) { | ||
let description = | ||
'The following parameters from the old link were present but ignored:\n'; | ||
for (const [key, value] of Object.entries(queryParams)) { | ||
if (key !== 'projectID') { | ||
description += `${key}: ${JSON.stringify(value)}\n`; | ||
} | ||
} | ||
return description; | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I looked at this yesterday and then had to do other things before I could comment.
Why don't we check that the contents of
locationState
are what is expected? Currently we're assuming that any history redirect with state metadata is a legacy redirect. It might not be in future?@jernestmyers @dmfalke
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like we should be checking for the presence and truthiness of
locationState.showLegacyMapRedirectModal
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you're right. I sort of glossed over this section. All the code right now is good, in that we aren't in danger of hitting
TypeError: undefined
errors.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe in the future it should be more like this, with some io-ts magic?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've addressed this feedback in a new PR: #983