11import { useEffect } from "react" ;
2-
3- import { Divider , Skeleton } from "antd" ;
2+ import { Divider , Skeleton , Button } from "antd" ;
3+ import { DownloadOutlined } from "@ant-design/icons" ;
4+ import { saveAs } from "file-saver" ;
45
56import { fetchIndividualPhenopacketsIfNecessary } from "@/modules/metadata/actions" ;
67import { individualPropTypesShape } from "@/propTypes" ;
78
8- import DownloadButton from "@/components/common/DownloadButton" ;
99import JsonView from "@/components/common/JsonView" ;
10- import { useService } from "@/modules/services/hooks" ;
1110import { useAppDispatch , useAppSelector } from "@/store" ;
1211
1312const IndividualPhenopackets = ( { individual } ) => {
1413 const dispatch = useAppDispatch ( ) ;
15-
1614 const { id : individualId } = individual ;
1715
18- const katsuUrl = useService ( "metadata" ) ?. url ?? "" ;
19- const downloadUrl = `${ katsuUrl } /api/individuals/${ individualId } /phenopackets?attachment=1&format=json` ;
20-
2116 const { phenopacketsByIndividualID } = useAppSelector ( ( state ) => state . individuals ) ;
2217
2318 const { isFetching, data } = phenopacketsByIndividualID [ individualId ] ?? { } ;
@@ -26,12 +21,24 @@ const IndividualPhenopackets = ({ individual }) => {
2621 dispatch ( fetchIndividualPhenopacketsIfNecessary ( individualId ) ) ;
2722 } , [ dispatch , individualId ] ) ;
2823
24+ const handleDownloadFromState = ( ) => {
25+ if ( ! data ) return ;
26+ const jsonString = JSON . stringify ( data , null , 2 ) ;
27+ const blob = new Blob ( [ jsonString ] , { type : "application/json" } ) ;
28+ saveAs ( blob , `phenopackets_${ individualId } .json` ) ;
29+ } ;
30+
2931 return (
3032 < >
31- < DownloadButton uri = { downloadUrl } disabled = { ! katsuUrl } >
33+ < Button
34+ icon = { < DownloadOutlined /> }
35+ onClick = { handleDownloadFromState }
36+ disabled = { ! data || isFetching }
37+ >
3238 Download JSON
33- </ DownloadButton >
34- < Divider />
39+ </ Button >
40+
41+ < Divider />
3542 { data === undefined || isFetching ? (
3643 < Skeleton title = { false } loading = { true } />
3744 ) : (
0 commit comments