Created
August 15, 2017 10:42
-
-
Save AlbertFX91/fc824e62365c3e036e83f067bf8744e5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit, OnDestroy } from '@angular/core'; | |
import { Router, ActivatedRoute } from '@angular/router'; | |
import { Response } from '@angular/http'; | |
import { Observable } from 'rxjs/Rx'; | |
import { JhiEventManager, JhiAlertService } from 'ng-jhipster'; | |
import { Game } from './game.model'; | |
import { GameService } from './game.service'; | |
@Component({ | |
selector: 'jhi-game-form', | |
templateUrl: './game-form.component.html' | |
}) | |
export class GameFormComponent implements OnInit, OnDestroy { | |
game: Game = {}; | |
isSaving: boolean; | |
creationDateDp: any; | |
constructor( | |
private alertService: JhiAlertService, | |
private gameService: GameService, | |
private eventManager: JhiEventManager, | |
private router: Router, | |
private activatedRoute: ActivatedRoute | |
) { | |
} | |
ngOnInit() { | |
this.activatedRoute.params.subscribe((params) => { | |
if ( params['id'] ) { | |
this.load(params['id']); | |
} | |
}); | |
this.isSaving = false; | |
} | |
ngOnDestroy(){ | |
} | |
load(id: string) { | |
this.gameService.find(id).subscribe((game) => { | |
this.game = game; | |
// This is used because the ngDatePicker works with this structure | |
this.game.creationDate = { | |
year: game.creationDate.getFullYear(), | |
month: game.creationDate.getMonth() + 1, | |
day: game.creationDate.getDate() | |
}; | |
}); | |
} | |
clear() { | |
this.router.navigate(['/game']) | |
} | |
save() { | |
this.isSaving = true; | |
if (this.game.id !== undefined) { | |
this.subscribeToSaveResponse( | |
this.gameService.update(this.game)); | |
} else { | |
this.subscribeToSaveResponse( | |
this.gameService.create(this.game)); | |
} | |
} | |
private subscribeToSaveResponse(result: Observable<Game>) { | |
result.subscribe((res: Game) => | |
this.onSaveSuccess(res), (res: Response) => this.onSaveError(res)); | |
} | |
private onSaveSuccess(result: Game) { | |
this.eventManager.broadcast({ name: 'gameListModification', content: 'OK'}); | |
this.isSaving = false; | |
this.router.navigate(['/game']) | |
} | |
private onSaveError(error) { | |
try { | |
error.json(); | |
} catch (exception) { | |
error.message = error.text(); | |
} | |
this.isSaving = false; | |
this.onError(error); | |
} | |
private onError(error) { | |
this.alertService.error(error.message, null, null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment