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: add transformer to files downloaded event #35

Merged
merged 13 commits into from
Jan 5, 2024
2 changes: 1 addition & 1 deletion filesmanager/static/html/bundle.js

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion react-app/components/FileManager/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import ErrorMessage from '@components/ErrorMessage';
import { sendTrackingLogEvent } from '@services/analyticsService';

import { useCustomFileMap, useFiles, useFolderChain, useFileActionHandler } from './hooks';
import { convertFileMapToTree } from './utils';
import { convertFileMapToTree, getMetadataFiles } from './utils';
import { prepareCustomFileMap, defaultFileActions, customFileActions, openFileAction } from './constants';

ChonkyActions.ToggleHiddenFiles.button.toolbar = false;
Expand All @@ -34,8 +34,11 @@ const FileManager = (props) => {
const [, setIsFetchLoading] = useState(false);
const [downloadFileErrorMessage, setDownloadFileErrorMessage] = useState(null);
const [reloadPage, setReloadPage] = useState(false);
const downloadFilesData = useRef(null);

const onFileDownloaded = () => {
const contents = downloadFilesData.current;
const objectData = getMetadataFiles(contents);
setDownloadFileErrorMessage(null);
const { isStudioView } = xBlockContext;
if(!isStudioView) {
Expand All @@ -44,6 +47,7 @@ const FileManager = (props) => {
xblock_id: xBlockContext.context.xblock_id,
user_id: xBlockContext.context.user_id,
username: xBlockContext.context.username,
files_downloaded_metadata: objectData,
created_at: new Date().toISOString(),
});
}
Expand All @@ -70,6 +74,7 @@ const FileManager = (props) => {
isDir,
} = fileData;
const { hostname, port, protocol } = window.location;
downloadFilesData.current = fileData;
const fullUrl = port ? `${protocol}//${hostname}:${port}${url}` : `${protocol}//${hostname}${url}`;
if (isDir){
downloadFiles([fileData])
Expand Down
23 changes: 23 additions & 0 deletions react-app/components/FileManager/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const convertTreeToNewFileMap = (node, parent = null, newFileMapObject, isSaved
id: node.id,
name: node.name,
isDir: isDirectory,
path: node.path || '',
metadata: node.metadata || {},
isSaved
};
Expand Down Expand Up @@ -177,3 +178,25 @@ export const getFilesFromATree = (node) => {

return files;
};

export const getMetadataFiles = (node) => {
const assetKeys = [];

const { name, path, metadata } = node;
if (metadata && metadata.asset_key) {
assetKeys.push({
name: name || '',
path: path || '',
asset_key: metadata.asset_key || '',
url: metadata.external_url || ''
});
}

if (node.children && node.children.length) {
node.children.forEach((child) => {
assetKeys.push(...getMetadataFiles(child));
});
}

return assetKeys;
};
Loading