Skip to content

Commit 308f42b

Browse files
committed
Add DRS URIs to LocalStorage
1 parent 208e80f commit 308f42b

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use client'
2+
import React from 'react';
3+
import Button from '@mui/material/Button';
4+
import ShoppingCartIcon from '@mui/icons-material/ShoppingCart'; // Ensure you have @mui/icons-material installed
5+
6+
interface DRSBundleButtonProps {
7+
data?: { [key: string]: string | bigint | number }[];
8+
}
9+
10+
function unique(L: string[]): string[] {
11+
const S = new Set()
12+
const U: string[] = []
13+
for (const el of L) {
14+
if (S.has(el)) continue
15+
S.add(el)
16+
U.push(el)
17+
}
18+
return U
19+
}
20+
21+
const DRSBundleCartButton: React.FC<DRSBundleButtonProps> = ({ data }) => {
22+
// Memoize the access_urls based on data
23+
const access_urls = React.useMemo(() => data?.filter(({ access_url }) => !!access_url).map(({ access_url }) => access_url as string), [data]);
24+
25+
// Handle the copy to clipboard action
26+
const handleDRSBundle = React.useCallback(() => {
27+
if (typeof localStorage !== 'undefined') {
28+
localStorage.setItem('drs-cart', unique([
29+
...(localStorage.getItem('drs-cart') || '').split('\n'),
30+
...(access_urls ?? []),
31+
].filter(item => !!item)).join('\n'))
32+
}
33+
}, [access_urls]);
34+
35+
// Return early if no data is available, ensuring hooks are still called
36+
if (!data || data.length === 0) return null;
37+
38+
return (
39+
<>
40+
<Button
41+
variant="contained"
42+
color="primary"
43+
startIcon={<ShoppingCartIcon />}
44+
onClick={handleDRSBundle}
45+
disabled={!access_urls?.length}
46+
>
47+
Add to Cart
48+
</Button>
49+
</>
50+
);
51+
};
52+
53+
export default DRSBundleCartButton;

drc-portals/app/data/c2m2/ExpandableTable.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import AccordionDetails from '@mui/material/AccordionDetails';
88
import SearchablePagedTable, { Description } from './SearchablePagedTable';
99
import DownloadButton from './DownloadButton';
1010
import DRSBundleButton from './DRSBundleButton';
11+
import DRSBundleCartButton from './DRSBundleCartButton';
1112
import { isURL, getNameFromBiosampleTable, getNameFromSubjectTable, getNameFromCollectionTable, getNameFromFileProjTable } from './utils';
1213
import Link from '@/utils/link';
1314
import { RowType } from './utils';
@@ -142,6 +143,7 @@ const ExpandableTable: React.FC<ExpandableTableProps> = ({
142143
onRowSelect={handleRowSelect}
143144
/>
144145
<div className="flex flex-row gap-4">
146+
{drsBundle && <DRSBundleCartButton data={dataToSend} />}
145147
{drsBundle && <DRSBundleButton data={dataToSend} />}
146148
<DownloadButton data={dataToSend} filename={downloadFileName} name={"Download Selected"} />
147149
<DownloadButton data={full_data} filename={downloadFileName+"_ALL.json"} name={"Download All"} />

0 commit comments

Comments
 (0)