Skip to content

Commit 9a6e3fc

Browse files
committed
✨ add validation for used space when upload new files
1 parent 147475c commit 9a6e3fc

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

src/app/shared/modals/upload-files/upload-files.component.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
</div>
99
<div class="body">
1010
<form>
11+
<div class="messages" *ngIf="!validateUsedSpace()">
12+
<span class="message error">
13+
No tienes espacio suficiente para subir los archivos seleccionados
14+
({{ totalSpace - usedSpace | fileSize }} disponibles)
15+
</span>
16+
</div>
1117
<div class="form-group" *ngIf="!files.length">
1218
<label for="file">Selecciona los archivos</label>
1319
<input type="file" id="file" name="file" multiple (change)="onFileChange($event)">
@@ -40,7 +46,7 @@
4046
<button class="btn btn-primary" *ngIf="files && files.length > 0" type="file" (click)="selectFiles()">
4147
Subir más archivos
4248
</button>
43-
<button class="btn btn-primary" [disabled]="!validateFiles() || uploading" (click)="uploadFiles()">
49+
<button class="btn btn-primary" [disabled]="!validateFiles() || uploading || !validateUsedSpace()" (click)="uploadFiles()">
4450
{{ uploading ? 'Guardando' : 'Guardar' }}
4551
<i *ngIf="uploading" class='bx bx-loader-circle bx-spin bx-rotate-90' ></i>
4652
</button>

src/app/shared/modals/upload-files/upload-files.component.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@
101101
@include fs-6;
102102
@include fw-500;
103103
color: var(--fc-purple);
104+
105+
&.error {
106+
@include fs-4;
107+
color: var(--fc-red);
108+
}
104109
}
105110
}
106111

src/app/shared/modals/upload-files/upload-files.component.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Component, ElementRef, HostListener, OnDestroy, OnInit, ViewChild } fro
22

33
import { Subscription } from 'rxjs';
44

5+
import { AuthService } from '@services/auth.service';
56
import { FileService } from '@services/file.service';
67
import { ModalService } from '@services/modal.service';
78

@@ -25,6 +26,8 @@ export class UploadFilesComponent implements OnInit, OnDestroy {
2526
private openModalSubscription: Subscription;
2627
private modalOpened = false;
2728
private folderId: string;
29+
usedSpace: number;
30+
totalSpace: number;
2831

2932
uploading = false;
3033

@@ -34,6 +37,7 @@ export class UploadFilesComponent implements OnInit, OnDestroy {
3437
constructor(
3538
private modalService: ModalService,
3639
private fileService: FileService,
40+
private authService: AuthService
3741
) { }
3842

3943
ngOnDestroy(): void {
@@ -44,6 +48,8 @@ export class UploadFilesComponent implements OnInit, OnDestroy {
4448
this.openModalSubscription = this.modalService.openNewFileModal
4549
.subscribe(({ folderID }) => {
4650
this.folderId = folderID;
51+
this.usedSpace = this.authService.userActive.usedSpace;
52+
this.totalSpace = this.authService.userActive.totalSpace;
4753
this.openModal();
4854
});
4955
}
@@ -71,6 +77,7 @@ export class UploadFilesComponent implements OnInit, OnDestroy {
7177
return file;
7278
});
7379
this.files = [...this.files, ...files];
80+
this.validateUsedSpace();
7481
}
7582

7683
getIcon(file: File): string {
@@ -86,7 +93,19 @@ export class UploadFilesComponent implements OnInit, OnDestroy {
8693
&& ALLOWED_FILE_TYPES.includes(file.type));
8794
}
8895

96+
validateUsedSpace(): boolean {
97+
const totalSize = this.files.reduce((acc, file) => acc + file.size, 0);
98+
return this.usedSpace + totalSize <= this.totalSpace;
99+
}
100+
101+
get getUsedSpace(): number {
102+
return this.usedSpace;
103+
}
104+
89105
uploadFiles(): void {
106+
if (!this.validateUsedSpace()) {
107+
return;
108+
}
90109
if (this.validateFiles() && this.folderId) {
91110
this.uploading = true;
92111
this.fileService.createFiles(this.files, this.folderId).subscribe({

src/styles.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
--fc-secondary: #{$dark-grey};
2424
--fc-white: #{$white-grey};
2525
--fc-purple: #{$purple};
26+
--fc-red: #{$red};
2627
--border-input: #{$purple};
2728
--input-selection: #{$purple-rgba};
2829
// --border-input: 1px solid #333639;

0 commit comments

Comments
 (0)