@@ -3,7 +3,7 @@ import { Team } from './../../models/team.model';
3
3
import { User } from './../../models/user.model' ;
4
4
import { Tournament } from './../../models/tournament.model' ;
5
5
import { TournamentService } from 'src/app/services/tournament.service' ;
6
- import { Component } from '@angular/core' ;
6
+ import { Component , ViewChild } from '@angular/core' ;
7
7
import { ActivatedRoute , Router } from '@angular/router' ;
8
8
import { LoginService } from 'src/app/services/login.service' ;
9
9
import { UserService } from 'src/app/services/user.service' ;
@@ -31,6 +31,11 @@ export class TournamentComponent{
31
31
hasMoreUsers : boolean = true ;
32
32
usersList : User [ ] = [ ] ;
33
33
34
+ @ViewChild ( "file" )
35
+ file : any ;
36
+
37
+ removeImage :boolean ;
38
+
34
39
35
40
constructor ( private router : Router , activatedRoute : ActivatedRoute , public tournamentService : TournamentService ,
36
41
public userService : UserService , public loginService : LoginService ) {
@@ -205,14 +210,48 @@ export class TournamentComponent{
205
210
startDate : newstartDate ,
206
211
started : false ,
207
212
//BE CAREFULL WITH THIS BOOLEAN WHEN UPDATED A IMG (NOT DONE YET)
208
- image : false
213
+ image : this . tournament . image
209
214
}
210
215
211
216
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
+ }
213
225
) ;
214
226
}
215
227
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
+
216
255
private formatDateReverse ( date : string ) : string {
217
256
let splitted1 = date . split ( ' ' ) ;
218
257
let splitted2 = splitted1 [ 1 ] . split ( '/' ) ;
0 commit comments