⚡ Improve service
This commit is contained in:
parent
13284f989d
commit
3cd25790e3
@ -1,13 +1,7 @@
|
|||||||
import { inject, Injectable } from "@angular/core";
|
import { inject, Injectable } from "@angular/core";
|
||||||
import { HttpClient } from "@angular/common/http";
|
import { HttpClient } from "@angular/common/http";
|
||||||
import { Category, Place } from "../types/poi";
|
import { Category, Place } from "../types/poi";
|
||||||
import {
|
import { BehaviorSubject, Observable, tap } from "rxjs";
|
||||||
BehaviorSubject,
|
|
||||||
distinctUntilChanged,
|
|
||||||
Observable,
|
|
||||||
shareReplay,
|
|
||||||
tap,
|
|
||||||
} from "rxjs";
|
|
||||||
import { Info } from "../types/info";
|
import { Info } from "../types/info";
|
||||||
import { Settings } from "../types/settings";
|
import { Settings } from "../types/settings";
|
||||||
import { Trip, TripBase, TripDay, TripItem } from "../types/trip";
|
import { Trip, TripBase, TripDay, TripItem } from "../types/trip";
|
||||||
@ -16,7 +10,7 @@ import { Trip, TripBase, TripDay, TripItem } from "../types/trip";
|
|||||||
providedIn: "root",
|
providedIn: "root",
|
||||||
})
|
})
|
||||||
export class ApiService {
|
export class ApiService {
|
||||||
public apiBaseUrl: string = "/api";
|
public readonly apiBaseUrl: string = "/api";
|
||||||
|
|
||||||
private categoriesSubject = new BehaviorSubject<Category[] | null>(null);
|
private categoriesSubject = new BehaviorSubject<Category[] | null>(null);
|
||||||
public categories$: Observable<Category[] | null> =
|
public categories$: Observable<Category[] | null> =
|
||||||
@ -33,7 +27,7 @@ export class ApiService {
|
|||||||
|
|
||||||
_categoriesSubjectNext(categories: Category[]) {
|
_categoriesSubjectNext(categories: Category[]) {
|
||||||
this.categoriesSubject.next(
|
this.categoriesSubject.next(
|
||||||
categories.sort((categoryA: Category, categoryB: Category) =>
|
[...categories].sort((categoryA: Category, categoryB: Category) =>
|
||||||
categoryA.name.localeCompare(categoryB.name),
|
categoryA.name.localeCompare(categoryB.name),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@ -43,11 +37,7 @@ export class ApiService {
|
|||||||
if (!this.categoriesSubject.value) {
|
if (!this.categoriesSubject.value) {
|
||||||
return this.httpClient
|
return this.httpClient
|
||||||
.get<Category[]>(`${this.apiBaseUrl}/categories`)
|
.get<Category[]>(`${this.apiBaseUrl}/categories`)
|
||||||
.pipe(
|
.pipe(tap((categories) => this._categoriesSubjectNext(categories)));
|
||||||
tap((categories) => this._categoriesSubjectNext(categories)),
|
|
||||||
distinctUntilChanged(),
|
|
||||||
shareReplay(),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
return this.categories$ as Observable<Category[]>;
|
return this.categories$ as Observable<Category[]>;
|
||||||
}
|
}
|
||||||
@ -70,11 +60,12 @@ export class ApiService {
|
|||||||
.put<Category>(this.apiBaseUrl + `/categories/${c_id}`, c)
|
.put<Category>(this.apiBaseUrl + `/categories/${c_id}`, c)
|
||||||
.pipe(
|
.pipe(
|
||||||
tap((category) => {
|
tap((category) => {
|
||||||
let categories = this.categoriesSubject.value || [];
|
const categories = this.categoriesSubject.value || [];
|
||||||
let categoryIndex = categories?.findIndex((c) => c.id == c_id) || -1;
|
const idx = categories?.findIndex((c) => c.id == c_id) || -1;
|
||||||
if (categoryIndex > -1) {
|
if (idx > -1) {
|
||||||
categories[categoryIndex] = category;
|
const updated = [...categories];
|
||||||
this._categoriesSubjectNext(categories);
|
updated[idx] = category;
|
||||||
|
this._categoriesSubjectNext(updated);
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
@ -84,22 +75,19 @@ export class ApiService {
|
|||||||
return this.httpClient
|
return this.httpClient
|
||||||
.delete<{}>(this.apiBaseUrl + `/categories/${category_id}`)
|
.delete<{}>(this.apiBaseUrl + `/categories/${category_id}`)
|
||||||
.pipe(
|
.pipe(
|
||||||
tap((_) => {
|
tap(() => {
|
||||||
let categories = this.categoriesSubject.value || [];
|
const categories = this.categoriesSubject.value || [];
|
||||||
let categoryIndex =
|
const idx = categories?.findIndex((c) => c.id == category_id) || -1;
|
||||||
categories?.findIndex((c) => c.id == category_id) || -1;
|
if (idx > -1) {
|
||||||
if (categoryIndex > -1) {
|
const updated = categories.filter((_, i) => i != idx);
|
||||||
categories.splice(categoryIndex, 1);
|
this._categoriesSubjectNext(updated);
|
||||||
this._categoriesSubjectNext(categories);
|
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
getPlaces(): Observable<Place[]> {
|
getPlaces(): Observable<Place[]> {
|
||||||
return this.httpClient
|
return this.httpClient.get<Place[]>(`${this.apiBaseUrl}/places`);
|
||||||
.get<Place[]>(`${this.apiBaseUrl}/places`)
|
|
||||||
.pipe(distinctUntilChanged(), shareReplay());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
postPlace(place: Place): Observable<Place> {
|
postPlace(place: Place): Observable<Place> {
|
||||||
@ -131,15 +119,11 @@ export class ApiService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getTrips(): Observable<TripBase[]> {
|
getTrips(): Observable<TripBase[]> {
|
||||||
return this.httpClient
|
return this.httpClient.get<TripBase[]>(`${this.apiBaseUrl}/trips`);
|
||||||
.get<TripBase[]>(`${this.apiBaseUrl}/trips`)
|
|
||||||
.pipe(distinctUntilChanged(), shareReplay());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getTrip(id: number): Observable<Trip> {
|
getTrip(id: number): Observable<Trip> {
|
||||||
return this.httpClient
|
return this.httpClient.get<Trip>(`${this.apiBaseUrl}/trips/${id}`);
|
||||||
.get<Trip>(`${this.apiBaseUrl}/trips/${id}`)
|
|
||||||
.pipe(distinctUntilChanged(), shareReplay());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
postTrip(trip: TripBase): Observable<TripBase> {
|
postTrip(trip: TripBase): Observable<TripBase> {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user