import { useEffect, useState } from 'react' import { fetchDrupalResource } from './lib/drupalClient' import './App.css' type DrupalArticle = { id: string attributes?: { title?: string status?: boolean created?: string } } type DrupalCollectionResponse = { data: T[] } type DrupalJsonApiEntryPoint = { links: Record } function App() { const [articles, setArticles] = useState([]) const [resourcePath, setResourcePath] = useState(null) const [isLoading, setIsLoading] = useState(true) const [error, setError] = useState(null) useEffect(() => { const loadArticles = async () => { try { setIsLoading(true) setError(null) const entryPoint = await fetchDrupalResource('') const nodeResources = Object.keys(entryPoint.links).filter((key) => key.startsWith('node--')) if (nodeResources.length === 0) { throw new Error('No node resources found in JSON:API. Create a content type (for example Article) and ensure JSON:API is enabled.') } const selectedNodeResource = nodeResources.includes('node--article') ? 'node--article' : nodeResources[0] const selectedPath = `/${selectedNodeResource.replace('--', '/')}` setResourcePath(selectedPath) const response = await fetchDrupalResource>(selectedPath) setArticles(response.data) } catch (loadError) { const message = loadError instanceof Error ? loadError.message : 'Failed to load articles from Drupal.' setError(message) } finally { setIsLoading(false) } } void loadArticles() }, []) return (

Articles

{!isLoading && !error && resourcePath &&

Using resource: {resourcePath}

} {isLoading &&

Loading articles…

} {error && (

Could not fetch articles: {error}

)} {!isLoading && !error && articles.length === 0 && (

Connected to Drupal, but no articles were returned.

)} {!isLoading && !error && articles.length > 0 && (
    {articles.map((article) => (
  • {article.attributes?.title ?? '(Untitled)'}
    ID: {article.id}
    Status: {article.attributes?.status ? 'Published' : 'Unpublished'}
    {article.attributes?.created &&
    Created: {new Date(article.attributes.created).toLocaleString()}
    }
  • ))}
)}
) } export default App