Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDK: Develop useDotCMS React Hook #31134

Open
6 tasks
Tracked by #30943
rjvelazco opened this issue Jan 14, 2025 · 1 comment
Open
6 tasks
Tracked by #30943

SDK: Develop useDotCMS React Hook #31134

rjvelazco opened this issue Jan 14, 2025 · 1 comment

Comments

@rjvelazco
Copy link
Contributor

rjvelazco commented Jan 14, 2025

Caution

GitHub Issue Blocked by: #30968

Parent Issue

#30943

Overview

Create a React hook to streamline interactions with dotCMS, focusing on editor mode. This hook will manage subscriptions and notify the editor of page loads and URL changes.

Background

Currently, a similar hook exists within our examples; however, this hook should be officially provided as part of the SDK. By doing so, we aim to enhance the developer experience, making it easier and more efficient for developers to integrate and set up a page with dotCMS. Providing this hook through the SDK will streamline the process, reducing setup time and complexity, and allowing developers to get up and running as quickly as possible.

Task

Tasks

Preview Give feedback

Proposed Objective

Technical User Experience

Proposed Priority

Priority 3 - Average

Acceptance Criteria

  • Users can edit pages and see the latest changes.
  • Update the example to reflect the new hook.
  • The hook should emit the user's Page Query to the UVE.

External Links... Slack Conversations, Support Tickets, Figma Designs, etc.

Challenges and Considerations

  1. Compatibility Across React Versions: Ensure the hook works seamlessly across different versions of React.
  2. Public API Accessibility: All methods used within the hook should be part of the public API. Users might want to implement custom solutions.
  3. Documentation and Developer Guidance: Provide comprehensive documentation and guidance to assist developers in using the hook effectively.

Pseudo-Code Example

function useDotCMS({initialPageAsset, params }) {
    const [pageAsset, setPageAsset] = useState(initialPageAsset);
    
   /**
     * Initializes the page asset state with content fetched from the server.
     * This effect runs once on component mount if an initial page asset is not provided.
     * It sets up the page asset state and cleans up by removing the 'changes' listener.
     */
    useEffect(() => {
        if (initialPageAsset) return;

        const { pageAsset } = client.getPageContent({
            url: pathName,
            ...params
        });

        setPageAsset(pageAsset)

        return () => uveEditor.off('changes');
    }, []);

    /**
     * Sets up a listener for changes in the UVE editor.
     * Updates the page asset state when changes occur.
     * The listener is only active when inside the editor or when onReload is defined.
     */
    useEffect(() => {
        if (!isInsideEditor() && !onReload) return;
        uveEditor.on('changes', (pageAsset) => setPageAsset(pageAsset));
        return () => uveEditor.off('changes');
    }, []);

    /**
     * Notifies the editor of URL changes.
     * This effect is only active when inside the editor.
     */
    useEffect(() => {
        if (!isInsideEditor()) return;
        notifyEditor(params);
    }, [params.url]);

    return { pageAsset };
}
@rjvelazco rjvelazco changed the title SDK: Develop React Hook for dotCMS UVE [Spike] SDK: Develop React Hook for dotCMS UVE Jan 15, 2025
@rjvelazco rjvelazco changed the title [Spike] SDK: Develop React Hook for dotCMS UVE [SPIKE] SDK: Develop React Hook for dotCMS UVE Jan 21, 2025
@rjvelazco rjvelazco changed the title [SPIKE] SDK: Develop React Hook for dotCMS UVE SDK: Develop React Hook for dotCMS UVE Jan 21, 2025
@rjvelazco rjvelazco changed the title SDK: Develop React Hook for dotCMS UVE SDK: Develop useDotCms React Hook Jan 22, 2025
@rjvelazco
Copy link
Contributor Author

@zJaaal @KevinDavilaDotCMS Whoever is going to take this issue, once it is ready, we must create/design the error handling

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: New
Development

No branches or pull requests

1 participant