Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #24480 - fix org switching on subs page
- Loading branch information
Showing
5 changed files
with
82 additions
and
10 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
19 changes: 19 additions & 0 deletions
19
webpack/components/WithOrganization/__snapshots__/withOrganization.test.js.snap
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,19 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`subscriptions page should render select org page 1`] = ` | ||
<CheckOrg> | ||
<Connect(withRouter(SetOrganization)) | ||
redirectPath="/test" | ||
/> | ||
</CheckOrg> | ||
`; | ||
|
||
exports[`subscriptions page should render the wrapped component 1`] = ` | ||
<CheckOrg> | ||
<WrappedComponent> | ||
<div> | ||
Wrapped! | ||
</div> | ||
</WrappedComponent> | ||
</CheckOrg> | ||
`; |
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 |
---|---|---|
@@ -1,28 +1,54 @@ | ||
import React, { Component } from 'react'; | ||
import { orgId } from '../../services/api'; | ||
import PropTypes from 'prop-types'; | ||
import { get } from 'lodash'; | ||
import SetOrganization from '../SelectOrg/SetOrganization'; | ||
import titleWithCaret from '../../helpers/caret'; | ||
|
||
function withOrganization(WrappedComponent, redirectPath) { | ||
return class CheckOrg extends Component { | ||
const checkOrg = class CheckOrg extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { orgId: null }; | ||
} | ||
static getDerivedStateFromProps(newProps, state) { | ||
const orgNodeId = document.getElementById('organization-id').dataset.id; | ||
|
||
if (state.orgId !== orgNodeId) { | ||
return { orgId: orgNodeId }; | ||
} | ||
return null; | ||
} | ||
|
||
componentDidUpdate(prevProps) { | ||
const { location } = this.props; | ||
|
||
// TODO: use topbar react component | ||
const orgTitle = location.state && location.state.orgChanged; | ||
const prevOrgTitle = prevProps.location.state && prevProps.location.state.orgChanged; | ||
const orgTitle = get(location, 'state.orgChanged'); | ||
const prevOrgTitle = get(prevProps, 'location.state.orgChanged'); | ||
|
||
if (orgTitle !== prevOrgTitle) { | ||
document.getElementById('organization-dropdown').children[0].innerHTML = titleWithCaret(orgTitle); | ||
} | ||
} | ||
render() { | ||
if (!orgId()) { | ||
const { location } = this.props; | ||
const newOrgSelected = get(location, 'state.orgChanged'); | ||
|
||
if (newOrgSelected) { | ||
return <WrappedComponent {...this.props} />; | ||
} else if (this.state.orgId === '') { | ||
return <SetOrganization redirectPath={redirectPath} />; | ||
} | ||
return <WrappedComponent {...this.props} />; | ||
} | ||
}; | ||
checkOrg.propTypes = { | ||
location: PropTypes.shape(PropTypes.object), | ||
}; | ||
checkOrg.defaultProps = { | ||
location: undefined, | ||
}; | ||
return checkOrg; | ||
} | ||
|
||
export default withOrganization; |
27 changes: 27 additions & 0 deletions
27
webpack/components/WithOrganization/withOrganization.test.js
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,27 @@ | ||
import React from 'react'; | ||
import { mount } from 'enzyme'; | ||
import toJson from 'enzyme-to-json'; | ||
import withOrganization from './withOrganization'; | ||
|
||
jest.mock('../SelectOrg/SetOrganization'); | ||
jest.mock('../../helpers/caret'); | ||
|
||
describe('subscriptions page', () => { | ||
const WrappedComponent = () => <div> Wrapped! </div>; | ||
|
||
it('should render the wrapped component', () => { | ||
global.document.getElementById = () => ({ dataset: { id: 1 } }); | ||
|
||
const Component = withOrganization(WrappedComponent, '/test'); | ||
const page = mount(<Component />); | ||
expect(toJson(page)).toMatchSnapshot(); | ||
}); | ||
|
||
it('should render select org page', () => { | ||
global.document.getElementById = () => ({ dataset: { id: '' } }); | ||
|
||
const Component = withOrganization(WrappedComponent, '/test'); | ||
const page = mount(<Component />); | ||
expect(toJson(page)).toMatchSnapshot(); | ||
}); | ||
}); |
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