43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
import { HttpClient } from '@angular/common/http';
|
|
import { inject, Injectable } from '@angular/core';
|
|
import { environment } from '../../../environments/environment';
|
|
import TwitchRedemption from '../models/twitch-redemption';
|
|
import { catchError, EMPTY, of } from 'rxjs';
|
|
import EventService from './EventService';
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export default class TwitchRedemptionService {
|
|
private readonly http = inject(HttpClient);
|
|
private readonly events = inject(EventService);
|
|
|
|
private twitchRedemptions: TwitchRedemption[] = [];
|
|
private loaded = false;
|
|
|
|
|
|
constructor() {
|
|
this.events.listen('logoff', () => {
|
|
this.twitchRedemptions = [];
|
|
this.loaded = false;
|
|
});
|
|
}
|
|
|
|
fetch() {
|
|
if (this.loaded)
|
|
return of(this.twitchRedemptions);
|
|
|
|
const $ = this.http.get<TwitchRedemption[]>(environment.API_HOST + '/twitch/redemptions', {
|
|
headers: {
|
|
'Authorization': 'Bearer ' + localStorage.getItem('jwt'),
|
|
}
|
|
});
|
|
$.pipe(catchError(() => EMPTY))
|
|
.subscribe({
|
|
next: d => this.twitchRedemptions = d,
|
|
error: d => console.log('Twitch API redemptions:', d.error),
|
|
complete: () => this.loaded = true,
|
|
});
|
|
return $;
|
|
}
|
|
} |