Mise à jour de composants
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { Component, OnInit, OnDestroy } from '@angular/core';
|
||||
import { Component, AfterContentChecked, OnInit, OnDestroy } from '@angular/core';
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { ActivatedRoute, Router, RouterLink, RouterModule } from '@angular/router';
|
||||
import { MatButtonModule } from '@angular/material/button';
|
||||
@@ -6,16 +6,17 @@ import { MatCardModule } from '@angular/material/card';
|
||||
import { MatDialog, MatDialogModule } from '@angular/material/dialog';
|
||||
import { MatIconModule } from '@angular/material/icon';
|
||||
import { MatMenuModule } from '@angular/material/menu';
|
||||
import { MatSnackBar, MatSnackBarHorizontalPosition, MatSnackBarModule, MatSnackBarVerticalPosition } from '@angular/material/snack-bar';
|
||||
import { MatSnackBar, MatSnackBarConfig, MatSnackBarHorizontalPosition, MatSnackBarModule, MatSnackBarVerticalPosition } from '@angular/material/snack-bar';
|
||||
import { Observable, Subscription } from 'rxjs';
|
||||
import { take } from 'rxjs/operators';
|
||||
|
||||
import { MenuItems, JumpAddDialogComponent, JumpTableComponent } from 'src/app/components/shared';
|
||||
//import { JumpTableComponent } from 'src/app/components/shared/helpers-jump';
|
||||
import { Errors, Jump, JumpListConfig, User } from 'src/app/core/models';
|
||||
import { JumpsService, UserService } from 'src/app/core/services';
|
||||
import { Errors, Jump, JumpAddParams, JumpListConfig, User } from 'src/app/core/models';
|
||||
import { JumpsService, JumpTableService, UserService } from 'src/app/core/services';
|
||||
import { environment } from 'src/environments/environment';
|
||||
import data from 'src/jumps.json';
|
||||
//import data from 'src/jumps_02.json';
|
||||
|
||||
@Component({
|
||||
standalone: true,
|
||||
@@ -29,29 +30,33 @@ import data from 'src/jumps.json';
|
||||
templateUrl: './logbook.component.html',
|
||||
styleUrls: ['./logbook.component.scss']
|
||||
})
|
||||
export class LogbookComponent implements OnInit, OnDestroy {
|
||||
export class LogbookComponent implements OnInit, OnDestroy, AfterContentChecked {
|
||||
private _currentUser: Subscription = new Subscription();
|
||||
private _data: Subscription = new Subscription();
|
||||
private _lastjump: Subscription = new Subscription();
|
||||
private _lastjump$: Observable<Jump> = new Observable();
|
||||
public errors!: Errors;
|
||||
public title = 'Carnet de sauts';
|
||||
public jump: Jump = {} as Jump;
|
||||
public lastjump: Jump = {} as Jump;
|
||||
public lastJump: Jump = {} as Jump;
|
||||
public currentUser: User = {} as User;
|
||||
public listConfig: JumpListConfig = { type: 'all', filters: {} };
|
||||
public refreshInterval: number = environment.refresh_interval;
|
||||
public refresh: boolean = false;
|
||||
public isAuthenticated = false;
|
||||
public isUser = true;
|
||||
public canModify = false;
|
||||
public isSubmitting = false;
|
||||
public tableRefresh = false;
|
||||
public jumpRefresh = false;
|
||||
|
||||
constructor(
|
||||
private route: ActivatedRoute,
|
||||
private router: Router,
|
||||
private dialog: MatDialog,
|
||||
private snackBar: MatSnackBar,
|
||||
private jumpsService: JumpsService,
|
||||
private userService: UserService,
|
||||
private _jumpsService: JumpsService,
|
||||
private _jumpTableService: JumpTableService,
|
||||
private _userService: UserService,
|
||||
public menuItems: MenuItems
|
||||
) { }
|
||||
|
||||
@@ -64,15 +69,15 @@ export class LogbookComponent implements OnInit, OnDestroy {
|
||||
this.router.navigateByUrl('/login');
|
||||
return;
|
||||
}
|
||||
const currentUser$: Observable<User> = this.userService.currentUser;
|
||||
const currentUser$: Observable<User> = this._userService.currentUser;
|
||||
this._currentUser = currentUser$.subscribe((userData: User) => {
|
||||
this.currentUser = userData;
|
||||
this.canModify = this.currentUser.role === 'Admin';
|
||||
});
|
||||
|
||||
const lastjump$: Observable<Jump> = this.jumpsService.getLastJump();
|
||||
this._lastjump = lastjump$.subscribe((jump) => {
|
||||
this.lastjump = jump;
|
||||
this._lastjump$ = this._jumpsService.getLastJump();
|
||||
this._lastjump = this._lastjump$.subscribe((jump) => {
|
||||
this.lastJump = jump;
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -83,12 +88,23 @@ export class LogbookComponent implements OnInit, OnDestroy {
|
||||
this._lastjump.unsubscribe();
|
||||
}
|
||||
|
||||
ngAfterContentChecked() {
|
||||
if (this.tableRefresh != this._jumpTableService.tableRefresh) {
|
||||
this.tableRefresh = this._jumpTableService.tableRefresh;
|
||||
this._lastjump$ = this._jumpsService.getLastJump();
|
||||
this._lastjump = this._lastjump$.subscribe((jump) => {
|
||||
this.lastJump = jump;
|
||||
});
|
||||
console.log('lastjump has been updated in logbook component');
|
||||
}
|
||||
}
|
||||
|
||||
importJumps() {
|
||||
try {
|
||||
data.forEach((entry) => {
|
||||
Object.assign(this.jump, entry);
|
||||
//console.log(this.jump);
|
||||
this.jumpsService.save(this.jump)
|
||||
this._jumpsService.save(this.jump)
|
||||
.pipe(take(1))
|
||||
.subscribe({
|
||||
next: (jump) => {
|
||||
@@ -106,9 +122,13 @@ export class LogbookComponent implements OnInit, OnDestroy {
|
||||
}
|
||||
|
||||
openAddDialog(): void {
|
||||
this.jump = <Jump>{};
|
||||
this.jump.numero = (this.lastJump.numero + 1);
|
||||
this.jump.sautants = [];
|
||||
this.jump.participants = 1;
|
||||
const dialogRef = this.dialog.open(JumpAddDialogComponent, {
|
||||
width: '60vw',
|
||||
data: this.jump
|
||||
width: '70vw',
|
||||
data: {jump: this.jump, lastJump: this.lastJump}
|
||||
});
|
||||
dialogRef.afterClosed().pipe(take(1))
|
||||
.subscribe(result => {
|
||||
@@ -118,35 +138,51 @@ export class LogbookComponent implements OnInit, OnDestroy {
|
||||
});
|
||||
}
|
||||
|
||||
private _onEntry(jumps: Array<Jump>): void {
|
||||
const max: number = jumps.length;
|
||||
let refresh: boolean = false;
|
||||
jumps.forEach((jump: Jump, index: number) => {
|
||||
console.log('Ajout du saut', jump.numero);
|
||||
Object.assign(this.jump, jump);
|
||||
if (max == index+1) {
|
||||
refresh = true;
|
||||
}
|
||||
this._saveJump(jump, refresh);
|
||||
});
|
||||
}
|
||||
|
||||
private _onEntry(entry: NonNullable<unknown>): void {
|
||||
try {
|
||||
this.isSubmitting = true;
|
||||
Object.assign(this.jump, entry);
|
||||
this.jumpsService.save(this.jump)
|
||||
.pipe(take(1))
|
||||
.subscribe({
|
||||
next: (jump) => {
|
||||
this._openSnackBar(`Le saut n°${jump.numero} a été ajouté !`, 'OK');
|
||||
this._resetErrors();
|
||||
//this.runQuery();
|
||||
},
|
||||
error: (err) => {
|
||||
this.errors = err;
|
||||
this.isSubmitting = false;
|
||||
private _saveJump(jump: Jump, refresh: boolean) {
|
||||
//console.log(jump);
|
||||
this._jumpsService.save(jump)
|
||||
.pipe(take(1))
|
||||
.subscribe({
|
||||
next: (jump) => {
|
||||
this._openSnackBar(`Le saut n°${jump.numero} a été ajouté !`, 'Annuler');
|
||||
this._resetErrors();
|
||||
if (refresh) {
|
||||
this.jumpRefresh = !this.jumpRefresh;
|
||||
this._jumpTableService.updateJumpRefresh(this.jumpRefresh);
|
||||
//this.refresh = !this.refresh;
|
||||
this._lastjump$ = this._jumpsService.getLastJump();
|
||||
this._lastjump = this._lastjump$.subscribe((jump) => {
|
||||
this.lastJump = jump;
|
||||
});
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
},
|
||||
error: (err) => {
|
||||
this.errors = err;
|
||||
this.isSubmitting = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private _openSnackBar(content: string, title: string) {
|
||||
this.snackBar.open(content, title, {
|
||||
const config: MatSnackBarConfig = {
|
||||
horizontalPosition: <MatSnackBarHorizontalPosition>'end',
|
||||
verticalPosition: <MatSnackBarVerticalPosition>'bottom',
|
||||
duration: 4000,
|
||||
});
|
||||
duration: 4000
|
||||
};
|
||||
this.snackBar.open(content, title, config);
|
||||
}
|
||||
|
||||
private _resetErrors(): void {
|
||||
|
||||
Reference in New Issue
Block a user