Github issues viewer

Let's use ConanJs to build a realistic app that browses a remote repository

Introduction

We used this example to experiment with defining different isolated states and show how they can be used together when different asynchronous calls are needed. In this section we have only shown the code in Typescript.

Creating the Conan states

We have divided the data handled by the app into four independent states:

The remote repository connection details is called repoState$:

export const repoState$: RepoState = 
        Conan.light<RepoData>('repo', {org: "rails", repo: "rails", page: 1})

The specific repository data information retrieved is modelled in repoDetailsState$ :

export interface RepoDetailsData {
    openIssuesCount: number
    error: string | null
}

export const repoDetailsState$: RepoDetailsState = Conan.state<RepoDetailsData>({
    name: 'repo-details',
    initialData: {openIssuesCount: -1, error: null},
    reducers: repoDetailsReducersFn,
    actions: repoDetailsActionsFn
});

The issues data is modelled in issuesState$:

and finally the issues comments is modelled in issuesCommentsState$:

Adding async operations

In this app we need to fetch the issues of a given repository, fetch the information of that same repository and finally retrieve the comments of a given issue. We have implemented this logic in a service called IssuesServiceImpl which uses ConanJs Asap to implement the asynchronous calls:

Adding DI dependencies

We have made these states and the service available to all the app via ConanJs DI:

Fetching the issues

We need to implement an async call to retrieve the issues of the repository given. We can see how that is done in the actions passed to issuesState$:

The fist action fetch has the async call, so it's wrapped with thread.monitor. The other 2 actions are synchronous, so they can just call the reducer

Fetching the repository information

We need another async call to retrieve the repository information, but this time bound to the repoDetailsState$. This state just has an action passed in, and it will look like:

Fetching comments

Our last async call will happen when the user selects to open an issue, and we need to show the comments. So the issuesCommentsState$ will have an action that looks like:

Displaying the issues pages

Let's now see how we can display the issues and the repository information. The following fragment belongs to the functional component IssuesListPage:

It uses connectMap to link repoDetailsState$ with the component IssuesPageHeader, and the DI context to fully connect issuesState with the component IssuesList.

Displaying the comments

The component IssueDetailsPage uses a ConanJs hook useConanState to connect with the state issuesCommentsState$ and retrieve the issues in its own useEffect:

Getting the code

The full code for this example is available at

or the code sandbox below, for more details on this ConanJs feature.

Code sandbox

Last updated

Was this helpful?