5.39.5 • Published 9 days ago

@webiny/app-aco v5.39.5

Weekly downloads
-
License
MIT
Repository
github
Last release
9 days ago

@webiny/app-aco

npm.io npm.io code style: prettier PRs Welcome

A set of frontend aco-related utilities.

Table of Contents

Installation

npm install --save @webiny/app-aco

Or if you prefer yarn:

yarn add @webiny/app-aco

Overview

The @webiny/app-aco package contains essential aco-related utilities (Advanced Content Organisation), that can be used within a React app. These include the FoldersProvider provider component, the useFolders and useLinks hooks, which can be used to retrieve the current folder and link information and interact with them.

ℹ️ INFO

Internally, the FoldersProvider provider retrieves information from the Webiny's default GraphQL API. Because of this, note that this project relies on @webiny/api-aco when it comes to retrieving folders and links information (via GraphQL).

Reference

Components

FoldersProvider

export declare const FoldersProvider: React.FC;

The FoldersProvider is a provider component, which retrieves the folders and links information. The component also makes it possible to use the useFolders and useLinks hook, which can be used to list, create, update or delete folders and links within the React app.

import React from "react";
import { FoldersProvider } from "@webiny/app-aco";

export const App = () => {
  return (
    <FoldersProvider>
      <MyApp />
    </FoldersProvider>
  );
};

FolderTree

import { NodeModel } from "@minoru/react-dnd-treeview";
import { DndItemData } from "~/types";

interface Props {
    type: string;
    title: string;
    onFolderClick: (data: NodeModel<DndItemData>["data"]) => void;
    onTitleClick?: (event: React.MouseEvent<HTMLElement>) => void;
    focusedFolderId?: string;
}

declare function FolderTree(props: Props): React.FC;

FolderTree component shows the tree list of folders for a particular type.

import React from "react";
import { FolderTree } from "@webiny/app-aco";

export const MyComponent = () => {
    return (
        <FolderTree
            type={"page"}
            title={"All pages"}
            onFolderClick={item => console.log(item)}
            onTitleClick={() => console.log("Do whatever you like on title click")}
            focusedFolderId={"anyExistingId"}
        />
    );
};

ℹ️ INFO

Internally, the FolderTree uses react-dnd-treeview to render and manage the folder list: DO NOT rely on any of this package features, we might change it in the future.

FolderDialogCreate

import { DialogOnClose } from "@webiny/ui/Dialog";

interface Props {
    type: string;
    open: boolean;
    onClose: DialogOnClose;
    parentId?: string | null;
}

declare function FolderDialogCreate(props: Props): React.FC;

FolderDialogCreate component shows a dialog to allow users to create a new folder.

import React, { useState } from "react";
import { FolderDialogCreate } from "@webiny/app-aco";

export const MyComponent = () => {
    const [dialogOpen, setDialogOpen] = useState(false);
    
    return (
        <>
            <button onClick={() => setDialogOpen(true)}>Create folder</button>
            <FolderDialogCreate
                type={"page"}
                open={dialogOpen}
                onClose={() => setDialogOpen(false)}
                parentId={"anyParentId"}
            />
        </>
    );
};

FolderDialogUpdate

import { DialogOnClose } from "@webiny/ui/Dialog";

interface Props {
    folder: FolderItem;
    open: boolean;
    onClose: DialogOnClose;
}

declare function FolderDialogUpdate(props: Props): React.FC;

FolderDialogUpdate component shows a dialog to allow users to update an existing folder.

import React, { useState } from "react";
import { FolderDialogUpdate } from "@webiny/app-aco";

type Props = {
    folder: FolderItem;
}

export const MyComponent = ({ folder }: Props) => {
    const [dialogOpen, setDialogOpen] = useState(false);
    
    return (
        <>
            <button onClick={() => setDialogOpen(true)}>Edit folder</button>
            <FolderDialogUpdate
                folder={folder}
                open={dialogOpen}
                onClose={() => setDialogOpen(false)}
            />
        </>
    );
};

FolderDialogDelete

import { DialogOnClose } from "@webiny/ui/Dialog";

interface Props {
    folder: FolderItem;
    open: boolean;
    onClose: DialogOnClose;
}

declare function FolderDialogDelete(props: Props): React.FC;

FolderDialogDelete component shows a dialog to allow users to delete an existing folder.

import React, { useState } from "react";
import { FolderDialogDelete } from "@webiny/app-aco";

type Props = {
    folder: FolderItem;
}

export const MyComponent = ({ folder }: Props) => {
    const [dialogOpen, setDialogOpen] = useState(false);
    
    return (
        <>
            <button onClick={() => setDialogOpen(true)}>Delete folder</button>
            <FolderDialogDelete
                folder={folder}
                open={dialogOpen}
                onClose={() => setDialogOpen(false)}
            />
        </>
    );
};

Hooks

useFolders

import {FolderItem, FoldersActions} from "./types";

interface UseFoldersHook {
    folders: FolderItem[];
    loading: Record<FoldersActions, boolean>;
    getFolder: (id: string) => Promise<FolderItem>;
    createFolder: (folder: Omit<FolderItem, "id">) => Promise<FolderItem>;
    updateFolder: (folder: FolderItem) => Promise<FolderItem>;
    deleteFolder(folder: FolderItem): Promise<true>;
}

export declare function useFolders(type: string): UseFoldersHook;

useFolders hook allows you to interact with folders state and related APIs while building your custom component.

import React from "react";
import { useFolders } from "@webiny/app-aco";

export const MyComponent = () => {
    const { folders } = useFolders("page");
    
    if (folders) {
        return folders.map(folder => {
            return <span key={folder.id}>{folder.name}</span>;
        });
    }
    
    return <span>No folders to show</span>;
};

As you might notice, there is not listFolders method available from useFolders() hook: this is because on first mount, listFolders is called internally, which will either issue a network request, or load folders from cache.

You don't need to store the result of it to any local state; that is managed by the context provider.

useLinks

import { LinkItem, LinksActions } from "./types";

interface UseLinksHook {
    links: LinkItem[];
    loading: Record<LinksActions, boolean>;
    getLink: (id: string, folderId: string) => Promise<LinkItem>;
    createLink: (link: Omit<LinkItem, "linkId">) => Promise<LinkItem>;
    updateLink: (link: LinkItem, contextFolderId: string) => Promise<LinkItem>;
    deleteLink(link: LinkItem): Promise<true>;
}

export declare function useLinks(folderId: string): UseLinksHook;

useLinks() hook allows you to interact with folders links state and related APIs while building your custom component.

import React, { useEffect, useState } from "react";
import { useLinks } from "@webiny/app-aco";

import { getEntryDetails } from "./any-custom-hook"

export const MyComponent = () => {
    const { links } = useLinks("anyFolderId");
    const [entries, setEntries] = useState([]);
    
    useEffect(() => {
        const details = getEntryDetails(links);
        setEntries(details)
    }, [links])
    
    if (entries) {
        return entries.map(entry => {
            return <span key={entry.id}>{entry.name}</span>;
        });
    }
    
    return <span>No entries to show</span>;
};

As you might notice, there is not listLinks method available from useLinks() hook: this is because on first mount, listLinks is called internally, which will either issue a network request, or load links from cache.

You don't need to store the result of it to any local state; that is managed by the context provider.

5.39.5

9 days ago

5.39.5-beta.1

9 days ago

5.40.0-beta.2

9 days ago

5.40.0-beta.1

15 days ago

5.39.4

21 days ago

5.39.4-beta.1

22 days ago

5.40.0-beta.0

1 month ago

5.39.4-beta.0

2 months ago

5.39.3

2 months ago

5.39.3-beta.0

2 months ago

5.39.3-beta.1

2 months ago

5.39.2

2 months ago

5.39.2-beta.2

2 months ago

5.39.2-beta.3

2 months ago

5.39.2-beta.1

2 months ago

5.39.2-beta.0

2 months ago

5.38.6

3 months ago

5.39.1

3 months ago

5.39.1-beta.1

3 months ago

5.39.0

3 months ago

5.39.1-beta.0

3 months ago

5.39.0-beta.2

3 months ago

5.39.0-beta.3

3 months ago

5.38.5-beta.0

3 months ago

5.38.5

3 months ago

5.38.4

4 months ago

5.38.4-beta.0

4 months ago

5.38.3-beta.1

4 months ago

5.38.3

4 months ago

5.38.3-beta.0

4 months ago

5.38.2

5 months ago

5.38.2-beta.0

5 months ago

5.37.2-beta.3

8 months ago

5.37.2-beta.2

8 months ago

5.39.0-beta.0

5 months ago

5.39.0-beta.1

5 months ago

5.38.0-beta.0

6 months ago

5.37.1-beta.3

9 months ago

5.37.1-beta.1

9 months ago

5.37.1-beta.2

9 months ago

5.37.3-beta.0

8 months ago

5.37.1-beta.0

9 months ago

5.37.6-beta.0

7 months ago

5.37.5-beta.0

7 months ago

5.37.3-beta.1

8 months ago

5.37.2-beta.1

9 months ago

5.37.6-beta.1

7 months ago

5.37.3-beta.2

8 months ago

5.37.2-beta.0

9 months ago

5.37.5-beta.1

7 months ago

5.37.4-beta.1

8 months ago

5.37.4-beta.0

8 months ago

5.37.8-beta.0

6 months ago

5.37.7-beta.0

6 months ago

5.38.0-beta.5

6 months ago

5.38.0-beta.6

6 months ago

5.38.0-beta.3

6 months ago

5.38.0-beta.4

6 months ago

5.38.0-beta.1

6 months ago

5.38.0-beta.2

6 months ago

5.38.1-beta.0

5 months ago

5.37.0-beta.3

10 months ago

5.37.0-beta.2

10 months ago

5.37.0-beta.1

10 months ago

5.37.7

6 months ago

5.37.8

6 months ago

5.37.5

7 months ago

5.37.6

7 months ago

5.37.3

8 months ago

5.37.4

8 months ago

5.37.1

9 months ago

5.37.2

8 months ago

5.37.0

9 months ago

5.34.9-beta.0

7 months ago

5.38.0

6 months ago

5.38.1

5 months ago

5.36.2

11 months ago

5.36.2-beta.2

11 months ago

5.36.2-beta.1

11 months ago

5.37.0-beta.0

11 months ago

5.36.2-beta.0

11 months ago

5.35.4

11 months ago

5.35.4-beta.1

11 months ago

5.35.4-beta.0

11 months ago

5.35.3

11 months ago

5.36.1

11 months ago

5.36.1-beta.1

11 months ago

5.36.1-beta.0

11 months ago

5.35.3-beta.0

11 months ago

5.36.0

12 months ago

5.36.0-beta.2

12 months ago

5.36.0-beta.1

12 months ago

5.36.0-beta.0

12 months ago

5.35.2

12 months ago

5.35.2-beta.0

12 months ago

5.35.1

12 months ago

5.35.1-beta.0

12 months ago

5.35.0

1 year ago

5.35.0-beta.2

1 year ago

5.35.0-beta.1

1 year ago

5.35.0-beta.0

1 year ago

5.34.8

1 year ago

5.34.8-beta.1

1 year ago

5.34.8-beta.0

1 year ago

5.34.7

1 year ago

5.34.7-beta.2

1 year ago

5.34.7-beta.1

1 year ago

5.34.7-beta.0

1 year ago

5.34.6

1 year ago

5.34.6-beta.6

1 year ago

5.34.6-beta.5

1 year ago

5.34.6-beta.4

1 year ago

5.34.6-beta.3

1 year ago

5.34.6-beta.2

1 year ago

5.34.6-beta.1

1 year ago

5.34.6-beta.0

1 year ago

5.34.5

1 year ago

5.34.5-beta.1

1 year ago

5.34.5-beta.0

1 year ago

5.34.4

1 year ago

5.34.4-beta.0

1 year ago

5.34.3

1 year ago

5.34.3-beta.3

1 year ago

5.34.3-beta.2

1 year ago

5.34.3-beta.1

1 year ago

5.34.3-beta.0

1 year ago

5.34.2

1 year ago

5.34.2-beta.2

1 year ago

5.34.2-beta.1

1 year ago

5.34.2-beta.0

1 year ago

5.34.1

1 year ago

5.34.1-beta.3

1 year ago

5.34.1-beta.2

1 year ago

5.34.1-beta.1

1 year ago

5.34.1-beta.0

1 year ago

5.34.0

1 year ago

5.34.0-beta.2

1 year ago

5.34.0-beta.1

1 year ago

5.34.0-beta.0

1 year ago