Skip to content
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

feat(redwood): add nau learning header customization #3

Merged
merged 5 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Header.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ ensureConfig([
subscribe(APP_CONFIG_INITIALIZED, () => {
mergeConfig({
AUTHN_MINIMAL_HEADER: !!process.env.AUTHN_MINIMAL_HEADER,
ENABLE_ORG_LOGO: !!process.env.ENABLE_ORG_LOGO,
}, 'Header additional config');
});

Expand Down
6 changes: 3 additions & 3 deletions src/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ $white: #fff;
.user-dropdown {
.btn {
height: 3rem;
// @media (max-width: -1 + map-get($grid-breakpoints, "sm")) {
// padding: 0 0.5rem;
// }
@media (map-get($grid-breakpoints, "sm")) {
padding: 0 0.5rem;
}
}
}
}
Expand Down
40 changes: 28 additions & 12 deletions src/learning-header/LearningHeader.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext } from 'react';
import React, { useContext, useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import { getConfig } from '@edx/frontend-platform';
import { injectIntl, intlShape } from '@edx/frontend-platform/i18n';
Expand All @@ -7,6 +7,7 @@ import { AppContext } from '@edx/frontend-platform/react';
import AnonymousUserMenu from './AnonymousUserMenu';
import AuthenticatedUserDropdown from './AuthenticatedUserDropdown';
import messages from './messages';
import getCourseLogoOrg from './data/api';

const LinkedLogo = ({
href,
Expand All @@ -24,11 +25,18 @@ LinkedLogo.propTypes = {
src: PropTypes.string.isRequired,
alt: PropTypes.string.isRequired,
};

const LearningHeader = ({
courseOrg, courseNumber, courseTitle, intl, showUserDropdown,
courseOrg, courseTitle, intl, showUserDropdown,
}) => {
const { authenticatedUser } = useContext(AppContext);
const [logoOrg, setLogoOrg] = useState(null);
const enableOrgLogo = getConfig().ENABLE_ORG_LOGO || false;

useEffect(() => {
if (courseOrg) {
getCourseLogoOrg().then((logoOrgUrl) => { setLogoOrg(logoOrgUrl); });
}
});

const headerLogo = (
<LinkedLogo
Expand All @@ -44,17 +52,27 @@ const LearningHeader = ({
<a className="sr-only sr-only-focusable" href="#main-content">{intl.formatMessage(messages.skipNavLink)}</a>
<div className="container-xl py-2 d-flex align-items-center">
{headerLogo}
<div className="flex-grow-1 course-title-lockup" style={{ lineHeight: 1 }}>
<span className="d-block small m-0">{courseOrg} {courseNumber}</span>
<span className="d-block m-0 font-weight-bold course-title">{courseTitle}</span>
<div className="d-none d-md-block flex-grow-1 course-title-lockup">
<div className={`d-md-flex ${enableOrgLogo && 'align-items-center justify-content-center'} w-100`}>
{enableOrgLogo ? (
(courseOrg && logoOrg)
&& <img src={logoOrg} alt={`${courseOrg} logo`} style={{ maxHeight: '45px' }} />
) : null}
<span
className="d-inline-block course-title font-weight-semibold ml-3 text-truncate text-left w-25"
style={{ fontSize: '0.85rem' }}
>
{courseTitle}
</span>
</div>
</div>
{showUserDropdown && authenticatedUser && (
<AuthenticatedUserDropdown
username={authenticatedUser.username}
/>
<AuthenticatedUserDropdown
username={authenticatedUser.username}
/>
)}
{showUserDropdown && !authenticatedUser && (
<AnonymousUserMenu />
<AnonymousUserMenu />
)}
</div>
</header>
Expand All @@ -63,15 +81,13 @@ const LearningHeader = ({

LearningHeader.propTypes = {
courseOrg: PropTypes.string,
courseNumber: PropTypes.string,
courseTitle: PropTypes.string,
intl: intlShape.isRequired,
showUserDropdown: PropTypes.bool,
};

LearningHeader.defaultProps = {
courseOrg: null,
courseNumber: null,
courseTitle: null,
showUserDropdown: true,
};
Expand Down
17 changes: 12 additions & 5 deletions src/learning-header/LearningHeader.test.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import React from 'react';
import {
authenticatedUser, initializeMockApp, render, screen,
authenticatedUser, initializeMockApp, render, screen, waitFor,
} from '../setupTest';
import { LearningHeader as Header } from '../index';

jest.mock('./data/api', () => ({
getCourseLogoOrg: jest.fn().mockResolvedValue(Promise.resolve('logo-url')),
}));

describe('Header', () => {
beforeAll(async () => {
// We need to mock AuthService to implicitly use `getAuthenticatedUser` within `AppContext.Provider`.
Expand All @@ -18,12 +22,15 @@ describe('Header', () => {
it('displays course data', () => {
const courseData = {
courseOrg: 'course-org',
courseNumber: 'course-number',
courseTitle: 'course-title',
};
render(<Header {...courseData} />);

expect(screen.getByText(`${courseData.courseOrg} ${courseData.courseNumber}`)).toBeInTheDocument();
expect(screen.getByText(courseData.courseTitle)).toBeInTheDocument();
waitFor(
() => {
expect(screen.getByAltText(`${courseData.courseOrg} logo`)).toHaveAttribute('src', 'logo-url');
expect(screen.getByText(`${courseData.courseOrg}`)).toBeInTheDocument();
expect(screen.getByText(courseData.courseTitle)).toBeInTheDocument();
},
);
});
});
10 changes: 10 additions & 0 deletions src/learning-header/_header.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@import "../../node_modules/bootstrap/scss/bootstrap-grid";
@import "../../node_modules/bootstrap/scss/mixins/breakpoints";

.logo {
img {
@include media-breakpoint-down(sm) {
max-width: 85% !important;
}
}
}
22 changes: 22 additions & 0 deletions src/learning-header/data/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { getConfig } from '@edx/frontend-platform';
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';

const getCourseLogoOrg = async () => {
try {
const orgId = window.location.pathname.match(/course-(.*?):([^+]+)/)[2];
const { data } = await getAuthenticatedHttpClient()
.get(
`${getConfig().LMS_BASE_URL}/api/organizations/v0/organizations/${orgId}/`,
{ useCache: true },
);
return data.logo;
} catch (error) {
const { httpErrorStatus } = error && error.customAttributes;
if (httpErrorStatus === 404) {
return null;
}
throw error;
}
};

export default getCourseLogoOrg;
59 changes: 59 additions & 0 deletions src/learning-header/data/api.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { getConfig } from '@edx/frontend-platform';
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';
import getCourseLogoOrg from './api';
import { initializeMockApp } from '../../setupTest';

jest.mock('@edx/frontend-platform/auth');

class CustomError extends Error {
constructor(httpErrorStatus) {
super();
this.customAttributes = {
httpErrorStatus,
};
}
}

describe('getCourseLogoOrg', () => {
beforeEach(async () => {
// We need to mock AuthService to implicitly use `getAuthenticatedHttpClient` within `AppContext.Provider`.
await initializeMockApp();
delete window.location;
getAuthenticatedHttpClient.mockReset();
});

it('should return the organization logo when the URL is valid', async () => {
window.location = new URL(`${getConfig().BASE_URL}/learning/course/course-v1:edX+DemoX+Demo_Course/home`);
getAuthenticatedHttpClient.mockReturnValue({
get: async () => Promise.resolve({
data: {
logo: 'https://example.com/logo.svg',
},
}),
});
const logoOrg = await getCourseLogoOrg();
expect(logoOrg).toBe('https://example.com/logo.svg');
});

it('should return null when the organization logo is not found', async () => {
window.location = new URL(`${getConfig().BASE_URL}/learning/course/course-v1:edX+DemoX+Nonexistent_Course/home`);
getAuthenticatedHttpClient.mockReturnValue({
get: async () => {
throw new CustomError(404);
},
});
const logoOrg = await getCourseLogoOrg();
expect(logoOrg).toBeNull();
});

it('should throw an error when an unexpected error occurs', async () => {
const customError = new CustomError(500);
window.location = new URL(`${getConfig().BASE_URL}/learning/course/course-v1:edX+DemoX+Demo_Course/home`);
getAuthenticatedHttpClient.mockReturnValue({
get: async () => {
throw customError;
},
});
await expect(getCourseLogoOrg()).rejects.toThrow(customError);
});
});
Loading