Skip to content

Commit

Permalink
refactor: geenric download popup to support feasibility reports (#1062)
Browse files Browse the repository at this point in the history
* fix: typo

* refactor: generic download popup

---------

Co-authored-by: Hicham <hicham.taroq-ext@aphp.fr>
  • Loading branch information
thicham43 and Hicham authored Oct 2, 2024
1 parent 5505461 commit dd92d63
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ const ControlPanel: React.FC<{
<>Créer la cohorte</>
)}
</Button>
{appConfig.features.feasabilityReport.enabled && (
{appConfig.features.feasibilityReport.enabled && (
<Button
disabled={isLoading || typeof onExecute !== 'function' || maintenanceIsActive || count_outdated}
onClick={handleGenerateReport}
Expand Down
8 changes: 4 additions & 4 deletions src/components/Routes/AppNavigation/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import PageNotFound from 'views/PageNotFound/PageNotFound'
import CareSiteView from 'views/Scope/CareSiteView'
import MyCohorts from 'views/MyCohorts'
import MyRequests from 'views/MyRequests'
import ExportDownload from 'views/ExportDownload/ExportDownload'
import DownloadPopup from 'views/DownloadPopup/DownloadPopup'

// import { ODD_CONTACT } from '../../../constants'

Expand Down Expand Up @@ -218,10 +218,10 @@ const configRoutes: configRoute[] = [
exact: false
},
{
path: '/exports/:exportId/download',
name: '/exports/:exportId/download',
path: '/download/:resource/:itemId',
name: '/download/:resource/:itemId',
isPrivate: true,
element: <ExportDownload />,
element: <DownloadPopup />,
exact: false,
displaySideBar: false
},
Expand Down
4 changes: 2 additions & 2 deletions src/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ type AppConfig = {
locationCount?: string
}
}
feasabilityReport: {
feasibilityReport: {
enabled: boolean
}
contact: {
Expand Down Expand Up @@ -280,7 +280,7 @@ let config: AppConfig = {
locationCount: 'https://terminology.eds.aphp.fr/fhir/profile/location/extension/count'
}
},
feasabilityReport: {
feasibilityReport: {
enabled: false
},
contact: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,32 @@ import useStyles from './styles'
import apiBackend from '../../services/apiBackend'
import { useNavigate, useParams } from 'react-router-dom'

const ExportDownload: React.FC = () => {
const DownloadPopup: React.FC = () => {
const navigate = useNavigate()
const { exportId } = useParams<{ exportId?: string }>()
const { resource } = useParams<{ resource?: 'exports' | 'feasibility-studies' }>()
const { itemId } = useParams<{ itemId?: string }>()
const { classes } = useStyles()
const [open, setOpen] = useState<boolean>(true)
const [downloading, setDownloading] = useState<boolean | null>(false)

const extractFilename = (contentDisposition: string): string => {
const filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/
const matches = filenameRegex.exec(contentDisposition)
let filename = 'Export.zip'
let default_filename = 'Download.zip'
if (matches != null && matches[1]) {
filename = matches[1].replace(/['"]/g, '')
default_filename = matches[1].replace(/['"]/g, '')
}
return filename
return default_filename
}

const downloadExport = async (exportId: string) => {
const downloadItem = async (itemId: string) => {
try {
setDownloading(true)
const downloadResponse = await apiBackend.get(`/exports/${exportId}/download/`, {
let path = `/${resource}/${itemId}/download/`
if (resource === 'feasibility-studies') {
path = `/cohort/${resource}/${itemId}/download/`
}
const downloadResponse = await apiBackend.get(path, {
responseType: 'blob',
onDownloadProgress: (progressEvent) => {
if (progressEvent.progress === 1) setDownloading(null)
Expand Down Expand Up @@ -66,8 +71,8 @@ const ExportDownload: React.FC = () => {
}

useEffect(() => {
if (exportId) downloadExport(exportId)
}, [exportId])
if (itemId) downloadItem(itemId)
}, [itemId])

const _quit = () => {
setOpen(false)
Expand Down Expand Up @@ -121,4 +126,4 @@ const ExportDownload: React.FC = () => {
)
}

export default ExportDownload
export default DownloadPopup
File renamed without changes.

0 comments on commit dd92d63

Please sign in to comment.