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

Readme React sample #18

Closed
maartenhvdh opened this issue May 5, 2023 · 6 comments
Closed

Readme React sample #18

maartenhvdh opened this issue May 5, 2023 · 6 comments
Assignees

Comments

@maartenhvdh
Copy link

maartenhvdh commented May 5, 2023

Brief bug description

React sample in the readme didn't work for me

Repro steps

  1. Install
  2. Copy paste React sample
  3. Gives multiple errors, mainly on naming

Expected behavior

This is the working React sample:

import { PortableText, PortableTextReactComponents } from "@portabletext/react";
import {
  browserParse,
  transformToPortableText,
} from '@kontent-ai/rich-text-resolver';
import { useMemo } from "react";

const createPortableTextComponents = (linkedItems) => {
  const portableTextComponents: Partial<PortableTextReactComponents> = {
    types: {
      component: (block) => {
        const item = linkedItems.find(
          (item) => item.system.codename === block.value.component._ref
        );
        return <div>{item?.elements.text_element.value}</div>;
      },
      table: ({ value }) => {
        const table = (
          <table>
            {value.rows.map((row) => (
              <tr>
                {row.cells.map((cell) => {
                  return (
                    <td>
                      <PortableText
                        value={cell.content}
                        components={portableTextComponents}
                      />
                    </td>
                  );
                })}
              </tr>
            ))}
          </table>
        );
        return table;
      },
      image: ({ value }) => {
        // It is possible to use images from the rich text element response same as for linked items
        // const image = images.find(image => image.image_id === value.asset._ref)
        return <img src={value.asset.url}></img>;
      },
    },
    marks: {
      link: ({ value, children }) => {
        const target = (value?.href || "").startsWith("http")
          ? "_blank"
          : undefined;
        return (
          <a
            href={value?.href}
            target={target}
            rel={value?.rel}
            title={value?.title}
            data-new-window={value["data-new-window"]}
          >
            {children}
          </a>
        );
      },
      internalLink: ({ value, children }) => {
        // It is possible to use links from the rich text element response same as for linked items
        // const item = links.find(link => link.link_id === value.reference._ref);
        return (
          <a href={"https://somerandomwebsite.xyz/" + value.reference._ref}>
            {children}
          </a>
        );
      },
    },
  };

  const MyComponent = ({ props }) => {
    // https://github.com/portabletext/react-portabletext#customizing-components
    const parsedTree = browserParse(props.element.value);
    const portableText = transformToPortableText(parsedTree);

    return (
      <PortableText value={portableText} components={portableTextComponents} />
    );
  }
};

Test environment

n/a

Additional context

n/a

Screenshots

n/a

@maartenhvdh maartenhvdh mentioned this issue May 5, 2023
5 tasks
@Simply007
Copy link
Contributor

I wrapped code sample to code snippet syntax

@Simply007
Copy link
Contributor

I still don't understand the transformation of createPortableTextComponents that does not return portable components and is not being used inside the component.

Could you provide the errors you got? Are you using TS or JS?

The sample itself should work more like a skeleton that you adjust with your resolvers (we might need to emphasize that).

There is a working example that this skeleton is derived from: kontent-ai/sample-app-react#224

In PR, there are also some functional issues I described in the review.

@maartenhvdh
Copy link
Author

I am using typescript and I have it up and running in my MOAD environment but I had issues with:

image

@Simply007
Copy link
Contributor

I am using typescript and I have it up and running in my MOAD environment but I had issues with:

image

Yes - that is because you removed the brackets and forgot to add a return statement around the expression (thus function return void):

@maartenhvdh
Copy link
Author

maartenhvdh commented May 11, 2023

So this works for me:

import { PortableText, PortableTextReactComponents } from "@portabletext/react";
import {
  browserParse,
  transformToPortableText,
} from '@kontent-ai/rich-text-resolver';
import { useMemo } from "react";

const createPortableTextComponents = (linkedItems) => {
  const portableTextComponents: Partial<PortableTextReactComponents> = {
    types: {
      component: (block) => {
        const item = linkedItems.find(
          (item) => item.system.codename === block.value.component._ref
        );
        return <div>{item?.elements.text_element.value}</div>;
      },
      table: ({ value }) => {
        const table = (
          <table>
            {value.rows.map((row) => (
              <tr>
                {row.cells.map((cell) => {
                  return (
                    <td>
                      <PortableText
                        value={cell.content}
                        components={portableTextComponents}
                      />
                    </td>
                  );
                })}
              </tr>
            ))}
          </table>
        );
        return table;
      },
      image: ({ value }) => {
        // It is possible to use images from the rich text element response same as for linked items
        // const image = images.find(image => image.image_id === value.asset._ref)
        return <img src={value.asset.url}></img>;
      },
    },
    marks: {
      link: ({ value, children }) => {
        const target = (value?.href || "").startsWith("http")
          ? "_blank"
          : undefined;
        return (
          <a
            href={value?.href}
            target={target}
            rel={value?.rel}
            title={value?.title}
            data-new-window={value["data-new-window"]}
          >
            {children}
          </a>
        );
      },
      internalLink: ({ value, children }) => {
        // It is possible to use links from the rich text element response same as for linked items
        // const item = links.find(link => link.link_id === value.reference._ref);
        return (
          <a href={"https://somerandomwebsite.xyz/" + value.reference._ref}>
            {children}
          </a>
        );
      },
    },
  }
  return portableTextComponents;
};

const MyComponent = ({ props }) => {
  // https://github.com/portabletext/react-portabletext#customizing-components
  const portableTextComponents = useMemo(
    () => createPortableTextComponents(props.element.linkedItems),
    [props.element.linkedItems]
  );

  const parsedTree = browserParse(props.element.value);
  const portableText = transformToPortableText(parsedTree);

  return (
    <PortableText value={portableText} components={portableTextComponents} />
  );
};

@pokornyd pokornyd self-assigned this Dec 10, 2023
@pokornyd
Copy link
Member

v1.0.0 modified the codebase and changed some naming. all code examples were adjusted accordingly. readability was chosen over complexity, so they are not copy-paste and assume some SDK and code knowledge, which is clearly communicated via inline comments. closing

@pokornyd pokornyd linked a pull request Jan 30, 2024 that will close this issue
5 tasks
@pokornyd pokornyd closed this as not planned Won't fix, can't repro, duplicate, stale Jan 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants