|
| 1 | +import React, { useEffect, useState } from 'react'; |
| 2 | +import DocumentListItem from '../lib_components/components/Documents/DocumentListItem'; |
| 3 | + |
| 4 | +export default function BiorepoCollectionPageContent() { |
| 5 | + const [filteredSpecs, setFilteredSpecs] = useState([]); |
| 6 | + |
| 7 | + useEffect(() => { |
| 8 | + // Get the list of product IDs from the global JavaScript variable |
| 9 | + const productIds = window.BiorepoCollectionData ? JSON.parse(window.BiorepoCollectionData) : []; |
| 10 | + |
| 11 | + if (productIds.length === 0) return; |
| 12 | + |
| 13 | + // Fetch specs for each product ID |
| 14 | + const fetchSpecs = async () => { |
| 15 | + try { |
| 16 | + const responses = await Promise.all( |
| 17 | + productIds.map((productId) => fetch(`https://data.neonscience.org/api/v0/products/${productId}`).then((res) => res.json())), |
| 18 | + ); |
| 19 | + |
| 20 | + // Extract and filter specs based on "Protocol" in specDescription |
| 21 | + const allFilteredSpecs = responses.flatMap((response) => response.data?.specs?.filter((spec) => spec.specDescription.includes('Protocol and Procedure')) || []); |
| 22 | + |
| 23 | + setFilteredSpecs(allFilteredSpecs); |
| 24 | + } catch (error) { |
| 25 | + console.error('Error fetching product specs:', error); |
| 26 | + } |
| 27 | + }; |
| 28 | + |
| 29 | + fetchSpecs(); |
| 30 | + }, []); |
| 31 | + |
| 32 | + return ( |
| 33 | + <div> |
| 34 | + {filteredSpecs.length > 0 ? ( |
| 35 | + filteredSpecs.map((spec, index) => ( |
| 36 | + <DocumentListItem |
| 37 | + key={spec.specId || index} |
| 38 | + document={{ |
| 39 | + name: spec.specNumber, // Document name |
| 40 | + type: spec.specType || 'application/pdf', // Default to PDF |
| 41 | + size: spec.specSize || 0, // Default size |
| 42 | + description: spec.specDescription, // Description |
| 43 | + }} |
| 44 | + containerComponent="div" |
| 45 | + enableDownloadButton |
| 46 | + /> |
| 47 | + )) |
| 48 | + ) : ( |
| 49 | + <p></p> |
| 50 | + )} |
| 51 | + </div> |
| 52 | + ); |
| 53 | +} |
0 commit comments