Skip to content

Commit

Permalink
feat: call service for add instructor
Browse files Browse the repository at this point in the history
  • Loading branch information
AuraAlba committed Oct 20, 2023
1 parent f105cbe commit 7aa6c89
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/features/Instructors/AddInstructors/_test_/index.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,11 @@ describe('Add instructor component', () => {
const ccxSelect = getByText('Select Class Name');
expect(instructorInfoInput).toBeInTheDocument();
expect(ccxSelect).toBeInTheDocument();

fireEvent.change(instructorInfoInput, { target: { value: 'Name' } });
const submitButton = getByText('Add');
await act(async () => {
fireEvent.click(submitButton);
});
});
});
48 changes: 44 additions & 4 deletions src/features/Instructors/AddInstructors/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useState, useReducer, useEffect } from 'react';
import {
Button, FormGroup, ModalDialog, useToggle, Form,
} from '@edx/paragon';
import { getCCXList } from 'features/Instructors/data/api';
import { getCCXList, handleInstructorsEnrollment } from 'features/Instructors/data/api';
import reducer from 'features/Instructors/AddInstructors/reducer';
import { logError } from '@edx/frontend-platform/logging';
import { camelCaseObject } from '@edx/frontend-platform';
Expand All @@ -26,6 +26,8 @@ const AddInstructors = () => {
const [state, dispatch] = useReducer(reducer, initialState);
const [isOpen, open, close] = useToggle(false);
const [addInstructorInfo, setAddInstructorInfo] = useState(addInstructorState);
const [isNoUser, setIsNoUser] = useState(false);
const enrollmentData = new FormData();

const fetchData = async () => {
dispatch({ type: FETCH_CCX_LIST_REQUEST });
Expand All @@ -46,6 +48,17 @@ const AddInstructors = () => {
});
};

// Set default value
useEffect(() => {
if (state.data.length > 0) {
setAddInstructorInfo((prevState) => ({
...prevState,
ccxId: state.data[0].classId,
ccxName: state.data[0].className,
}));
}
}, [state.data]);

const handleCcxSelect = (e) => {
const select = e.target;
setAddInstructorInfo({
Expand All @@ -55,6 +68,24 @@ const AddInstructors = () => {
});
};

const handleAddInstructors = async () => {
try {
enrollmentData.append('unique_student_identifier', addInstructorInfo.instructorInfo);
enrollmentData.append('rolename', 'staff');
enrollmentData.append('action', 'allow');
const response = await handleInstructorsEnrollment(enrollmentData, addInstructorInfo.ccxId);
if (response.data?.userDoesNotExist) {
setIsNoUser(true);
} else {
fetchData();
close();
setIsNoUser(false);
}
} catch (error) {
logError(error);
}
};

useEffect(() => {
fetchData();
}, []);
Expand Down Expand Up @@ -83,9 +114,12 @@ const AddInstructors = () => {
className="my-4 mr-0"
onChange={handleCcxSelect}
id="selectCcx"
value={addInstructorInfo.ccxId}
>
{state.data.map((ccx) => <option value={ccx.classId}>{ccx.className}</option>)}
</Form.Control>
</FormGroup>
<FormGroup>
<Form.Control
type="text"
placeholder="Enter Username or Email of the instructor"
Expand All @@ -94,10 +128,16 @@ const AddInstructors = () => {
onChange={handleInstructorInput}
className="my-4 mr-0"
/>
<div className="d-flex justify-content-end">
<Button>Add</Button>
</div>
{isNoUser && (
<Form.Control.Feedback type="invalid">
User does not exist
</Form.Control.Feedback>
)}
</FormGroup>
<div className="d-flex justify-content-end">
<Button onClick={handleAddInstructors}>Add</Button>
</div>

</ModalDialog.Body>
</ModalDialog>
</>
Expand Down
26 changes: 25 additions & 1 deletion src/features/Instructors/data/_test_/api.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';
import { getInstructorData } from 'features/Instructors/data/api';
import { getInstructorData, handleInstructorsEnrollment } from 'features/Instructors/data/api';

jest.mock('@edx/frontend-platform/auth', () => ({
getAuthenticatedHttpClient: jest.fn(),
Expand Down Expand Up @@ -34,3 +34,27 @@ describe('getInstructorData', () => {
);
});
});

describe('handleInstructorsEnrollment', () => {
test('should call getAuthenticatedHttpClient with the correct parameters', () => {
const httpClientMock = {
post: jest.fn(),
};

const courseId = 'course123';
const data = new FormData();

getAuthenticatedHttpClient.mockReturnValue(httpClientMock);

handleInstructorsEnrollment(data, courseId);

expect(getAuthenticatedHttpClient).toHaveBeenCalledTimes(2);
expect(getAuthenticatedHttpClient).toHaveBeenCalledWith();

expect(httpClientMock.post).toHaveBeenCalledTimes(1);
expect(httpClientMock.post).toHaveBeenCalledWith(
'http://localhost:18000/courses/course123/instructor/api/modify_access',
data,
);
});
});
13 changes: 12 additions & 1 deletion src/features/Instructors/data/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,22 @@ function getInstructorData(page, filters) {
function getCCXList() {
const apiV2BaseUrl = getConfig().COURSE_OPERATIONS_API_V2_BASE_URL;
return getAuthenticatedHttpClient().get(
`${apiV2BaseUrl}/classes?limit=false`,
`${apiV2BaseUrl}/classes/?limit=false`,
);
}

function handleInstructorsEnrollment(data, courseId) {
const INSTRUCTOR_API_URL = `${getConfig().LMS_BASE_URL}/courses/course_id/instructor/api`;
const courseIdSearchPattern = /course_id/;

return getAuthenticatedHttpClient().post(
`${INSTRUCTOR_API_URL.replace(courseIdSearchPattern, courseId)}/modify_access`,
data,
);
}

export {
getInstructorData,
getCCXList,
handleInstructorsEnrollment,
};

0 comments on commit 7aa6c89

Please sign in to comment.