-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #924 from betagouv/feat/file-epxlorer
Browsable file table
- Loading branch information
Showing
19 changed files
with
421 additions
and
324 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { css } from "@emotion/react"; | ||
import { useContext } from "react"; | ||
import { FileContext } from "./FileContext"; | ||
import { fileTableRowActionCellBtnStyle } from "./style"; | ||
|
||
interface BaseTableActionCellProps { | ||
onOpen: (name: string) => void; | ||
} | ||
|
||
const cellStyle = css({ | ||
width: "150px", | ||
}); | ||
|
||
export default function BaseTableActionCell({ | ||
onOpen, | ||
}: BaseTableActionCellProps) { | ||
const t = { | ||
Open: window.gettext("Open"), | ||
}; | ||
const file = useContext(FileContext); | ||
|
||
return ( | ||
<td css={cellStyle}> | ||
{file && ( | ||
<ul className="fr-btns-group fr-btns-group--inline fr-btns-group--sm"> | ||
<li> | ||
<button | ||
className="fr-btn fr-icon-arrow-right-line fr-btn--secondary" | ||
css={fileTableRowActionCellBtnStyle} | ||
onClick={() => onOpen(file.name)} | ||
> | ||
{t["Open"]} | ||
</button> | ||
</li> | ||
</ul> | ||
)} | ||
</td> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { displayMessage } from "../utils"; | ||
import { FileService } from "../file-service"; | ||
import { css } from "@emotion/react"; | ||
import { useContext } from "react"; | ||
import { FileContext } from "./FileContext"; | ||
import { fileTableRowActionCellBtnStyle } from "./style"; | ||
|
||
interface BaseFileActionCellProps { | ||
fileService: FileService; | ||
canDelete: boolean; | ||
onDeleteSuccess?: (fileName: string) => void; | ||
setIsLoading?: (isLoading: boolean) => void; | ||
} | ||
|
||
const cellStyle = css({ | ||
width: "150px", | ||
}); | ||
|
||
export default function BaseFileActionCell({ | ||
fileService, | ||
canDelete, | ||
onDeleteSuccess, | ||
setIsLoading, | ||
}: BaseFileActionCellProps) { | ||
const t = { | ||
"Delete the document %s ?": window.gettext("Delete the document %s ?"), | ||
"File %s could not be removed.": window.gettext( | ||
"File %s could not be removed.", | ||
), | ||
"File %s has been removed.": window.gettext("File %s has been removed."), | ||
"Download file": window.gettext("Download file"), | ||
"Delete file": window.gettext("Delete file"), | ||
}; | ||
const file = useContext(FileContext); | ||
|
||
const downloadFile = async (path: string) => { | ||
const url = await fileService.fetchPresignedURL(path); | ||
window.open(url, "_blank"); | ||
}; | ||
|
||
const deleteFile = async (name: string, path: string) => { | ||
if ( | ||
!window.confirm(window.interpolate(t["Delete the document %s ?"], [name])) | ||
) { | ||
return; | ||
} | ||
setIsLoading && setIsLoading(true); | ||
try { | ||
await fileService.deleteFile(path); | ||
} catch (error) { | ||
displayMessage( | ||
window.interpolate(t["File %s could not be removed."], [name]), | ||
"error", | ||
); | ||
setIsLoading && setIsLoading(false); | ||
} | ||
if (onDeleteSuccess) onDeleteSuccess(name); | ||
setIsLoading && setIsLoading(false); | ||
displayMessage( | ||
window.interpolate(t["File %s has been removed."], [name]), | ||
"success", | ||
); | ||
}; | ||
|
||
return ( | ||
<td css={cellStyle}> | ||
{file && ( | ||
<ul className="fr-btns-group fr-btns-group--inline fr-btns-group--sm"> | ||
<li> | ||
<button | ||
className="fr-btn fr-icon-download-line fr-btn--secondary" | ||
css={fileTableRowActionCellBtnStyle} | ||
onClick={() => downloadFile(file.path)} | ||
> | ||
{t["Download file"]} | ||
</button> | ||
</li> | ||
{canDelete && ( | ||
<li> | ||
<button | ||
className="fr-btn fr-icon-delete-line fr-btn--secondary" | ||
css={fileTableRowActionCellBtnStyle} | ||
onClick={() => deleteFile(file.name, file.path)} | ||
> | ||
{t["Delete file"]} | ||
</button> | ||
</li> | ||
)} | ||
</ul> | ||
)} | ||
</td> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,38 @@ | ||
import { displayMessage } from "../utils"; | ||
import { FileService } from "../file-service"; | ||
import { css } from "@emotion/react"; | ||
import { useContext } from "react"; | ||
import { FileContext } from "./FileContext"; | ||
import BaseFileActionCell from "./BaseFileActionCell"; | ||
import BaseDirectoryActionCell from "./BaseDirectoryActionCell"; | ||
|
||
interface BaseTableActionCellProps { | ||
fileService: FileService; | ||
canDelete: boolean; | ||
onDeleteSuccess?: (fileName: string) => void; | ||
setIsLoading?: (isLoading: boolean) => void; | ||
onFolderOpen: (name: string) => void; | ||
} | ||
|
||
const cellStyle = css({ | ||
width: "150px", | ||
}); | ||
|
||
export default function BaseTableActionCell({ | ||
fileService, | ||
canDelete, | ||
onDeleteSuccess, | ||
setIsLoading, | ||
onFolderOpen, | ||
}: BaseTableActionCellProps) { | ||
const t = { | ||
"Delete the document %s ?": window.gettext("Delete the document %s ?"), | ||
"File %s could not be removed.": window.gettext( | ||
"File %s could not be removed.", | ||
), | ||
"File %s has been removed.": window.gettext("File %s has been removed."), | ||
"Download file": window.gettext("Download file"), | ||
"Delete file": window.gettext("Delete file"), | ||
}; | ||
const file = useContext(FileContext); | ||
|
||
const downloadFile = async (path: string) => { | ||
const url = await fileService.fetchPresignedURL(path); | ||
window.open(url, "_blank"); | ||
}; | ||
|
||
const deleteFile = async (name: string, path: string) => { | ||
if ( | ||
!window.confirm(window.interpolate(t["Delete the document %s ?"], [name])) | ||
) { | ||
return; | ||
} | ||
setIsLoading && setIsLoading(true); | ||
try { | ||
await fileService.deleteFile(path); | ||
} catch (error) { | ||
displayMessage( | ||
window.interpolate(t["File %s could not be removed."], [name]), | ||
"error", | ||
); | ||
setIsLoading && setIsLoading(false); | ||
} | ||
if (onDeleteSuccess) onDeleteSuccess(name); | ||
setIsLoading && setIsLoading(false); | ||
displayMessage( | ||
window.interpolate(t["File %s has been removed."], [name]), | ||
"success", | ||
); | ||
}; | ||
|
||
return ( | ||
<td css={cellStyle}> | ||
{file && ( | ||
<ul className="fr-btns-group fr-btns-group--inline fr-btns-group--sm"> | ||
<li> | ||
<button | ||
className="download-btn fr-btn fr-icon-download-line fr-btn--secondary" | ||
onClick={() => downloadFile(file.path)} | ||
> | ||
{t["Download file"]} | ||
</button> | ||
</li> | ||
{canDelete && ( | ||
<li> | ||
<button | ||
className="delete-btn fr-btn fr-icon-delete-line fr-btn--secondary" | ||
onClick={() => deleteFile(file.name, file.path)} | ||
> | ||
{t["Delete file"]} | ||
</button> | ||
</li> | ||
)} | ||
</ul> | ||
)} | ||
</td> | ||
<> | ||
{file && | ||
(file.isDir ? ( | ||
<BaseDirectoryActionCell onOpen={onFolderOpen} /> | ||
) : ( | ||
<BaseFileActionCell | ||
fileService={fileService} | ||
canDelete={canDelete} | ||
onDeleteSuccess={onDeleteSuccess} | ||
setIsLoading={setIsLoading} | ||
/> | ||
))} | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.