52 lines
1.8 KiB
TypeScript
52 lines
1.8 KiB
TypeScript
import { Injectable } from '@angular/core';
|
|
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
|
|
|
|
import { Observable, Subject, throwError, of } from 'rxjs';
|
|
import { catchError, map, tap, first } from 'rxjs/operators';
|
|
|
|
import { LOGIN_URL, USER_CREATE_URL } from '../constants/urls';
|
|
|
|
@Injectable()
|
|
export class UserService {
|
|
|
|
private options: {};
|
|
|
|
constructor(private httpClient: HttpClient){
|
|
this.options = {
|
|
withCredentials: true
|
|
};
|
|
}
|
|
|
|
create(firstName: string, lastName: string, street: string, city: string, state: string, zip: string, country: string) {
|
|
const body = {
|
|
firstName: firstName,
|
|
lastName: lastName,
|
|
street: street,
|
|
city: city,
|
|
state: state,
|
|
zip: zip,
|
|
country: country
|
|
};
|
|
console.log(body);
|
|
return this.httpClient.post(USER_CREATE_URL, body, this.options)
|
|
.pipe(tap(res => console.log(res)), catchError(this.handleError));
|
|
}
|
|
|
|
private handleError(error: HttpErrorResponse) {
|
|
if (error.error instanceof ErrorEvent) {
|
|
// A client-side or network error occurred. Handle it accordingly.
|
|
console.error('An error occurred:', error.error.message);
|
|
} else {
|
|
// The backend returned an unsuccessful response code.
|
|
// The response body may contain clues as to what went wrong,
|
|
console.error(
|
|
`Backend returned code ${error.status}, ` +
|
|
`body was: ${error.error}`);
|
|
console.error(error);
|
|
}
|
|
// return an observable with a user-facing error message
|
|
return throwError((error && error.error && error.error.message) ? error.error.message :
|
|
'Something bad happened; please try again later.');
|
|
};
|
|
}
|