Exercise - ThrowError Operator
Exercise
- 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.
- To start, use the
fromEvent()operator to add an event listener to theloginform'ssubmitevent. - We'll want to prevent the default behavior of submitting the form, so use the
tap()operator to execute the side effect of invoking thepreventDefault()method on theSubmitEventobject. - Within a
mergeMap()check for the validity of theemailandpasswordvalues. For simplicity, just assert their truthiness. If the values are falsey use thethrowError()operator to return a new Observable that immediately emits an error notification, providing either theErrorobject or a factory function. If the values are truthy use theajax.post()method to post theemailandpasswordvalues 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
})