Welcome! In this tutorial we will build a simple react app using the Uniscale SDK.
Step 1: Create a React app and install the SDK
Start a new React project using your preferred method. In this tutorial, we are using React + Next.js with TypeScript, but since we are only building a simple one-page app with very little functionality, the framework itself doesn't matter.
Run the following command and choose all default options:
npx create-next-app@latest
To use the SDK with our tutorial application, we will add an .npmrc file to the root of our project folder. The tutorial uses Uniscale Demo solution's SDK. Copy and paste the following block inside the .npmrc file you created:
Create a dispatcher.ts file inside your app folder. We will only use sample data from the SDK in this tutorial, so we will add a dispatcher and intercept only the features we want to use. Copy and paste this code block into the dispatcher.ts file you created:
It is recommended that you initialize the dispatcher only once when starting up your app. For the purpose of this tutorial, we will initialize the dispatcher inside our one-page application's top component, which is the Home component inside page.tsx. After successful initialization, we give the session to our app context, which will then allow any component within that context to use it.
If you are using Next.js in a real-life scenario, you may want to handle the session builder very differently, but for the sake of a general solution that works with a basic react app, we are initializing the session here. The dispatcher could also be implemented in a singleton class.
Replace the content of your page.tsx with the following code block:
After adding the previous block, you should be getting errors from the missing AppContextProvider. Create a file app.context.tsx and copy and paste this code block inside it:
Now that we have initialized the dispatcher and have a session stored in the app context, we can use the session to make requests. Create a new file messages.tsx and paste the following code block inside it:
This component will get any direct messages returned by the GetDirectMessageList endpoint. In this case, it only returns the sample data from the SDK, but in a real-life scenario, the session would make a POST request to a service.
Now replace the empty <div /> inside page.tsx with <MessageList />
Step 5: Run the app
Run the app with npm run dev or any script that works for your chosen environment.
You will now get a page with this text in the middle:
2020-02-28T08:09:10Z
Hello all
This is sample data returned from the session dispatcher. If you want to call a real API, you can modify dispatcher.ts to look like this:
import { DispatcherSession, GatewayRequest, Platform, Result,} from"@uniscale-sdk/ActorCharacter-Messagethreads";import { Patterns } from"@uniscale-sdk/ActorCharacter-Messagethreads/sdk/UniscaleDemo/Messages";import { DirectMessageFull } from"@uniscale-sdk/ActorCharacter-Messagethreads/sdk/UniscaleDemo/Messages/Messages/DirectMessageFull";exportconstinitializeDispatcher=async ():Promise<DispatcherSession> => {constsession=awaitPlatform.builder().withInterceptors((i) => {// Sample data if GetDirectMessageList endpoint calledi.interceptRequest(Patterns.messages.getDirectMessageList.allRequestUsages,Patterns.messages.getDirectMessageList.handleDirect((_input, _ctx) => {return [DirectMessageFull.samples().defaultSample()]; }), );// Real api if any other Messages service endpoints calledi.interceptPattern(Patterns.pattern,async (input, ctx) => {constheaders= { "Content-Type":"application/json" };constresponse=awaitfetch(`/api/service-to-module/`+ctx.featureId, { method:"POST", headers, body:GatewayRequest.from(input, ctx).toJson(), }, );returnResult.fromJson(awaitresponse.json()); }); }).build();return session.asSolution("fb344616-794e-4bd7-b81a-fb1e3361701f").withLocale("en-GB").withDataTenant("Customer 1 data tenant");};
At the end of this tutorial, your folder structure should look like this:
This function will return a DispatcherSession which we can later use to call any features specified in the SDK. See from the Quick start tutorial on how to use interceptors to call real services. In a real-life scenario, you would replace the solution's UUID with your own solution's UUID. See more information here: Front-end with Uniscale SDK