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 thelogin
form'ssubmit
event. - 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 theSubmitEvent
object. - Within a
mergeMap()
check for the validity of theemail
andpassword
values. 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 theError
object or a factory function. If the values are truthy use theajax.post()
method to post theemail
andpassword
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
})