Skip to content

Commit e64b4e1

Browse files
committed
tournament image done
1 parent 67131c4 commit e64b4e1

File tree

3 files changed

+58
-5
lines changed

3 files changed

+58
-5
lines changed

frontend/src/app/components/tournament/tournament.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,9 +556,9 @@ <h3 class="tournament-bracket__round-title">Final</h3>
556556

557557
<img *ngIf="tournament.image" src="/api/tournaments/{{tournament.id}}/image"><br>
558558
<img *ngIf="!tournament.image" src="../../../assets/images/product_02.jpg">
559-
<input *ngIf="tournament.image" type='checkbox' name='removeImage'> <label>Remove image</label><br>
559+
<input *ngIf="tournament.image" type='checkbox' name='removeImage' [(ngModel)]="removeImage"> <label>Remove image</label><br>
560560

561-
<input type='file' name='imageField' accept=".jpg, .jpeg">
561+
<input #file type='file' name='imageField' accept=".jpg, .jpeg">
562562

563563
</div>
564564
</div>

frontend/src/app/components/tournament/tournament.component.ts

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Team } from './../../models/team.model';
33
import { User } from './../../models/user.model';
44
import { Tournament } from './../../models/tournament.model';
55
import { TournamentService } from 'src/app/services/tournament.service';
6-
import { Component } from '@angular/core';
6+
import { Component, ViewChild } from '@angular/core';
77
import { ActivatedRoute, Router } from '@angular/router';
88
import { LoginService } from 'src/app/services/login.service';
99
import { UserService } from 'src/app/services/user.service';
@@ -31,6 +31,11 @@ export class TournamentComponent{
3131
hasMoreUsers: boolean = true;
3232
usersList: User[] = [];
3333

34+
@ViewChild("file")
35+
file: any;
36+
37+
removeImage:boolean;
38+
3439

3540
constructor(private router: Router, activatedRoute: ActivatedRoute, public tournamentService: TournamentService,
3641
public userService: UserService, public loginService: LoginService) {
@@ -205,14 +210,48 @@ export class TournamentComponent{
205210
startDate: newstartDate,
206211
started: false,
207212
//BE CAREFULL WITH THIS BOOLEAN WHEN UPDATED A IMG (NOT DONE YET)
208-
image: false
213+
image: this.tournament.image
209214
}
210215

211216
this.tournamentService.updateTournament(updatedTournanent).subscribe(
212-
_ => this.getTournamentInit(this.id)
217+
response => { this.uploadImage();
218+
this.getTournamentInit(this.id)
219+
},
220+
error => {
221+
if (error.status != 400) {
222+
console.error('Unexpected Error on deleteUser')
223+
}
224+
}
213225
);
214226
}
215227

228+
uploadImage(): void {
229+
230+
const image = this.file.nativeElement.files[0];
231+
if (image) {
232+
let formData = new FormData();
233+
formData.append("imageFile", image);
234+
this.tournamentService.setTournamentImage(this.id,formData).subscribe(
235+
_ => {this.afterUploadImage();this.getTournamentInit(this.id)},
236+
error => alert('Error uploading user image: ' + error)
237+
);
238+
} else if(this.removeImage){
239+
this.tournamentService.deleteTournamentImage(this.id).subscribe(
240+
_ => {this.afterUploadImage();
241+
this.getTournamentInit(this.id)},
242+
error => alert('Error deleting user image: ' + error)
243+
);
244+
} else {
245+
console.log("Entramos en else")
246+
this.afterUploadImage();
247+
this.getTournamentInit(this.id);
248+
}
249+
}
250+
251+
private afterUploadImage(){
252+
this.router.navigate(['/tournament/'+this.id]);
253+
}
254+
216255
private formatDateReverse(date: string): string {
217256
let splitted1 = date.split(' ');
218257
let splitted2 = splitted1[1].split('/');

frontend/src/app/services/tournament.service.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,20 @@ export class TournamentService {
8585
))
8686
}
8787

88+
setTournamentImage(id:number, formData: FormData) {
89+
return this.http.post(BASE_URL +id+ '/image', formData)
90+
.pipe(
91+
catchError(error => this.handleError(error))
92+
);
93+
}
94+
95+
deleteTournamentImage(id:number){
96+
return this.http.delete(BASE_URL +id+ '/image')
97+
.pipe(
98+
catchError(error => this.handleError(error))
99+
);
100+
}
101+
88102
private handleError(error: any) {
89103
console.log("ERROR:");
90104
console.error(error);

0 commit comments

Comments
 (0)