Skip to content

Commit

Permalink
Nuevo componente: Spinner
Browse files Browse the repository at this point in the history
- Implementación de la pantalla de carga.
  • Loading branch information
nmarulo committed Dec 15, 2024
1 parent 18af01a commit 4414ecf
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
<router-outlet/>

<app-spinner/>
4 changes: 3 additions & 1 deletion src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import {Router, RouterOutlet} from '@angular/router';
import {DataSidebar} from './models/data-sidebar';
import {SidebarService} from './services/layout/sidebar.service';
import {BrowserStorageService} from './services/layout/browser-storage.service';
import {SpinnerComponent} from '@app/layout/spinner/spinner.component';

@Component({
selector: 'app-root',
imports: [
RouterOutlet
RouterOutlet,
SpinnerComponent
],
templateUrl: './app.component.html',
styleUrl: './app.component.css'
Expand Down
4 changes: 3 additions & 1 deletion src/app/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import {authInterceptor} from './shared/interceptor/auth.interceptor';
import {providePrimeNG} from 'primeng/config';
import Lara from '@primeng/themes/lara';
import {definePreset} from '@primeng/themes';
import {spinnerInterceptor} from '@app/shared/interceptor/spinner.interceptor';

export const appConfig: ApplicationConfig = {
providers: [
provideRouter(routes, withComponentInputBinding()),
provideHttpClient(
withInterceptors([
authInterceptor
authInterceptor,
spinnerInterceptor
])
),
provideAnimations(),
Expand Down
7 changes: 7 additions & 0 deletions src/app/layout/spinner/spinner.component.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.spinner-content {
top: 0;
min-width: 100%;
min-height: 100%;
left: 0;
z-index: 1073;
}
7 changes: 7 additions & 0 deletions src/app/layout/spinner/spinner.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@if (show()) {
<div class="spinner-content align-items-center d-flex justify-content-center position-fixed">
<p-progress-spinner ariaLabel="loading"
strokeWidth="4"/>
</div>
<div class="modal-backdrop fade show"></div>
}
20 changes: 20 additions & 0 deletions src/app/layout/spinner/spinner.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {Component, computed} from '@angular/core';
import {ProgressSpinner} from 'primeng/progressspinner';
import {SpinnerService} from '@app/services/layout/spinner.service';

@Component({
selector: 'app-spinner',
imports: [
ProgressSpinner
],
templateUrl: './spinner.component.html',
styleUrl: './spinner.component.css'
})
export class SpinnerComponent {

show = computed(() => this.spinnerService.getSpinner());

constructor(private spinnerService: SpinnerService) {
}

}
32 changes: 32 additions & 0 deletions src/app/services/layout/spinner.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {Injectable, signal} from '@angular/core';

@Injectable({
providedIn: 'root'
})
export class SpinnerService {

private spinner = signal<boolean>(false);

private timeout: any;

constructor() {
}

show() {
clearTimeout(this.timeout);

this.timeout = setTimeout(() => {
this.spinner.set(true);
}, 500);
}

hide() {
clearTimeout(this.timeout);
this.spinner.set(false);
}

getSpinner() {
return this.spinner();
}

}
17 changes: 17 additions & 0 deletions src/app/shared/interceptor/spinner.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {HttpInterceptorFn} from '@angular/common/http';
import {inject} from '@angular/core';
import {SpinnerService} from '@app/services/layout/spinner.service';
import {finalize} from 'rxjs';

export const spinnerInterceptor: HttpInterceptorFn = (req, next) => {
const spinnerService = inject(SpinnerService);

spinnerService.show();

return next(req)
.pipe(
finalize(() => {
spinnerService.hide();
})
);
};

0 comments on commit 4414ecf

Please sign in to comment.