Skip to content

Commit

Permalink
feat: Added CoursewareSearchResults UI component
Browse files Browse the repository at this point in the history
  • Loading branch information
rijuma committed Oct 25, 2023
1 parent 7c92110 commit 71cc670
Show file tree
Hide file tree
Showing 7 changed files with 353 additions and 52 deletions.
71 changes: 19 additions & 52 deletions src/course-home/courseware-search/CoursewareSearch.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useState } from 'react';
import { useDispatch } from 'react-redux';
import { injectIntl, intlShape } from '@edx/frontend-platform/i18n';
import { Button, Icon } from '@edx/paragon';
Expand All @@ -10,15 +10,27 @@ import { useElementBoundingBox, useLockScroll } from './hooks';
import messages from './messages';

import CoursewareSearchForm from './CoursewareSearchForm';
import CoursewareSearchResults from './CoursewareSearchResults';
import mockedData from './test-data/mockedResults';

const CoursewareSearch = ({ intl, ...sectionProps }) => {
const [results, setResults] = useState();
const dispatch = useDispatch();

useLockScroll();

const info = useElementBoundingBox('courseTabsNavigation');
const top = info ? `${Math.floor(info.top)}px` : 0;

const handleSubmit = (search) => {
if (!search) {
setResults(undefined);
return;

Check warning on line 28 in src/course-home/courseware-search/CoursewareSearch.jsx

View check run for this annotation

Codecov / codecov/patch

src/course-home/courseware-search/CoursewareSearch.jsx#L27-L28

Added lines #L27 - L28 were not covered by tests
}

setResults(search.toLowerCase() !== 'lorem ipsum' ? mockedData : []);
};

return (
<section className="courseware-search" style={{ '--modal-top-position': top }} data-testid="courseware-search-section" {...sectionProps}>
<div className="courseware-search__close">
Expand All @@ -32,60 +44,15 @@ const CoursewareSearch = ({ intl, ...sectionProps }) => {
</Button>
</div>
<div className="courseware-search__outer-content">
<div className="courseware-search__content" style={{ height: '999px' }}>
<div className="courseware-search__content">
<h2>{intl.formatMessage(messages.searchModuleTitle)}</h2>
<CoursewareSearchForm
onSubmit={handleSubmit}
placeholder={intl.formatMessage(messages.searchBarPlaceholderText)}
/>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis semper rutrum odio quis congue.
Duis sodales nibh et sapien elementum fermentum. Quisque magna urna, gravida at gravida et,
ultricies vel massa.Aliquam in vehicula dolor, id scelerisque felis.
Morbi posuere scelerisque tincidunt. Proin et gravida tortor. Vestibulum vel orci vulputate,
gravida justo eu, varius dolor. Etiam viverra diam sed est tincidunt, et aliquam est efficitur.
Donec imperdiet eros quis est condimentum faucibus.
</p>
<p>
In mattis, tellus ut lacinia viverra, ligula ex sagittis ex, sed mollis ex enim ut velit.
Nunc elementum, risus eget feugiat scelerisque, sapien felis laoreet nisl, ut pharetra neque
lorem a elit. Maecenas elementum, metus fringilla suscipit imperdiet, mi nunc efficitur elit,
sed consequat massa magna sit amet dui. Curabitur ultrices nisi vel lorem scelerisque, pharetra
luctus nunc pulvinar. Morbi aliquam ante eget arcu condimentum consectetur. Fusce faucibus lacus
sed pretium ultrices. Curabitur neque lacus, elementum convallis augue placerat, gravida
scelerisque ipsum. Donec bibendum lectus id ullamcorper sodales. Integer quis ante facilisis erat
maximus viverra. Nunc rutrum posuere lectus, aliquam congue odio blandit nec. Phasellus placerat,
magna non bibendum lacinia, tortor orci vulputate dui, vitae imperdiet turpis dui nec tortor.
Praesent porttitor mollis diam ut gravida. Praesent vitae felis dignissim sem accumsan dignissim.
Fusce ullamcorper bibendum ante ac pellentesque. Aliquam sed leo vel leo pellentesque cursus a at risus.
Donec sollicitudin maximus diam, sit amet molestie sapien commodo at.
</p>
<p>
Cras ornare pulvinar est id rhoncus. Aenean ut risus magna. Fusce cursus pulvinar dui ut egestas.
Quisque condimentum risus non mi sagittis, eu facilisis enim hendrerit. Integer faucibus dapibus rutrum.
Nullam vitae mollis tortor, eu lacinia mi. Nunc commodo ex id eros hendrerit, vel interdum augue tristique.
Suspendisse ullamcorper, purus in vestibulum auctor, justo nisi finibus dolor,
nec dignissim arcu enim a augue.
</p>
<p>
Fusce vel libero odio. Orci varius natoque penatibus et magnis dis parturient montes,
nascetur ridiculus mus. Pellentesque at varius turpis. Ut pulvinar efficitur congue. Vivamus cursus,
purus at aliquet malesuada, felis quam blandit dolor, a interdum justo est semper augue.
In eu lectus sit amet est pellentesque porta vel eget magna. Morbi sollicitudin turpis vitae faucibus
pulvinar. Etiam placerat pulvinar porta.
</p>
<p>
Suspendisse mattis eget felis non sagittis. Nulla facilisi. In bibendum cursus purus, non venenatis tellus
dignissim sit amet. Phasellus volutpat ipsum turpis, non imperdiet nisi elementum a. Nunc mollis, sapien
cursus vehicula consectetur, nunc turpis pulvinar mauris, at varius justo mi egestas nisi. Fusce semper
sapien in orci rhoncus ornare. Donec maximus mi eu pulvinar convallis.
</p>
<p>
Nullam tortor sem, hendrerit eu sapien ac, venenatis rhoncus ligula. Donec nibh leo, venenatis sed interdum
ac, pharetra sed nibh. Orci varius natoque penatibus et magnis dis parturient montes,
nascetur ridiculus mus. Sed congue risus eu mattis condimentum. In id nulla sit amet magna suscipit
consectetur. Nullam vitae augue felis. In consequat tempus diam, a eleifend ante bibendum ac.
Vivamus mi orci, fermentum ac viverra quis, tristique a ipsum. Morbi imperdiet porta sem, in sollicitudin
risus dignissim at. Nulla dapibus iaculis vestibulum.
</p>
{results !== undefined ? (
<CoursewareSearchResults results={results} />

Check warning on line 54 in src/course-home/courseware-search/CoursewareSearch.jsx

View check run for this annotation

Codecov / codecov/patch

src/course-home/courseware-search/CoursewareSearch.jsx#L54

Added line #L54 was not covered by tests
) : null}
</div>
</div>
</section>
Expand Down
78 changes: 78 additions & 0 deletions src/course-home/courseware-search/CoursewareSearchResults.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import React from 'react';
import { injectIntl, intlShape } from '@edx/frontend-platform/i18n';
import {
Folder, TextFields, VideoCamera, Article,
} from '@edx/paragon/icons';
import { Icon } from '@edx/paragon';
import PropTypes from 'prop-types';
import messages from './messages';

const iconTypeMapping = {
document: Folder,
text: TextFields,
video: VideoCamera,
};
const defaultIcon = Article;

const CoursewareSearchResults = ({ intl, results }) => {
if (!results.length) {
return (
<div className="courseware-search-results">
<p data-testid="no-results">{intl.formatMessage(messages.searchResultsNone)}</p>
</div>
);
}

return (
<div className="courseware-search-results">
{results.map(({
title, href, type, breadcrumbs, contentMatches, isExternal,
}) => {
const key = type.toLowerCase();
const icon = iconTypeMapping[key] || defaultIcon;

const linkProps = { href };

if (isExternal) {
linkProps.target = '_blank';
linkProps.rel = 'nofollow';
}

return (
<a className="courseware-search-results__item" {...linkProps}>
<div className="courseware-search-results__icon"><Icon src={icon} /></div>
<div className="courseware-search-results__info">
<div className="courseware-search-results__title">
<span>{title}</span>
{contentMatches ? (<em>{contentMatches}</em>) : null }
</div>
{breadcrumbs?.length ? (
<ul className="courseware-search-results__breadcrumbs">
{breadcrumbs.map(bc => (<li><div>{bc}</div></li>))}
</ul>
) : null}
</div>
</a>
);
})}
</div>
);
};

CoursewareSearchResults.propTypes = {
intl: intlShape.isRequired,
results: PropTypes.arrayOf(PropTypes.objectOf({
title: PropTypes.string.isRequired,
href: PropTypes.string.isRequired,
type: PropTypes.string,
breadcrumbs: PropTypes.arrayOf(PropTypes.string),
contentMatches: PropTypes.number,
isExternal: PropTypes.bool,
})),
};

CoursewareSearchResults.defaultProps = {
results: [],
};

export default injectIntl(CoursewareSearchResults);
38 changes: 38 additions & 0 deletions src/course-home/courseware-search/CoursewareSearchResults.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React from 'react';
import {
initializeMockApp,
render,
screen,
} from '../../setupTest';
import CoursewareSearchResults from './CoursewareSearchResults';
import messages from './messages';
import mockedData from './test-data/mockedResults';

jest.mock('react-redux');

function renderComponent({ results }) {
const { container } = render(<CoursewareSearchResults results={results} />);
return container;
}

describe('CoursewareSearchResults', () => {
beforeAll(async () => {
initializeMockApp();
});

describe('when an empty array is provided', () => {
beforeEach(() => { renderComponent({ results: [] }); });

it('should render a "no results found" message.', () => {
expect(screen.getByTestId('no-results').textContent).toBe(messages.searchResultsNone.defaultMessage);
});
});

describe('when list of results is provided', () => {
beforeEach(() => { renderComponent({ results: mockedData }); });

it('should match the snapshot', () => {
expect(screen).toMatchSnapshot();
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`CoursewareSearchResults when list of results is provided should match the snapshot 1`] = `
Object {
"debug": [Function],
"findAllByAltText": [Function],
"findAllByDisplayValue": [Function],
"findAllByLabelText": [Function],
"findAllByPlaceholderText": [Function],
"findAllByRole": [Function],
"findAllByTestId": [Function],
"findAllByText": [Function],
"findAllByTitle": [Function],
"findByAltText": [Function],
"findByDisplayValue": [Function],
"findByLabelText": [Function],
"findByPlaceholderText": [Function],
"findByRole": [Function],
"findByTestId": [Function],
"findByText": [Function],
"findByTitle": [Function],
"getAllByAltText": [Function],
"getAllByDisplayValue": [Function],
"getAllByLabelText": [Function],
"getAllByPlaceholderText": [Function],
"getAllByRole": [Function],
"getAllByTestId": [Function],
"getAllByText": [Function],
"getAllByTitle": [Function],
"getByAltText": [Function],
"getByDisplayValue": [Function],
"getByLabelText": [Function],
"getByPlaceholderText": [Function],
"getByRole": [Function],
"getByTestId": [Function],
"getByText": [Function],
"getByTitle": [Function],
"logTestingPlaygroundURL": [Function],
"queryAllByAltText": [Function],
"queryAllByDisplayValue": [Function],
"queryAllByLabelText": [Function],
"queryAllByPlaceholderText": [Function],
"queryAllByRole": [Function],
"queryAllByTestId": [Function],
"queryAllByText": [Function],
"queryAllByTitle": [Function],
"queryByAltText": [Function],
"queryByDisplayValue": [Function],
"queryByLabelText": [Function],
"queryByPlaceholderText": [Function],
"queryByRole": [Function],
"queryByTestId": [Function],
"queryByText": [Function],
"queryByTitle": [Function],
}
`;
91 changes: 91 additions & 0 deletions src/course-home/courseware-search/courseware-search.scss
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,97 @@
}
}

.courseware-search-results {
margin-top: 1.5rem;

&__item {
display: block;
padding: .75rem 1rem;
font-weight: 500;
display: flex;
gap: 0.625rem;

&:hover {
text-decoration: none;
background: $light-300;
}

&:not(:first-child) {
border-top: 1px solid $light-300;
}
}

&__icon {
padding: 0.375rem 0 0 0.375rem;
color: $gray-300;
}

&__info {
flex: 1;
overflow: hidden;
}

&__title {
display: flex;
align-items: center;
line-height: 2.5;
font-size: 0.875rem;
color: $black;

> span {
display: block;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}

em {
padding: 0.125rem 0.375rem;
font-variant-numeric: lining-nums tabular-nums;
min-width: 1.25rem;
line-height: 1rem;
background: $light-300;
border-radius: 99rem;
font-style: normal;
margin-left: 0.375rem;
font-size: 0.6875rem;
text-align: center;
}
}

&__breadcrumbs {
display: flex;
gap: 1.25rem;
color: $gray-400;
overflow: hidden;
list-style: none;
padding: 0;
margin: 0;

> li {
position: relative;
flex-shrink: 1;
min-width: 0;

&:not(:first-child)::before {
content: '';
position: absolute;
top: 50%;
transform: translate(-50%, -55%);
left: -0.625rem;
}
}

div {
font-size: 0.75rem;
line-height: 1;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
}
}

@media (min-width: map-get($grid-breakpoints, 'md')) {
.courseware-search__content {
padding-top: 8rem;
Expand Down
5 changes: 5 additions & 0 deletions src/course-home/courseware-search/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ const messages = defineMessages({
defaultMessage: 'Search',
description: 'Placeholder text for the Courseware Search input control',
},
searchResultsNone: {
id: 'learn.coursewareSerch.searchResultsNone',
defaultMessage: 'No results found.',
description: 'Text to show when the Courseware Search found no results matching the criteria.',
},
});

export default messages;
Loading

0 comments on commit 71cc670

Please sign in to comment.