Skip to content

Commit

Permalink
refactor: add hoc to use params
Browse files Browse the repository at this point in the history
  • Loading branch information
Syed-Ali-Abbas-Zaidi committed Apr 7, 2023
1 parent 6ea6c30 commit 2e8bdf7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
18 changes: 9 additions & 9 deletions src/profile/ProfilePage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import { profilePageSelector } from './data/selectors';
// i18n
import messages from './ProfilePage.messages';

import withParams from '../utils/hoc';

ensureConfig(['CREDENTIALS_BASE_URL', 'LMS_BASE_URL'], 'ProfilePage');

class ProfilePage extends React.Component {
Expand All @@ -63,9 +65,9 @@ class ProfilePage extends React.Component {
}

componentDidMount() {
this.props.fetchProfile(this.props.match.params.username);
this.props.fetchProfile(this.props.params.username);
sendTrackingLogEvent('edx.profile.viewed', {
username: this.props.match.params.username,
username: this.props.params.username,
});
}

Expand Down Expand Up @@ -102,7 +104,7 @@ class ProfilePage extends React.Component {
}

isAuthenticatedUserProfile() {
return this.props.match.params.username === this.context.authenticatedUser.username;
return this.props.params.username === this.context.authenticatedUser.username;
}

// Inserted into the DOM in two places (for responsive layout)
Expand All @@ -124,7 +126,7 @@ class ProfilePage extends React.Component {

return (
<span data-hj-suppress>
<h1 className="h2 mb-0 font-weight-bold">{this.props.match.params.username}</h1>
<h1 className="h2 mb-0 font-weight-bold">{this.props.params.username}</h1>
<DateJoined date={dateJoined} />
{this.isYOBDisabled() && <UsernameDescription />}
<hr className="d-none d-md-block" />
Expand Down Expand Up @@ -370,10 +372,8 @@ ProfilePage.propTypes = {
updateDraft: PropTypes.func.isRequired,

// Router
match: PropTypes.shape({
params: PropTypes.shape({
username: PropTypes.string.isRequired,
}).isRequired,
params: PropTypes.shape({
username: PropTypes.string.isRequired,
}).isRequired,

// i18n
Expand Down Expand Up @@ -410,4 +410,4 @@ export default connect(
closeForm,
updateDraft,
},
)(injectIntl(ProfilePage));
)(injectIntl(withParams(ProfilePage)));
15 changes: 7 additions & 8 deletions src/profile/ProfilePage.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const requiredProfilePageProps = {
deleteProfilePhoto: () => {},
openField: () => {},
closeField: () => {},
match: { params: { username: 'staff' } },
params: { username: 'staff' },
};

// Mock language cookie
Expand Down Expand Up @@ -67,29 +67,28 @@ beforeEach(() => {
});

const ProfilePageWrapper = ({
contextValue, store, match, requiresParentalConsent,
contextValue, store, params, requiresParentalConsent,
}) => (
<AppContext.Provider
value={contextValue}
>
<IntlProvider locale="en">
<Provider store={store}>
<ProfilePage {...requiredProfilePageProps} match={match} requiresParentalConsent={requiresParentalConsent} />
<ProfilePage {...requiredProfilePageProps} params={params} requiresParentalConsent={requiresParentalConsent} />
</Provider>
</IntlProvider>
</AppContext.Provider>
);

ProfilePageWrapper.defaultProps = {
match: { params: { username: 'staff' } },
params: { username: 'staff' },
requiresParentalConsent: null,

};

ProfilePageWrapper.propTypes = {
contextValue: PropTypes.shape({}).isRequired,
store: PropTypes.shape({}).isRequired,
match: PropTypes.shape({}),
params: PropTypes.shape({}),
requiresParentalConsent: PropTypes.bool,
};

Expand Down Expand Up @@ -124,7 +123,7 @@ describe('<ProfilePage />', () => {
<ProfilePageWrapper
contextValue={contextValue}
store={mockStore(storeMocks.viewOtherProfile)}
match={{ params: { username: 'verified' } }} // Override default match
params={{ username: 'verified' }} // Override default params
/>
);
const tree = renderer.create(component).toJSON();
Expand Down Expand Up @@ -279,7 +278,7 @@ describe('<ProfilePage />', () => {
<ProfilePageWrapper
contextValue={contextValue}
store={mockStore(storeMocks.loadingApp)}
match={{ params: { username: 'test-username' } }}
params={{ username: 'test-username' }}
/>
);
const wrapper = mount(component);
Expand Down
10 changes: 10 additions & 0 deletions src/utils/hoc.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react';

import { useParams } from 'react-router-dom';

const withParams = (WrappedComponent) => {
const WithParamsComponent = (props) => <WrappedComponent params={useParams()} {...props} />;
return WithParamsComponent;
};

export default withParams;

0 comments on commit 2e8bdf7

Please sign in to comment.