Skip to content

Commit 18f4483

Browse files
committed
✨ add delete files
1 parent 721aadb commit 18f4483

File tree

8 files changed

+45
-4
lines changed

8 files changed

+45
-4
lines changed

src/app/core/interfaces/response.interface.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@ export interface IFolderResponse {
2828
export interface IFileResponse {
2929
ok: boolean,
3030
msg: string,
31-
files: File[]
31+
files: File[],
32+
file: File,
3233
}

src/app/core/services/file.service.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const base_url = environment.base_url;
1717
})
1818
export class FileService {
1919
filesCreated: EventEmitter<FileModel[]> = new EventEmitter();
20+
fileDeleted: EventEmitter<FileModel> = new EventEmitter();
2021
updateTotalSize: EventEmitter<number> = new EventEmitter();
2122

2223
get headers() {
@@ -47,4 +48,14 @@ export class FileService {
4748
const url = `${base_url}/file/download/${fileID}`;
4849
return this.http.get(url, { responseType: 'blob', ...this.headers });
4950
}
51+
52+
deleteFile(fileID: string): Observable<boolean> {
53+
const url = `${base_url}/file/${fileID}`;
54+
return this.http.delete<IFileResponse>(url, this.headers).pipe(map(resp => {
55+
this.fileDeleted.emit(resp.file);
56+
this.updateTotalSize.emit(-resp.file.size);
57+
return resp.ok;
58+
}));
59+
}
60+
5061
}

src/app/features/folder/folder.component.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export class FolderComponent implements OnInit, OnDestroy {
1818
private folderCreatedSubscripion: Subscription;
1919
private filesUploadedSubscription: Subscription;
2020
private routerSubscription: Subscription;
21+
private fileDeletedSubscription: Subscription;
22+
2123
folder: Folder;
2224
loading = false;
2325
allFiles: (Folder | File)[];
@@ -32,6 +34,7 @@ export class FolderComponent implements OnInit, OnDestroy {
3234
this.folderCreatedSubscripion.unsubscribe();
3335
this.filesUploadedSubscription.unsubscribe();
3436
this.routerSubscription.unsubscribe();
37+
this.fileDeletedSubscription.unsubscribe();
3538
}
3639

3740
ngOnInit(): void {
@@ -47,6 +50,10 @@ export class FolderComponent implements OnInit, OnDestroy {
4750
this.mixFilesAndFolders();
4851
});
4952
this.routerSubscription = this.activadedRoute.params.subscribe(() => this.getFolder());
53+
this.fileDeletedSubscription = this.fileService.fileDeleted.subscribe((file) => {
54+
this.folder.files = this.folder.files.filter((f) => f._id !== file._id);
55+
this.mixFilesAndFolders();
56+
});
5057
}
5158

5259
getFolder(): void {

src/app/features/home/home.component.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ import { Folder } from '@models/folder.model';
1515
})
1616
export class HomeComponent implements OnInit, OnDestroy {
1717
root: Folder;
18-
folderCreatedSubscription: Subscription;
19-
filesCreatedSubscription: Subscription;
18+
private folderCreatedSubscription: Subscription;
19+
private filesCreatedSubscription: Subscription;
20+
private fileDeletedSubscription: Subscription;
21+
2022
constructor(
2123
private authService: AuthService,
2224
private folderService: FolderService,
@@ -26,6 +28,7 @@ export class HomeComponent implements OnInit, OnDestroy {
2628
ngOnDestroy(): void {
2729
this.folderCreatedSubscription.unsubscribe();
2830
this.filesCreatedSubscription.unsubscribe();
31+
this.fileDeletedSubscription.unsubscribe();
2932
}
3033

3134
ngOnInit(): void {
@@ -46,6 +49,9 @@ export class HomeComponent implements OnInit, OnDestroy {
4649
this.root.files.push(...files);
4750
this.orderFiles();
4851
});
52+
this.fileDeletedSubscription = this.fileService.fileDeleted.subscribe((file) => {
53+
this.root.files = this.root.files.filter((f) => f._id !== file._id);
54+
});
4955
}
5056

5157
private orderFolders(): void {

src/app/shared/dropdowns/dots-dropdown/dots-dropdown.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<i class="bx bx-download"></i>
1515
<span class="title">Descargar</span>
1616
</li>
17-
<li role="menuitem" class='dropdown-item'>
17+
<li role="menuitem" class='dropdown-item danger' (click)="deleteFile()" *ngIf="isFile()">
1818
<i class="bx bx-trash"></i>
1919
<span class="title">Eliminar</span>
2020
</li>

src/app/shared/dropdowns/dots-dropdown/dots-dropdown.component.scss

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ ul {
7272

7373
&:last-child {
7474
border-radius: 0 0 10px 10px;
75+
}
7576

77+
&.danger {
7678
&:hover {
7779
background-color: #ff72568c;
7880
}

src/app/shared/dropdowns/dots-dropdown/dots-dropdown.component.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,14 @@ export class DotsDropdownComponent {
7171
this.modalService.openUpdateColor(this.file._id as string, this.file.color as string);
7272
}
7373
}
74+
75+
deleteFile(): void {
76+
if (this.isFile()) {
77+
this.fileService.deleteFile(this.file._id as string).subscribe({
78+
complete: () => {
79+
this.hideDropdown();
80+
}
81+
});
82+
}
83+
}
7484
}

src/app/shared/modals/colors/colors.component.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@
2424
}
2525
}
2626
}
27+
28+
.modal-content {
29+
max-width: 27rem;
30+
}

0 commit comments

Comments
 (0)