LiveLoveApp logo

Exercise - ThrowError Operator

Exercise

  1. Open exercise on codesandbox. I've already imported the necessary functions from rxjs for you, and provided constant variables referencing the DOM elements in the template.
  2. To start, use the fromEvent() operator to add an event listener to the login form's submit event.
  3. We'll want to prevent the default behavior of submitting the form, so use the tap() operator to execute the side effect of invoking the preventDefault() method on the SubmitEvent object.
  4. Within a mergeMap() check for the validity of the email and password values. For simplicity, just assert their truthiness. If the values are falsey use the throwError() operator to return a new Observable that immediately emits an error notification, providing either the Error object or a factory function. If the values are truthy use the ajax.post() method to post the email and password values to the API.

Note, I've provided a hint for POSTing the login to the API that includes the URL to use:

ajax.post('https://reqres.in/api/login', {
  email: email.value,
  password: password.value
})