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

PADV-1762 add delete service #141

Merged
merged 2 commits into from
Dec 23, 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/assets/colors.scss
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ $blue-50: #a6dff7;
// Black
$black: #000;
$black-80: #333;
$black-30: #212529;
38 changes: 35 additions & 3 deletions src/features/Instructors/InstructorsDetailPage/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ import {
import { getConfig } from '@edx/frontend-platform';
import { CalendarExpanded } from 'react-paragon-topaz';
import { startOfMonth, endOfMonth } from 'date-fns';
import { logError } from '@edx/frontend-platform/logging';

import Table from 'features/Main/Table';
import { fetchClassesData } from 'features/Classes/data/thunks';
import { resetClassesTable, updateCurrentPage } from 'features/Classes/data/slice';
import { fetchInstructorsData, fetchEventsData, resetEvents } from 'features/Instructors/data';
import { columns } from 'features/Instructors/InstructorDetailTable/columns';
import { initialPage, RequestStatus } from 'features/constants';
import { deleteEvent } from 'features/Instructors/data/api';
import { updateEvents } from 'features/Instructors/data/slice';

import { useInstitutionIdQueryParam } from 'hooks';

Expand Down Expand Up @@ -72,6 +75,32 @@ const InstructorsDetailPage = () => {
dispatch(updateCurrentPage(targetPage));
};

const handleDeleteEvent = async (event) => {
try {
const params = event?.recurrence ? {
delete_occurrence: true,
start_occurrence: event?.start,
} : {};

await deleteEvent(event.id, params);

const newEventsState = events.filter(currentEvent => currentEvent.elementId !== event.elementId);
dispatch(updateEvents(newEventsState));
} catch (error) {
logError(error);
}
};

const handleDeleteMultipleEvents = async (event) => {
try {
await deleteEvent(event.id);
const newEventsState = events.filter(currentEvent => currentEvent.id !== event.id);
dispatch(updateEvents(newEventsState));
} catch (error) {
logError(error);
}
};

useEffect(() => {
if (institution.id) {
dispatch(fetchInstructorsData(institution.id, initialPage, { instructor: instructorUsername }));
Expand All @@ -90,7 +119,10 @@ const InstructorsDetailPage = () => {

useEffect(() => {
if (instructorInfo.instructorId && showInstructorCalendar) {
dispatch(fetchEventsData({ ...rangeDates, instructor_id: instructorInfo.instructorId }));
dispatch(fetchEventsData({
...rangeDates,
instructor_id: instructorInfo.instructorId,
}));
}

return () => {
Expand Down Expand Up @@ -160,8 +192,8 @@ const InstructorsDetailPage = () => {
eventsList={eventsList}
onRangeChange={getRangeDate}
onEdit={() => {}}
onDelete={() => {}}
onDeleteMultiple={() => {}}
onDelete={handleDeleteEvent}
onDeleteMultiple={handleDeleteMultipleEvents}
onEditSinglRec={() => {}}
/>
</div>
Expand Down
5 changes: 5 additions & 0 deletions src/features/Instructors/InstructorsDetailPage/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@

.container-calendar {
box-shadow: 0px 3px 12px 0px $gray-30;

.pgn__dropdown-item {
text-decoration: none;
color: $black-30;
}
}
22 changes: 22 additions & 0 deletions src/features/Instructors/data/__test__/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
handleInstructorsEnrollment,
handleNewInstructor,
getEventsByInstructor,
deleteEvent,
} from 'features/Instructors/data/api';

jest.mock('@edx/frontend-platform/auth', () => ({
Expand Down Expand Up @@ -100,4 +101,25 @@ describe('should call getAuthenticatedHttpClient with the correct parameters', (
},
);
});

test('delete event', () => {
const httpClientMock = {
delete: jest.fn(),
};

const eventId = 1;

getAuthenticatedHttpClient.mockReturnValue(httpClientMock);

deleteEvent(eventId);

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

expect(httpClientMock.delete).toHaveBeenCalledTimes(1);
expect(httpClientMock.delete).toHaveBeenCalledWith(
'http://localhost:18000/pearson_course_operation/api/v2/events/',
{ params: { event_id: eventId } },
);
});
});
18 changes: 18 additions & 0 deletions src/features/Instructors/data/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,26 @@ function getEventsByInstructor(params) {
);
}

/**
* Delete event.
*
* @param {number} - Event id to be deleted
* @returns {Promise} - A promise that resolves with the response of the DELETE request.
*/
function deleteEvent(eventId, options = {}) {
const params = {
event_id: eventId,
...options,
};
return getAuthenticatedHttpClient().delete(
`${getConfig().COURSE_OPERATIONS_API_V2_BASE_URL}/events/`,
{ params },
);
}

export {
handleInstructorsEnrollment,
handleNewInstructor,
getEventsByInstructor,
deleteEvent,
};
Loading