Search After Stop Typing React Component

In this post I’m going to go through how to implement a search criteria component that only invokes the search in a calling component after the user has stopped typing.

So, we have the following search criteria component that invokes the search on a submit button at the moment:

… and this component is referenced like the following in a calling component:

At the moment, our submit button triggers the form’s submit which in tern calls handleSubmit() which then calls this.props.doSearch in our calling component to invoke the search (our calling component actually does the search).

Here’s what our component looks like at the moment:

We want to remove the submit button and just have the search invoke when the user stops typing.

So, let’s first remove the form and submit button from our render function:

Now let’s just invoke the search in handleCriteriaChange after we have set the criteria in the state:

So, our component invokes the search on every keystroke that the user does:

Not quite what we want!

We need to “debounce” the search. That’s where the debounce function in the excellent lodash library comes in.

So, let’s bring lodash and the TypeScript types into our project:

We now need to import just the debounce function into our component:

If we look at the docs, debounce just creates another function that delays calling another function. So, let’s create our “debounce” function leaving the console.log() so that we can check that doSearch is only being called when the user has stopped typing:

… and let’s call our new function from handleCriteriaChange:

… and let’s give this a try:

… which is just what we want!

Here’s the full code for our debouncing search criteria component:

Recommended reading for building great React apps:

Getting the right version of TypeScript in a Visual Studio ASP.NET Core Project

I’m building an ASP.NET Core Angular 2 app. I think I’m using the latest version of TypeScript (2.1 at the moment) but I’m not sure …

If I open a command prompt and type:

it gives me:

So, great, my app must be using the 2.1?

Let’s check this by writing some 2.1 code:

Visual Studio complains straight away, putting a red squiggly line under the ...‘s. If you hover over the complaints, you get a tooltip saying “property assignment expected“.

Lets try running the app. It runs perfectly fine with mergedPerson correctly outputted to the console.

What’s going on? The visual studio compiler must be using a different version of TypeScript.

For classic ASP.NET projects, TypeScriptToolsVersion in csproj used to define the TypeScript version but that is no longer the case in ASP.NET core. In ASP.NET Core, this appears to be defined in a file called Microsoft.TypeScript.targets at C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v15.0\TypeScript:

So, this makes sense – Visual Studio appears to be using v1.8 (line 4).

Unfortunately, simply changing the 1.8 to 2.1 didn’t work for me.
I looked in my C:\Program Files (x86)\Microsoft SDKs\TypeScript directory. I only had a folder for 1.8.

Visual studio was obviously missing something, so, I downloaded and installed the latest version of TypeScript for Visual Studio.

Voila! The red squiggly lines disappeared in Visual Studio, my app still ran fine and the version references in C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v15.0\TypeScript\Microsoft.TypeScript.targets has been upgraded to 2.1.

Recommended reading for building great TypeScript apps:

Connect with me:RSSGitHubTwitterLinkedIn

Taking Stripe Payments with Angular 2 and ASP.NET Core

Stripe is a service that allows you to very easily take payments online. In this post, I’ll go through how to take a payment via stripe using an Angular 2 frontend and an ASP.NET Core backend.

Sign up to Stripe

Firstly, we need to sign up for a stripe account. You can signup for a free test account here

After you have signed up, we need the Secret and Publishable keys. You can get these from Your account > Account settings > API Keys

Frontend Page

Let’s build the frontend first, using an Angular 2 component.

We are going to use stripe checkout which is super easy to implement and protects us from any PCI headaches (as long as our site runs on HTTPS).

First, we need to add a reference to the standard stripe checkout JavaScript in our server side ASP.NET Core view (line 3). This adds the global variable StripeCheckout which we’ll reference in our angular component. Note that we can not add this script into our angular component template because angular will remove it.

Now let’s add our angular payment component …

Here’s the simple markup (referencing bootstrap classes) which allows a user to buy 3 products, a T-shirt, some trainers or some jeans. Each button will invoke a function in our component code to take the payment via stripe. We show whether the payment has been successful or not in the last div in the markup, referencing a property called takePaymentResult.

Our component code is below …

Our 3 button handlers at are the bottom of the code and simply call into openCheckout() passing the product name, amount (in pence) and a callback to get invoked when we receive a token from stripe.

openCheckout() references the global variable StripeCheckout from checkout.js. Firstly we configure StripeCheckout, passing our publishable key and then we open the stripe payment form passing in our product and amount for the transaction.

After the user enters their credit card details and presses the Pay button, these details will be sent to stripe to initiate the payment. Stripe will then return a token and our callback (takePayment()) will be invoked.

takePayment() will call our ASP.NET Core API (/api/stripepayment) using angular’s http service. This is where the stripe payment will be processed – we’ll implement this in a minute.

The status from the /api/stripepayment API response is put in takePaymentResult which will be presented in the UI by angular.


OK, on to our web API that processes the stripe payment …

First, we need to bring in stripe.net via nuget – it makes processing the payment very simple.

Below is the full API controller and the API request model, StripePaymentRequest

First we tell stripe our secret key (line 7). We then setup the stripe charge object, setting the token, amount and description (to the product name). We also set our payment reference in the MetaData dictionary so that we tie the payment in our app to the payment in stripe.

The charge is then made and the result (which includes whether the payment has been successful) is passed back down in the response.

Test it!

You can test different scenarios using stripe’s test credit card numbers here.

Recommended reading for learning more about making Angular and ASP.NET core work well together:

8 Reasons to use TypeScript

I’ve been using TypeScript again whilst using Angular 2. I dismissed TypeScript a few years ago because the tooling surrounding it was a bit too painful and I didn’t think it would gain traction. Things have improved a lot. Here’s why I think it is worth considering using TypeScript – particularly when writing decent sized web apps …

1. Readability

The TypeScript syntax is really nice – I find it easier to read than the equivalent JavaScript. This can have a significant impact on the maintenance cost with large codebases with large teams. Here’s some sample TypeScript on the left and the equivalent JavaScript on the right:

You can see that the TypeScript is much cleaner – no IFFE to mimic a class or “prototype” references when defining functions, …

2. ES6 in older browsers

TypeScript lets you use ES6 features today in older browsers because TypeScript can compile to ES5. Features like classes in the above example, arrow functions in the example allow you to write cleaner code …

3. IntelliSense

IntelliSense allows you to understand APIs quicker. Again this can improve productivity in large codebases.

4. Familiar Object Oriented style

The TypeScript syntax is very similar C# and Java. So, if you have a lot of experience in these languages, you will be right at home with TypeScript.

5. Catch errors quicker

TypeScript can catch errors as you write your code. A common mistake I make with JavaScript is typing out the wrong property or function name when consuming an API. Without TypeScript, these errors are caught when you run the code or when you write a unit test. With TypeScript, these errors are caught as you type the incorrect code. Again, this improves productivity.

6. Not all or nothing

The migration to TypeScript can be gradual – you don’t have to migrate every line of code from JavaScript to TypeScript before releasing. You can migrate the short, simple files that are referenced a lot first, to start gaining a good level of benefit. You don’t have to migrate all areas of code to TypeScript as well – there are cases when strong typing adds a lot of friction.

7. Traction

Looking at Google Trends, TypeScript has continued to gain traction in the last couple of years since release. Angular 2’s dependency on TypeScript will mean that traction will continue to grow – Angular is hugely popular.

8. Mature

TypeScript was released to the general public towards the end of 2012 after 2 years of internal development at Microsoft.
So, there’s been lots of development following feedback in the last couple of years. The tooling surrounding TypeScript is now much better and WebStorm and Visual Studio Code now give excellent experiences now when writing TypeScript code.

Recommended reading for building great TypeScript apps:

Connect with me:RSSGitHubTwitterLinkedIn

Building a simple component in React.js – Part 1 – Intro and Basic UI

In the next 3 posts, we’re going to build search and results components using React.js. The components will allow you to search for a company name and the matching names will be displayed beneath the search box.

The components will look like below. There will be 3 components:

  1. Criteria. The search textbox and “go” button
  2. Results. The list of results
  3. Search. The overall component – the parent of Criteria and Results

Consuming page

The html of the consuming page is listed below.

  • Line 10 is a placeholder for where the components will be injected
  • Line 12 is a reference to React.js
  • Line 13 is a reference to the React library that will transform the JSX in our components into valid HTML / JS
  • Line 15 is a placeholder for where we are going to write our React components

Creating our 3 basic components

You create a component in React.js by calling React.createClass(). The key function within a React component, that you must implement, is render() which should return the JSX for the component – JSX is a lot like HTML.

In the code below, we create our 3 component classes, returning the basic JSX we need.

  • Line 24 defines the outer component referencing the sub components, Criteria and Results
  • Line 2 defines the Criteria component
  • Line 13 defines the Results component with some hardcoded results at this point
  • Line 35 is where we tell React to inject the outer component, Search, into the DOM node with id “app”

In subsequent parts we will build on these component implementations …