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

Issues regarding integreting the widget to a NextJS project #198

Open
Pooya-Oladazimi opened this issue Feb 3, 2025 · 1 comment
Open
Labels
documentation Improvements or additions to documentation enhancement New feature or request

Comments

@Pooya-Oladazimi
Copy link
Collaborator

The TS4NFDI service portal was initiated last week using the NextJS framework. This is the official React documentation recommendation regarding starting a React project (do not use React Lib alone, create-react-app is deprecated): https://react.dev/learn/start-a-new-react-project

I list the issues I faced here so that it can help us to improve the widgets and installation process:

I will add to this list if I find something more.

@Pooya-Oladazimi Pooya-Oladazimi added documentation Improvements or additions to documentation enhancement New feature or request labels Feb 3, 2025
@Pooya-Oladazimi
Copy link
Collaborator Author

Pooya-Oladazimi commented Feb 4, 2025

To use a widget in a NextJS app:

Our widgets are using the browser window object that is not accessible on the NextJS server. Therefore, just importing the widgets in a normal way does not work and raises errors on the build.

The widgets need to get importet dynamically on runtime. The issue is that dynamic import causes losing the widget input props type. So we need to define the type based on the used props:

'use client'

import dynamic from 'next/dynamic';

type AutocompleteWidgetProps = {
  api: string;
  hasShortSelectedLabel: boolean;
  parameter: string;
  placeholder: string;
  preselected: never[];
  selectionChangedEvent: () => void;
  showApiSource: boolean;
  singleSelection: boolean;
  ts4nfdiGateway: boolean;
};

const AutocompleteWidget = dynamic<AutocompleteWidgetProps>(() =>
  import("@ts4nfdi/terminology-service-suite").then((mod) => mod.AutocompleteWidget)
  , { ssr: false }) as React.ComponentType<AutocompleteWidgetProps>;;

import { QueryClient, QueryClientProvider } from "react-query";


export default function Widgets() {
  const queryClient = new QueryClient();
  return (
    <div className="md:col-span-8 sm:row-span-1">
      <QueryClientProvider client={queryClient}>
        <AutocompleteWidget
          api={"https://ts4nfdi-api-gateway.prod.km.k8s.zbmed.de/api-gateway/"}
          hasShortSelectedLabel={true}
          parameter={"ontology=mesh,efo&type=class&collection=nfdi4health&fieldList=description,label,iri,ontology_name,type,short_form"}
          placeholder="Search for a Concept"
          preselected={[]}
          selectionChangedEvent={function noRefCheck() { }}
          showApiSource={true}
          singleSelection={true}
          ts4nfdiGateway={true}
        />
      </QueryClientProvider>
    </div>
  );
}

Ref: https://nextjs.org/docs/pages/building-your-application/optimizing/lazy-loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant