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

plugin-flow-builder: create contents with map #2863

Merged
merged 10 commits into from
Jul 9, 2024

Conversation

Iru89
Copy link
Contributor

@Iru89 Iru89 commented Jun 27, 2024

Description

  • The functions getTextContent and getImageSrc return an empty string if it does not find the value.

  • The useWebviewContents hook accept an object mapContents. This object relates the names the developer wants for each content to the contentID of each content. Using this object the hook returns a contents object with all the contents.

Context

The hook useWebviewContents now returns a contents object which is like the mapContents passed as a parameter but with the relation key: fetchedContent
An important point is to have the contents object typed the same as the mapContents

Approach taken / Explain the design

To have the contents typed like the mapContents it is necessary create MyWebviewContentsContext outside the webview component and export it to be able to use it inside the components.
To create this MyWebviewContentsContext we use the createWebviewContentsContext function that allows to pass the type of the used in mapContents, by creating the type of this object or by passing typeof mapContents.
This way when using useContext(MyWebviewContentsContext) we get the contents typed as the mapContents.

To document / Usage example

  type WebviewContents = {
    textIntro: string
    image2: string
    headerWebview: string
    image: string
  }

export const MyWebviewContentsContext = createWebviewContentsContext<WebviewContents>()

export const FlowBuilderWebview = () => {
  const webviewRequestContext = useContext(WebviewRequestContext)

  const { isLoading, error, webviewContentsContext } = useWebviewContents({
    apiUrl: FLOW_BUILDER_API_URL,
    version: FlowBuilderJSONVersion.LATEST,
    orgId: webviewRequestContext.session.organization_id,
    botId: webviewRequestContext.session.bot.id,
    webviewId: WEBVIEW_ID,
    locale: 'es',
    mapContents: {
      textIntro: 'TEXT_INTRO',
      image2: 'IMAGE_2',
      headerWebview: 'HEADER_WEBVIEW',
      image: 'IMAGE',
    },
  })

  if (error) {
    return <div>Error</div>
  }

  return (
    <MyWebviewContentsContext.Provider value={webviewContentsContext}>
      // your components
      <Step1 />
    </MyWebviewContentsContext.Provider>
  )
}

In Step1 component you can read the contents from context like

export const Step1 = () => {
  const webviewContentsContext = useContext(MyWebviewContentsContext)

  const { contents } = webviewContentsContext

  return (
    <div>
      <h1>Step2</h1>

      <div>
        <p>{contents.textIntro}</p>
        <img style={{ width: '350px' }} src={contents.image2} />
      </div>
    </div>
  )
}

@Iru89 Iru89 requested review from asastre and AlbertGom June 27, 2024 10:58
Copy link
Contributor

@asastre asastre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a 1 change, the rest LGTM

@Iru89 Iru89 force-pushed the plugin-flow-builder/refactor-webview-context branch from 85e2146 to 3291c26 Compare July 2, 2024 09:18
@pull-request-size pull-request-size bot added size/L and removed size/M labels Jul 4, 2024
@Iru89 Iru89 requested a review from asastre July 5, 2024 10:53
@Iru89 Iru89 marked this pull request as ready for review July 5, 2024 10:53
@Iru89 Iru89 force-pushed the plugin-flow-builder/refactor-webview-context branch from da4335b to 7a23d15 Compare July 8, 2024 07:35
Copy link
Contributor

@vanbasten17 vanbasten17 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice feature!

@Iru89 Iru89 requested review from asastre and removed request for asastre July 8, 2024 09:29
@Iru89 Iru89 merged commit 74a05fb into master-lts Jul 9, 2024
3 of 4 checks passed
@Iru89 Iru89 deleted the plugin-flow-builder/refactor-webview-context branch July 9, 2024 14:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants