diff --git a/src/app/redemptions/redemption-list/redemption-list.component.ts b/src/app/redemptions/redemption-list/redemption-list.component.ts index 4ec13d7..a7a9e97 100644 --- a/src/app/redemptions/redemption-list/redemption-list.component.ts +++ b/src/app/redemptions/redemption-list/redemption-list.component.ts @@ -36,7 +36,7 @@ import { HermesClientService } from '../../hermes-client.service'; templateUrl: './redemption-list.component.html', styleUrl: './redemption-list.component.scss' }) -export class RedemptionListComponent implements OnInit, OnDestroy { +export class RedemptionListComponent implements OnDestroy { private readonly client = inject(HermesClientService); private readonly redemptionService = inject(RedemptionService); private readonly route = inject(ActivatedRoute); @@ -58,56 +58,52 @@ export class RedemptionListComponent implements OnInit, OnDestroy { this._twitchRedemptionsDict = Object.assign({}, ...r['twitchRedemptions'].map((t: TwitchRedemption) => ({ [t.id]: t.title }))); this._actions = r['redeemableActions']; - let redemptions = r['redemptions']; + let redemptions = [...r['redemptions']]; redemptions.sort((a: Redemption, b: Redemption) => this.compare(a, b)); this._redemptions = redemptions; - - let subscription = this.redemptionService.create$?.subscribe(d => { - if (d.error || !d.data || d.request.nounce != null && d.request.nounce.startsWith(this.client.session_id)) { - return; - } - - let index = -1; - for (let i = 0; i < this._redemptions.length; i++) { - const comp = this.compare(d.data, this._redemptions[i]); - if (comp < 0) { - index = i; - break; - } - } - this._redemptions.splice(index >= 0 ? index : this._redemptions.length, 0, d.data); - }); - if (subscription) - this._subscriptions.push(subscription); - - subscription = this.redemptionService.update$?.subscribe(d => { - if (d.error || !d.data || d.request.nounce != null && d.request.nounce.startsWith(this.client.session_id)) - return; - - const redemption = this._redemptions.find(r => r.id = d.data.id); - if (redemption) { - redemption.action_name = d.data.action_name; - redemption.redemption_id = d.data.redemption_id; - redemption.order = d.data.order; - redemption.state = d.data.state; - } - }); - if (subscription) - this._subscriptions.push(subscription); - - subscription = this.redemptionService.delete$?.subscribe(d => { - if (d.error || d.request.nounce != null && d.request.nounce.startsWith(this.client.session_id)) - return; - - this._redemptions = this._redemptions.filter(r => r.id != d.request.data.id); - }); - if (subscription) - this._subscriptions.push(subscription); }); - } - ngOnInit(): void { + let subscription = this.redemptionService.create$?.subscribe(d => { + if (d.error || !d.data || d.request.nounce != null && d.request.nounce.startsWith(this.client.session_id)) { + return; + } + let index = -1; + for (let i = 0; i < this._redemptions.length; i++) { + const comp = this.compare(d.data, this._redemptions[i]); + if (comp < 0) { + index = i; + break; + } + } + this._redemptions.splice(index >= 0 ? index : this._redemptions.length, 0, d.data); + }); + if (subscription) + this._subscriptions.push(subscription); + + subscription = this.redemptionService.update$?.subscribe(d => { + if (d.error || !d.data || d.request.nounce != null && d.request.nounce.startsWith(this.client.session_id)) + return; + + const redemption = this._redemptions.find(r => r.id = d.data.id); + if (redemption) { + redemption.action_name = d.data.action_name; + redemption.redemption_id = d.data.redemption_id; + redemption.order = d.data.order; + redemption.state = d.data.state; + } + }); + if (subscription) + this._subscriptions.push(subscription); + + subscription = this.redemptionService.delete$?.subscribe(d => { + if (d.error || d.request.nounce != null && d.request.nounce.startsWith(this.client.session_id)) + return; + + this._redemptions = this._redemptions.filter(r => r.id != d.request.data.id); + }); + if (subscription) + this._subscriptions.push(subscription); } ngOnDestroy(): void { diff --git a/src/app/shared/services/redemption.service.ts b/src/app/shared/services/redemption.service.ts index 863a9fb..21956b2 100644 --- a/src/app/shared/services/redemption.service.ts +++ b/src/app/shared/services/redemption.service.ts @@ -10,7 +10,6 @@ export class RedemptionService { private client = inject(HermesClientService); private data: Redemption[] = [] create$: Observable | undefined; - fetch$: Observable | undefined; update$: Observable | undefined; delete$: Observable | undefined; loaded = false; @@ -19,6 +18,18 @@ export class RedemptionService { this.create$ = this.client.filterByRequestType('create_redemption'); this.update$ = this.client.filterByRequestType('update_redemption'); this.delete$ = this.client.filterByRequestType('delete_redemption'); + + this.create$?.subscribe(d => this.data.push(d.data)); + this.update$?.subscribe(d => { + const redemption = this.data.find(r => r.id == d.data.id); + if (redemption) { + redemption.action_name = d.data.action_name; + redemption.redemption_id = d.data.redemption_id; + redemption.order = d.data.order; + redemption.state = d.data.state; + } + }); + this.delete$?.subscribe(d => this.data = this.data.filter(r => r.id != d.request.data.id)); }