I am starting with the Angular and I need some support. I have watched some of the tutorials and lot of them are using things differently. I upgraded some tutorial with my own idea, but I am not sure is this right way to do apps or not.
If you can take a look also at pagination and solution, not sure that is the best way to do it. What can I improve in code, what to focus on next?
Git link on full project Git repo
component.ts
import { Observable } from 'rxjs';
import { Router } from '@angular/router';
import { IContact } from './../../models/icontact';
import { Component, OnChanges, OnInit, SimpleChanges } from '@angular/core';
import { ContactService } from 'src/app/services/contact.service';
@Component({
selector: 'app-contact-manager',
templateUrl: './contact-manager.component.html',
styleUrls: ['./contact-manager.component.scss'],
})
export class ContactManagerComponent implements OnInit {
public loading: boolean = false;
public contacts: IContact[] = [];
public loadedContacts: IContact[] = [];
public errorMessage: string | null = null;
public contactsFiltered: IContact[] = [];
public searchString: string = '';
public currentPage = 1;
public itemsPerPage = 6;
public numberOfContacts = 0;
public numberOfContactsInDB = 0;
public totalPages = 0;
public disablePrevous = true;
public disableNext = true;
public contacts$: Observable<IContact[]> = {} as Observable<IContact[]>;
constructor(private contactService: ContactService, public router: Router) {}
ngOnInit(): void {
this.getContactsPaginated();
}
public getContactsPaginated() {
this.contactService
.getContactsPaginated(this.currentPage, this.itemsPerPage)
.subscribe({
next: (response) => {
this.numberOfContactsInDB = +response.headers.get('X-Total-Count')!;
this.contacts = response.body!;
this.loadedContacts = response.body!;
this.contactsFiltered = response.body!;
this.calculatePages();
this.changePageButton();
},
});
}
submitDeleteContact(contactId: string) {
this.contactService.deleteContact(contactId).subscribe({
next: (data) => {
this.router.navigateByUrl('/');
this.getContactsPaginated();
},
error: (error) => {
this.errorMessage = error;
},
});
}
searchUser(event: KeyboardEvent) {
if (this.searchString == '') {
this.getContactsPaginated();
} else {
this.contactService
.searchByName(this.searchString, this.currentPage, this.itemsPerPage)
.subscribe((data) => {
this.numberOfContactsInDB = +data.headers.get('X-Total-Count')!;
this.contacts = data.body!;
this.contactsFiltered = data.body!;
this.loading = false;
this.calculatePages();
this.changePageButton();
});
this.changePage(1);
}
}
changePage(page: number) {
this.currentPage = page;
this.changePageButton();
this.calculatePages();
this.contactService
.searchByName(this.searchString, page, this.itemsPerPage)
.subscribe((data) => {
this.contacts = data.body!;
this.contactsFiltered = data.body!;
this.loading = false;
this.calculatePages();
this.changePageButton();
});
}
changePageButton() {
if (this.currentPage == 1) {
this.disablePrevous = true;
} else {
this.disablePrevous = false;
}
if (this.currentPage == this.totalPages) {
this.disableNext = true;
} else {
this.disableNext = false;
}
}
calculatePages() {
this.numberOfContacts = this.numberOfContactsInDB;
this.totalPages = Math.ceil(this.numberOfContacts / this.itemsPerPage);
}
}
service.ts
import { IContact } from './../models/icontact';
import {
HttpClient,
HttpErrorResponse,
HttpHeaders,
HttpResponse,
} from '@angular/common/http';
import { Injectable } from '@angular/core';
import { catchError, map, Observable, throwError } from 'rxjs';
import { IGroup } from '../models/igroup';
@Injectable({
providedIn: 'root',
})
export class ContactService {
private serverURL: string = 'http://localhost:9000';
constructor(private httpClient: HttpClient) {}
public getAllContacts(): Observable<IContact[]> {
let dataURL: string = `${this.serverURL}/contacts`;
return this.httpClient
.get<IContact[]>(dataURL)
.pipe(catchError(this.handleError));
}
public getContactsPaginated(page: number, limit: number) {
let dataURL: string = `${this.serverURL}/contacts?_page=${page}&_limit=${limit}`;
return this.httpClient
.get<IContact[]>(dataURL, { observe: 'response' })
.pipe(catchError(this.handleError));
}
public getContact(contactId: string): Observable<IContact> {
let dataURL: string = `${this.serverURL}/contacts/${contactId}`;
return this.httpClient
.get<IContact>(dataURL)
.pipe(catchError(this.handleError));
}
public createContact(contact: IContact): Observable<IContact> {
let dataURL: string = `${this.serverURL}/contacts`;
return this.httpClient
.post<IContact>(dataURL, contact)
.pipe(catchError(this.handleError));
}
public updateContact(
contact: IContact,
contactId: string
): Observable<IContact> {
let dataURL: string = `${this.serverURL}/contacts/${contactId}`;
return this.httpClient
.put<IContact>(dataURL, contact)
.pipe(catchError(this.handleError));
}
public deleteContact(contactId: string): Observable<{}> {
let dataURL: string = `${this.serverURL}/contacts/${contactId}`;
return this.httpClient
.delete<{}>(dataURL)
.pipe(catchError(this.handleError));
}
public getAllGroups(): Observable<IGroup[]> {
let dataURL: string = `${this.serverURL}/groups`;
return this.httpClient
.get<IGroup[]>(dataURL)
.pipe(catchError(this.handleError));
}
public getGroup(contact: IContact): Observable<IGroup> {
let dataURL: string = `${this.serverURL}/groups/${contact.groupId}`;
return this.httpClient
.get<IGroup>(dataURL)
.pipe(catchError(this.handleError));
}
public searchByName(searchName: string, page: number, limit: number) {
let dataURL: string = `${this.serverURL}/contacts?name_like=${searchName}&_limit=6&_page=${page}&_limit=${limit}`;
return this.httpClient
.get<IContact[]>(dataURL, { observe: 'response' })
.pipe(catchError(this.handleError));
}
public handleError(error: HttpErrorResponse) {
let errorMessage: string = '';
if (error.error instanceof ErrorEvent) {
errorMessage = 'Client error ' + error.error.message;
} else {
errorMessage = 'Server error ' + error.error.message + ' ' + error.status;
}
return throwError(errorMessage);
}
getAllContactsObservableWay(): Observable<any> {
return this.httpClient.get(this.serverURL).pipe();
}
}