-
Notifications
You must be signed in to change notification settings - Fork 412
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add parse environment id , project id , organisation id
- Loading branch information
Showing
2 changed files
with
90 additions
and
80 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { matchPath } from 'react-router' | ||
import get from 'lodash/get' | ||
import AccountStore from './stores/account-store' | ||
import { withRouter } from 'react-router-dom' | ||
import { FC } from 'react' | ||
|
||
function getProjectId() { | ||
const location = document.location | ||
const pathname = location.pathname | ||
const match = matchPath(pathname, { | ||
exact: false, | ||
path: '/project/:projectId/environment/:environmentId', | ||
strict: false, | ||
}) | ||
const match2 = matchPath(pathname, { | ||
exact: false, | ||
path: '/project/:projectId', | ||
strict: false, | ||
}) | ||
const projectId = | ||
get(match, 'params.projectId') || get(match2, 'params.projectId') | ||
return projectId ? parseInt(projectId) : null | ||
} | ||
|
||
function getEnvironmentId() { | ||
const location = document.location | ||
const pathname = location.pathname | ||
|
||
const match = matchPath(pathname, { | ||
exact: false, | ||
path: '/project/:projectId/environment/:environmentId', | ||
strict: false, | ||
}) | ||
|
||
const environmentId = get(match, 'params.environmentId') | ||
return environmentId | ||
} | ||
function getOrganisationId() { | ||
const location = document.location | ||
const pathname = location.pathname | ||
|
||
const match = matchPath(pathname, { | ||
exact: false, | ||
path: '/organisation/:organisationId', | ||
strict: false, | ||
}) | ||
const storeId = AccountStore.getOrganisation()?.id | ||
const organisationId = get(match, 'params.organisationId') | ||
return organisationId | ||
? parseInt(organisationId) | ||
: storeId | ||
? parseInt(storeId) | ||
: null | ||
} | ||
export type WithParamsProps = { | ||
organisationId: number | null | ||
projectId: number | null | ||
environmentId: number | null | ||
} | ||
export default function withParams(WrappedComponent: any) { | ||
const HOC: FC<WithParamsProps> = (props) => { | ||
const organisationId = getOrganisationId() | ||
const projectId = getProjectId() | ||
|
||
const environmentId = getEnvironmentId() | ||
return ( | ||
<WrappedComponent | ||
{...props} | ||
organisationId={organisationId} | ||
environmentId={environmentId} | ||
projectId={projectId} | ||
/> | ||
) | ||
} | ||
|
||
return withRouter(HOC as any) | ||
} |
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