Skip to content

Commit da33baa

Browse files
ddey2longshuicy
andauthored
Adding download button on UI to download correct verison file (#334)
* Adding download button on UI to download correct verison file * fix filename issue * Update FileVersion.ts --------- Co-authored-by: Chen Wang <[email protected]>
1 parent 542dfb5 commit da33baa

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

backend/app/routers/files.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,8 @@ async def download_file(
312312
):
313313
# If file exists in MongoDB, download from Minio
314314
if (file := await db["files"].find_one({"_id": ObjectId(file_id)})) is not None:
315+
file_obj = FileOut.from_mongo(file)
315316
if version is not None:
316-
file_obj = FileOut.from_mongo(file)
317317
# Version is specified, so get the minio ID from versions table if possible
318318
if (
319319
file_vers := await db["file_versions"].find_one(

frontend/src/actions/file.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,14 @@ export function fetchFileVersions(fileId){
182182
}
183183

184184
export const DOWNLOAD_FILE = "DOWNLOAD_FILE";
185-
export function fileDownloaded(fileId, filename = "") {
185+
export function fileDownloaded(fileId, filename = "", fileVersionNum = 0) {
186186
return async (dispatch) => {
187187
if (filename === "") {
188188
filename = `${fileId}.zip`;
189189
}
190-
const endpoint = `${config.hostname}/api/v2/files/${fileId}`;
190+
let endpoint = `${config.hostname}/api/v2/files/${fileId}`;
191+
if (fileVersionNum != 0)
192+
endpoint = endpoint + '?version=' + fileVersionNum;
191193
const response = await fetch(endpoint, {method: "GET", mode: "cors", headers: await getHeader()});
192194

193195
if (response.status === 200) {

frontend/src/components/files/File.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ export const File = (): JSX.Element => {
240240
{/*Version History*/}
241241
<TabPanel value={selectedTabIndex} index={0}>
242242
{fileVersions !== undefined ?
243-
<FileVersionHistory fileVersions={fileVersions}/> : <></>}
243+
<FileVersionHistory fileVersions={fileVersions} filename={fileSummary.name}/> : <></>}
244244
</TabPanel>
245245
<TabPanel value={selectedTabIndex} index={1}>
246246
{

frontend/src/components/versions/FileVersionHistory.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
import React from "react";
2-
import {Box, List, ListItem, ListItemAvatar, ListItemText} from "@mui/material";
2+
import {Box, Button, List, ListItem, ListItemAvatar, ListItemText} from "@mui/material";
33
import {VersionChip} from "./VersionChip";
44
import {parseDate} from "../../utils/common";
55
import {FileVersion} from "../../openapi/v2";
6+
import {fileDownloaded} from "../../actions/file";
7+
import {useDispatch} from "react-redux";
68

79
type FileVersionHistoryProps = {
8-
fileVersions: FileVersion[]
10+
fileVersions: FileVersion[],
11+
filename: string,
912
}
1013

1114
export function FileVersionHistory(props: FileVersionHistoryProps) {
12-
const {fileVersions} = props;
15+
const {fileVersions, filename} = props;
16+
const dispatch = useDispatch();
17+
const downloadFile = (fileId:string|undefined, filename:string|undefined, fileVersion:number|undefined) => dispatch(fileDownloaded(fileId, filename, fileVersion))
1318

1419
return (
1520
<Box className="infoCard">
@@ -28,8 +33,12 @@ export function FileVersionHistory(props: FileVersionHistoryProps) {
2833
`${creator.first_name} ${creator.last_name}` : ""}`}
2934
secondary={`Uploaded on ${parseDate(created)}`}
3035
/>
36+
<Button onClick={()=>{
37+
downloadFile(fileVersion.file_id, filename, version_num);
38+
}}>
39+
Download
40+
</Button>
3141
{/*TODO implement those actions*/}
32-
{/*<Button disabled>Download</Button>*/}
3342
{/*<Button disabled>Delete</Button>*/}
3443
{/*<Button disabled>Make Current</Button>*/}
3544
</ListItem>

0 commit comments

Comments
 (0)