-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Refine routes definitions #4579
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c33a3cc
Refine routes definitions
kravets-levko 8348fe0
Replace HoC wrappers with functions to create route definition
kravets-levko e90f2ce
Some updates for code consistency
kravets-levko abe6b5b
ItemsList component: remove currentRoute dependency
kravets-levko 1a79f30
Prepare route parametes in wrapper functions
kravets-levko 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
59 changes: 0 additions & 59 deletions
59
client/app/components/ApplicationArea/AuthenticatedPageWrapper.jsx
This file was deleted.
Oops, something went wrong.
41 changes: 0 additions & 41 deletions
41
client/app/components/ApplicationArea/SignedOutPageWrapper.jsx
This file was deleted.
Oops, something went wrong.
63 changes: 63 additions & 0 deletions
63
client/app/components/ApplicationArea/routeWithApiKeySession.jsx
This file contains hidden or 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,63 @@ | ||
| import React, { useEffect, useState, useContext } from "react"; | ||
| import PropTypes from "prop-types"; | ||
| import { ErrorBoundaryContext } from "@/components/ErrorBoundary"; | ||
| import { Auth } from "@/services/auth"; | ||
|
|
||
| // This wrapper modifies `route.render` function and instead of passing `currentRoute` passes an object | ||
| // that contains: | ||
| // - `currentRoute.routeParams` | ||
| // - `pageTitle` field which is equal to `currentRoute.title` | ||
| // - `onError` field which is a `handleError` method of nearest error boundary | ||
| // - `apiKey` field | ||
|
|
||
| function ApiKeySessionWrapper({ apiKey, currentRoute, renderChildren }) { | ||
| const [isAuthenticated, setIsAuthenticated] = useState(false); | ||
| const { handleError } = useContext(ErrorBoundaryContext); | ||
|
|
||
| useEffect(() => { | ||
| let isCancelled = false; | ||
| Auth.setApiKey(apiKey); | ||
| Auth.loadConfig() | ||
| .then(() => { | ||
| if (!isCancelled) { | ||
| setIsAuthenticated(true); | ||
| } | ||
| }) | ||
| .catch(() => { | ||
| if (!isCancelled) { | ||
| setIsAuthenticated(false); | ||
| } | ||
| }); | ||
| return () => { | ||
| isCancelled = true; | ||
| }; | ||
| }, [apiKey]); | ||
|
|
||
| if (!isAuthenticated) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <React.Fragment key={currentRoute.key}> | ||
| {renderChildren({ ...currentRoute.routeParams, pageTitle: currentRoute.title, onError: handleError, apiKey })} | ||
| </React.Fragment> | ||
| ); | ||
| } | ||
|
|
||
| ApiKeySessionWrapper.propTypes = { | ||
| apiKey: PropTypes.string.isRequired, | ||
| renderChildren: PropTypes.func, | ||
| }; | ||
|
|
||
| ApiKeySessionWrapper.defaultProps = { | ||
| renderChildren: () => null, | ||
| }; | ||
|
|
||
| export default function routeWithApiKeySession({ render, getApiKey, ...rest }) { | ||
| return { | ||
| ...rest, | ||
| render: currentRoute => ( | ||
| <ApiKeySessionWrapper apiKey={getApiKey(currentRoute)} currentRoute={currentRoute} renderChildren={render} /> | ||
| ), | ||
| }; | ||
| } |
82 changes: 82 additions & 0 deletions
82
client/app/components/ApplicationArea/routeWithUserSession.jsx
This file contains hidden or 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,82 @@ | ||
| import React, { useEffect, useState } from "react"; | ||
| import PropTypes from "prop-types"; | ||
| import ErrorBoundary, { ErrorBoundaryContext } from "@/components/ErrorBoundary"; | ||
| import { Auth } from "@/services/auth"; | ||
| import organizationStatus from "@/services/organizationStatus"; | ||
| import ApplicationHeader from "./ApplicationHeader"; | ||
| import ErrorMessage from "./ErrorMessage"; | ||
|
|
||
| // This wrapper modifies `route.render` function and instead of passing `currentRoute` passes an object | ||
| // that contains: | ||
| // - `currentRoute.routeParams` | ||
| // - `pageTitle` field which is equal to `currentRoute.title` | ||
| // - `onError` field which is a `handleError` method of nearest error boundary | ||
|
|
||
| function UserSessionWrapper({ bodyClass, currentRoute, renderChildren }) { | ||
| const [isAuthenticated, setIsAuthenticated] = useState(!!Auth.isAuthenticated()); | ||
|
|
||
| useEffect(() => { | ||
| let isCancelled = false; | ||
| Promise.all([Auth.requireSession(), organizationStatus.refresh()]) | ||
| .then(() => { | ||
| if (!isCancelled) { | ||
| setIsAuthenticated(!!Auth.isAuthenticated()); | ||
| } | ||
| }) | ||
| .catch(() => { | ||
| if (!isCancelled) { | ||
| setIsAuthenticated(false); | ||
| } | ||
| }); | ||
| return () => { | ||
| isCancelled = true; | ||
| }; | ||
| }, []); | ||
|
|
||
| useEffect(() => { | ||
| if (bodyClass) { | ||
| document.body.classList.toggle(bodyClass, true); | ||
| return () => { | ||
| document.body.classList.toggle(bodyClass, false); | ||
| }; | ||
| } | ||
| }, [bodyClass]); | ||
|
|
||
| if (!isAuthenticated) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <React.Fragment> | ||
| <ApplicationHeader /> | ||
| <React.Fragment key={currentRoute.key}> | ||
| <ErrorBoundary renderError={error => <ErrorMessage error={error} />}> | ||
| <ErrorBoundaryContext.Consumer> | ||
| {({ handleError }) => | ||
| renderChildren({ ...currentRoute.routeParams, pageTitle: currentRoute.title, onError: handleError }) | ||
| } | ||
| </ErrorBoundaryContext.Consumer> | ||
| </ErrorBoundary> | ||
| </React.Fragment> | ||
| </React.Fragment> | ||
| ); | ||
| } | ||
|
|
||
| UserSessionWrapper.propTypes = { | ||
| bodyClass: PropTypes.string, | ||
| renderChildren: PropTypes.func, | ||
| }; | ||
|
|
||
| UserSessionWrapper.defaultProps = { | ||
| bodyClass: null, | ||
| renderChildren: () => null, | ||
| }; | ||
|
|
||
| export default function routeWithUserSession({ render, bodyClass, ...rest }) { | ||
| return { | ||
| ...rest, | ||
| render: currentRoute => ( | ||
| <UserSessionWrapper bodyClass={bodyClass} currentRoute={currentRoute} renderChildren={render} /> | ||
| ), | ||
| }; | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.