Skip to content

Commit

Permalink
feat: #46 auto game data save with erorr handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pablitoo1 committed Sep 18, 2024
1 parent c0a42b4 commit c3f1ed2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import { Component, EventEmitter, inject, Input, Output } from '@angular/core';
import { GameRecordEndpointsService } from '@endpoints/game-record-endpoints.service';
import { TExchangeData } from '@gameModels/exchange-data.type';
import { IRecordedGameRequest } from 'app/shared/models/recorded-game.models';
import { NotificationService } from 'app/shared/services/notification.service';

@Component({
selector: 'app-data-download',
standalone: true,
template: `<div class="flex flex-col">
<button
class="font-bold mt-2 border-b-[1px] border-mainOrange w-full text-center"
(click)="vIsDataCollectingActive.value = !vIsDataCollectingActive.value">
(click)="handleCollectingData()">
@if (!vIsDataCollectingActive.value) {
Start collecting data
} @else {
Expand All @@ -22,11 +23,6 @@ import { IRecordedGameRequest } from 'app/shared/models/recorded-game.models';
class="mt-4 py-1 text-center text-mainCreme border-mainCreme border-[1px] hover:bg-mainCreme hover:text-darkGray transition-all ease-in-out duration-300">
Download JSON ({{ collectedDataArray.length }} records)
</button>
<button
(click)="sendData()"
class="mt-3 py-1 text-blue-900 border-blue-900 border-[1px] hover:bg-blue-900 hover:text-mainCreme transition-all ease-in-out duration-300">
Save data
</button>
<button
(click)="deleteCollectedData()"
class="mt-3 py-1 text-center font-bold text-red-500 border-red-500 border-[1px] hover:bg-red-500 hover:text-darkGray transition-all ease-in-out duration-300">
Expand All @@ -43,14 +39,29 @@ export class DataDownloadComponent {
@Output() public deleteCollectedDataArrayEmitter = new EventEmitter<void>();

private _gameRecordEndpointsService = inject(GameRecordEndpointsService);
private _notificationService = inject(NotificationService);

public sendData(): void {
const gameRecordData: IRecordedGameRequest[] = [
{ gameName: this.gameName, values: this.collectedDataArray },
];
this._gameRecordEndpointsService
.addGameRecording(gameRecordData)
.subscribe(r => console.log(r)); //todo
public handleCollectingData(): void {
this.vIsDataCollectingActive.value = !this.vIsDataCollectingActive.value;

if (!this.vIsDataCollectingActive.value) {
const gameRecordData: IRecordedGameRequest = {
gameName: this.gameName,
values: this.collectedDataArray,
};
this._gameRecordEndpointsService
.addGameRecording(gameRecordData)
.subscribe({
next: () => {
this._notificationService.addNotification(
'Game record data has been saved correctly'
);
},
error: (error: string) => {
this._notificationService.addNotification(error);
},
});
}
}

public generateJSON(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class GameRecordEndpointsService {
}

public addGameRecording(
gameRecordData: IRecordedGameRequest[]
gameRecordData: IRecordedGameRequest
): Observable<void> {
return this._httpClient
.post<void>(
Expand Down

0 comments on commit c3f1ed2

Please sign in to comment.