Skip to content

Commit

Permalink
feat: update usage location call (#764)
Browse files Browse the repository at this point in the history
  • Loading branch information
KristinAoki authored Jan 9, 2024
1 parent cae7f9b commit bee3758
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
import React from 'react';
import { PropTypes } from 'prop-types';
import { Icon } from '@edx/paragon';
import { isNil } from 'lodash';
import { injectIntl, FormattedMessage } from '@edx/frontend-platform/i18n';
import { Icon, Spinner } from '@edx/paragon';
import { Check } from '@edx/paragon/icons';

const ActiveColumn = ({ row }) => {
const { usageLocations } = row.original;
const numOfUsageLocations = usageLocations?.length;
if (isNil(usageLocations)) {
return (
<Spinner
animation="border"
role="status"
variant="primary"
size="sm"
screenReaderText={(
<FormattedMessage
id="authoring.loading"
defaultMessage="Loading..."
description="Screen-reader message for when a page is loading."
/>
)}
/>
);
}
const numOfUsageLocations = usageLocations.length;
return numOfUsageLocations > 0 ? <Icon src={Check} /> : null;
};

Expand All @@ -17,4 +36,4 @@ ActiveColumn.propTypes = {
}.isRequired,
};

export default ActiveColumn;
export default injectIntl(ActiveColumn);
2 changes: 1 addition & 1 deletion src/files-and-videos/videos-page/VideosPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const VideosPage = ({

const supportedFileFormats = { 'video/*': videoSupportedFileFormats || FILES_AND_UPLOAD_TYPE_FILTERS.video };

const handleAddFile = (file) => dispatch(addVideoFile(courseId, file));
const handleAddFile = (file) => dispatch(addVideoFile(courseId, file, videoIds));
const handleDeleteFile = (id) => dispatch(deleteVideoFile(courseId, id));
const handleDownloadFile = (selectedRows) => dispatch(fetchVideoDownload({ selectedRows, courseId }));
const handleUsagePaths = (video) => dispatch(getUsagePaths({ video, courseId }));
Expand Down
23 changes: 19 additions & 4 deletions src/files-and-videos/videos-page/VideosPage.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@ const mockStore = async (
status,
) => {
const fetchVideosUrl = getVideosUrl(courseId);
axiosMock.onGet(fetchVideosUrl).reply(getStatusValue(status), generateFetchVideosApiResponse());
const videosData = generateFetchVideosApiResponse();
axiosMock.onGet(fetchVideosUrl).reply(getStatusValue(status), videosData);

videosData.previous_uploads.forEach((video) => {
axiosMock.onGet(`${getVideosUrl(courseId)}/${video.edx_video_id}/usage`).reply(200, { usageLocations: [] });
});

renderComponent();
await executeThunk(fetchVideos(courseId), store.dispatch);
};
Expand All @@ -71,7 +77,7 @@ const emptyMockStore = async (status) => {
await executeThunk(fetchVideos(courseId), store.dispatch);
};

describe('FilesAndUploads', () => {
describe('Videos page', () => {
describe('empty state', () => {
beforeEach(async () => {
initializeMockApp({
Expand Down Expand Up @@ -179,6 +185,13 @@ describe('FilesAndUploads', () => {

it('should switch table to list view', async () => {
await mockStore(RequestStatus.SUCCESSFUL);
axiosMock.onGet(`${getVideosUrl(courseId)}/mOckID1/usage`)
.reply(201, {
usageLocations: [{
display_location: 'subsection - unit / block',
url: 'base/unit_id#block_id',
}],
});
expect(screen.getByTestId('files-data-table')).toBeVisible();

expect(screen.getByTestId('grid-card-mOckID1')).toBeVisible();
Expand Down Expand Up @@ -219,9 +232,10 @@ describe('FilesAndUploads', () => {
axiosMock.onGet(getCourseVideosApiUrl(courseId)).reply(200, generateAddVideoApiResponse());

const addFilesButton = screen.getAllByLabelText('file-input')[3];
const { videoIds } = store.getState().videos;
await act(async () => {
userEvent.upload(addFilesButton, file);
await executeThunk(addVideoFile(courseId, file), store.dispatch);
await executeThunk(addVideoFile(courseId, file, videoIds), store.dispatch);
});
const addStatus = store.getState().videos.addingStatus;
expect(addStatus).toEqual(RequestStatus.SUCCESSFUL);
Expand Down Expand Up @@ -419,12 +433,13 @@ describe('FilesAndUploads', () => {
const videoMenuButton = screen.getByTestId('file-menu-dropdown-mOckID1');

axiosMock.onGet(`${getVideosUrl(courseId)}/mOckID1/usage`)
.reply(201, {
.reply(200, {
usageLocations: [{
display_location: 'subsection - unit / block',
url: 'base/unit_id#block_id',
}],
});

await waitFor(() => {
fireEvent.click(within(videoMenuButton).getByLabelText('file-menu-toggle'));
fireEvent.click(screen.getByText('Info'));
Expand Down
1 change: 1 addition & 0 deletions src/files-and-videos/videos-page/data/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ export async function uploadVideo(
const formData = new FormData();
formData.append('uploaded-file', uploadFile);
const uploadErrors = [];

await fetch(uploadUrl, {
method: 'PUT',
body: formData,
Expand Down
25 changes: 21 additions & 4 deletions src/files-and-videos/videos-page/data/thunks.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
addModels,
removeModel,
updateModel,
updateModels,
} from '../../../generic/model-store';
import {
addThumbnail,
Expand Down Expand Up @@ -38,6 +37,20 @@ import {

import { updateFileValues } from './utils';

async function fetchUsageLocation(videoId, dispatch, courseId) {
const { usageLocations } = await getVideoUsagePaths({ videoId, courseId });
const activeStatus = usageLocations?.length > 0 ? 'active' : 'inactive';

dispatch(updateModel({
modelType: 'videos',
model: {
id: videoId,
usageLocations,
activeStatus,
},
}));
}

export function fetchVideos(courseId) {
return async (dispatch) => {
dispatch(updateLoadingStatus({ courseId, status: RequestStatus.IN_PROGRESS }));
Expand All @@ -51,6 +64,9 @@ export function fetchVideos(courseId) {
}));
dispatch(setPageSettings({ ...data }));
dispatch(updateLoadingStatus({ courseId, status: RequestStatus.SUCCESSFUL }));
parsedVideos.forEach(async (video) => {
fetchUsageLocation(video.id, dispatch, courseId);
});
} catch (error) {
if (error.response && error.response.status === 403) {
dispatch(updateLoadingStatus({ status: RequestStatus.DENIED }));
Expand Down Expand Up @@ -90,7 +106,7 @@ export function deleteVideoFile(courseId, id) {
};
}

export function addVideoFile(courseId, file) {
export function addVideoFile(courseId, file, videoIds) {
return async (dispatch) => {
dispatch(updateEditStatus({ editType: 'add', status: RequestStatus.IN_PROGRESS }));

Expand All @@ -104,8 +120,9 @@ export function addVideoFile(courseId, file) {
edxVideoId,
);
const { videos } = await fetchVideoList(courseId);
const parsedVideos = updateFileValues(videos);
dispatch(updateModels({
const newVideos = videos.filter(video => !videoIds.includes(video.edxVideoId));
const parsedVideos = updateFileValues(newVideos, true);
dispatch(addModels({
modelType: 'videos',
models: parsedVideos,
}));
Expand Down
7 changes: 3 additions & 4 deletions src/files-and-videos/videos-page/data/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ ensureConfig([
'STUDIO_BASE_URL',
], 'Course Apps API service');

export const updateFileValues = (files) => {
export const updateFileValues = (files, isNewFile) => {
const updatedFiles = [];
files.forEach(file => {
const {
Expand All @@ -25,7 +25,6 @@ export const updateFileValues = (files) => {
courseVideoImageUrl,
status,
transcripts,
usageLocations,
} = file;
const wrapperType = 'video';

Expand All @@ -34,7 +33,6 @@ export const updateFileValues = (files) => {
thumbnail = `${getConfig().STUDIO_BASE_URL}${thumbnail}`;
}
const transcriptStatus = transcripts?.length > 0 ? 'transcribed' : 'notTranscribed';
const activeStatus = usageLocations?.length > 0 ? 'active' : 'inactive';

let uploadStatus = status;
if (VIDEO_SUCCESS_STATUSES.includes(status)) {
Expand All @@ -49,10 +47,11 @@ export const updateFileValues = (files) => {
id: edxVideoId,
wrapperType,
dateAdded: created.toString(),
usageLocations: isNewFile ? [] : null,
status: uploadStatus,
thumbnail,
transcriptStatus,
activeStatus,
activeStatus: 'inactive',
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ describe('<PacingSection />', () => {
});

it('shows disabled radio inputs correctly', () => {
const pastDate = '2024-12-31';
const year = new Date().getFullYear() + 1;
const pastDate = `${year}-12-31`;
const initialProps = { ...props, startDate: pastDate };
const { getAllByRole, queryAllByText } = render(
<RootWrapper {...initialProps} />,
Expand Down

0 comments on commit bee3758

Please sign in to comment.