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: send individuals emails with pluggable component #6

Closed
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
94 changes: 90 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"@fortawesome/react-fontawesome": "0.2.0",
"@loadable/component": "^5.15.3",
"@openedx-plugins/communications-app-check-box-form": "file:plugins/communications-app/CheckBoxForm",
"@openedx-plugins/communications-app-individual-emails": "file:plugins/communications-app/IndividualEmails",
"@openedx-plugins/communications-app-input-form": "file:plugins/communications-app/InputForm",
"@openedx-plugins/communications-app-test-component": "file:plugins/communications-app/TestComponent",
"@tinymce/tinymce-react": "3.14.0",
Expand All @@ -57,6 +58,7 @@
"popper.js": "1.16.1",
"prop-types": "15.8.1",
"react": "17.0.2",
"react-bootstrap-typeahead": "^6.3.2",
"react-dom": "17.0.2",
"react-helmet": "^6.1.0",
"react-redux": "7.2.9",
Expand All @@ -65,7 +67,8 @@
"redux": "4.2.0",
"regenerator-runtime": "0.13.11",
"tinymce": "5.10.7",
"use-deep-compare-effect": "^1.8.1"
"use-deep-compare-effect": "^1.8.1",
"uuid": "^9.0.1"
},
"devDependencies": {
"@edx/browserslist-config": "^1.2.0",
Expand Down
14 changes: 14 additions & 0 deletions plugins/communications-app/IndividualEmails/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { getConfig } from '@edx/frontend-platform';
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';
import { logError } from '@edx/frontend-platform/logging';

export async function getLearnersEmailInstructorTask(courseId, search) {
const endpointUrl = `${getConfig().LMS_BASE_URL}/platform-plugin-communications/${courseId}/api/search_learners?query=${search}&page=1&page_size=10`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is the page and page_size fixed? shouldn't it be parametrized?

try {
const response = await getAuthenticatedHttpClient().get(endpointUrl);
return response;
} catch (error) {
logError(error);
throw new Error(error);
}
}
44 changes: 44 additions & 0 deletions plugins/communications-app/IndividualEmails/api.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';
import { getConfig } from '@edx/frontend-platform';
import { logError } from '@edx/frontend-platform/logging';

import { getLearnersEmailInstructorTask } from './api';

jest.mock('@edx/frontend-platform/auth', () => ({
getAuthenticatedHttpClient: jest.fn(),
}));
jest.mock('@edx/frontend-platform', () => ({
getConfig: jest.fn(),
}));
jest.mock('@edx/frontend-platform/logging', () => ({
logError: jest.fn(),
}));

describe('getLearnersEmailInstructorTask', () => {
const mockCourseId = 'course123';
const mockSearch = 'testuser';
const mockResponseData = { data: 'someData' };
const mockConfig = { LMS_BASE_URL: 'http://localhost' };

beforeEach(() => {
getConfig.mockReturnValue(mockConfig);
getAuthenticatedHttpClient.mockReturnValue({
get: jest.fn().mockResolvedValue(mockResponseData),
});
});

it('successfully fetches data', async () => {
const data = await getLearnersEmailInstructorTask(mockCourseId, mockSearch);
expect(data).toEqual(mockResponseData);
expect(getAuthenticatedHttpClient().get).toHaveBeenCalledWith(
`http://localhost/platform-plugin-communications/${mockCourseId}/api/search_learners?query=${mockSearch}&page=1&page_size=10`,
);
});

it('handles an error', async () => {
getAuthenticatedHttpClient().get.mockRejectedValue(new Error('Network error'));

await expect(getLearnersEmailInstructorTask(mockCourseId, mockSearch)).rejects.toThrow('Network error');
expect(logError).toHaveBeenCalledWith(new Error('Network error'));
});
});
115 changes: 115 additions & 0 deletions plugins/communications-app/IndividualEmails/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import React, { useState } from 'react';
import PropTypes from 'prop-types';
import { AsyncTypeahead } from 'react-bootstrap-typeahead';
import { v4 as uuidv4 } from 'uuid';
import { Form, Chip, Container } from '@edx/paragon';
import { Person, Close } from '@edx/paragon/icons';
import { injectIntl, intlShape } from '@edx/frontend-platform/i18n';
import { getLearnersEmailInstructorTask } from './api';
import messages from './messages';

import './styles.scss';

function IndividualEmails(props) {
const {
courseId, handleEmailSelected, emailList, handleDeleteEmail,
intl,
} = props;
const [isLoading, setIsLoading] = useState(false);
const [options, setOptions] = useState([]);
const [inputValue] = useState([]);

const handleSearchEmailLearners = async (userEmail) => {
setIsLoading(true);
try {
const response = await getLearnersEmailInstructorTask(courseId, userEmail);
const { results } = response.data;
const formatResult = results.map((item) => ({ id: uuidv4(), ...item }));
setOptions(formatResult);
} catch (error) {
// eslint-disable-next-line no-console
console.error('error autocomplete input', error.message);
Comment on lines +30 to +31
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why a console error instead of a standard logError?

} finally {
setIsLoading(false);
}
};

const filterBy = (option) => option.name || option.email || option.username;
const handleDeleteEmailSelected = (id) => {
if (handleDeleteEmail) {
handleDeleteEmail(id);
}
};

const handleSelectedLearnerEmail = (selected) => {
const [itemSelected] = selected || [{ email: '' }];
const isEmailAdded = emailList.some((item) => item.email === itemSelected.email);

if (selected && !isEmailAdded) {
handleEmailSelected(selected);
}
};

return (
<Container className="col-12 my-5">
<Form.Label className="mt-3" data-testid="learners-email-input-label">{intl.formatMessage(messages.individualEmailsLabelLearnersInputLabel)}</Form.Label>
<AsyncTypeahead
filterBy={filterBy}
id="async-autocompleinput"
isLoading={isLoading}
labelKey="username"
minLength={3}
onSearch={handleSearchEmailLearners}
options={options}
name="studentEmail"
selected={inputValue}
data-testid="input-typeahead"
placeholder={intl.formatMessage(messages.individualEmailsLabelLearnersInputPlaceholder)}
onChange={handleSelectedLearnerEmail}
renderMenuItemChildren={({ name, email }) => (
<span data-testid="autocomplete-email-option">{name ? `${name} -` : name} {email}</span>
Comment on lines +69 to +70
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The search query can also be the username. Would the name/email combination appear even though I'm looking for the student by their username? wouldn't that be confusing?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add the username as well, no problem. For me is not confusing due to the user can see the name/email of the student in the list an selected that one

)}
/>
<Container className="email-list">
<Form.Label className="col-12" data-testid="learners-email-list-label">{intl.formatMessage(messages.individualEmailsLabelLearnersListLabel)}</Form.Label>
{emailList.map(({ id, email }) => (
<Chip
variant="light"
className="email-chip"
iconBefore={Person}
iconAfter={Close}
onIconAfterClick={() => handleDeleteEmailSelected(id)}
key={id}
data-testid="email-list-chip"
>
{email}
</Chip>
))}
</Container>

</Container>
);
}

IndividualEmails.defaultProps = {
courseId: '',
handleEmailSelected: () => {},
handleDeleteEmail: () => {},
emailList: [],
};

IndividualEmails.propTypes = {
courseId: PropTypes.string,
handleEmailSelected: PropTypes.func,
handleDeleteEmail: PropTypes.func,
emailList: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.string,
email: PropTypes.string,
username: PropTypes.string,
}),
),
intl: intlShape.isRequired,
};

export default injectIntl(IndividualEmails);
Loading
Loading