From 719a1e81d5af5c872f79f11f462e4b30211f80b7 Mon Sep 17 00:00:00 2001 From: IwanIDev Date: Fri, 20 Mar 2026 13:39:53 +0000 Subject: Add local development support with Docker for Drupal and configure API proxy --- src/App.tsx | 107 ++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 82 insertions(+), 25 deletions(-) (limited to 'src/App.tsx') diff --git a/src/App.tsx b/src/App.tsx index aaeba0a..3472fd0 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,34 +1,91 @@ -import { useState } from 'react' -import reactLogo from './assets/react.svg' -import viteLogo from '/vite.svg' +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 [count, setCount] = useState(0) + 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 ( - <> - -

Vite + React

-
- -

- Edit src/App.tsx and save to test HMR +

+

Articles

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

Using resource: {resourcePath}

} + + {isLoading &&

Loading articles…

} + + {error && ( +

+ Could not fetch articles: {error}

-
-

- Click on the Vite and React logos to learn more -

- + )} + + {!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()}
    } +
  • + ))} +
+ )} + ) } -- cgit