Skip to content

Commit fba8ad5

Browse files
committed
feat: add delete news in news service
1 parent 8fa6b67 commit fba8ad5

File tree

8 files changed

+88
-18
lines changed

8 files changed

+88
-18
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,9 @@ export class NewsService {
5656
return this.httpClient
5757
.put<News>(`${this.serverUrl}/api/user/news/${id}`, formNewsData);
5858
}
59+
60+
deleteNews(id: string): Observable<string> {
61+
return this.httpClient
62+
.delete(`${this.serverUrl}/api/user/news/${id}`, { responseType: "text" });
63+
}
5964
}

src/app/pages/user/list-news-page/list-news-page.component.html

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,16 @@ <h2>Mis Noticias</h2>
4040
</svg>
4141
</button>
4242
</a>
43-
<a>
44-
<button>
45-
<svg
46-
fill="currentColor"
47-
viewBox="0 0 24 24"
48-
xmlns="http://www.w3.org/2000/svg"
49-
>
50-
<path d="M8 1.5V2.5H3C2.44772 2.5 2 2.94772 2 3.5V4.5C2 5.05228 2.44772 5.5 3 5.5H21C21.5523 5.5 22 5.05228 22 4.5V3.5C22 2.94772 21.5523 2.5 21 2.5H16V1.5C16 0.947715 15.5523 0.5 15 0.5H9C8.44772 0.5 8 0.947715 8 1.5Z"></path>
51-
<path d="M3.9231 7.5H20.0767L19.1344 20.2216C19.0183 21.7882 17.7135 23 16.1426 23H7.85724C6.28636 23 4.98148 21.7882 4.86544 20.2216L3.9231 7.5Z"></path>
52-
</svg>
53-
</button>
54-
</a>
43+
<button (click)="openModal(news.idNews)">
44+
<svg
45+
fill="currentColor"
46+
viewBox="0 0 24 24"
47+
xmlns="http://www.w3.org/2000/svg"
48+
>
49+
<path d="M8 1.5V2.5H3C2.44772 2.5 2 2.94772 2 3.5V4.5C2 5.05228 2.44772 5.5 3 5.5H21C21.5523 5.5 22 5.05228 22 4.5V3.5C22 2.94772 21.5523 2.5 21 2.5H16V1.5C16 0.947715 15.5523 0.5 15 0.5H9C8.44772 0.5 8 0.947715 8 1.5Z"></path>
50+
<path d="M3.9231 7.5H20.0767L19.1344 20.2216C19.0183 21.7882 17.7135 23 16.1426 23H7.85724C6.28636 23 4.98148 21.7882 4.86544 20.2216L3.9231 7.5Z"></path>
51+
</svg>
52+
</button>
5553
</td>
5654
</tr>
5755
} @empty {
@@ -96,4 +94,17 @@ <h2>Mis Noticias</h2>
9694
}
9795
</tbody>
9896
</table>
99-
</section>
97+
</section>
98+
<app-modal [show]="isOpen">
99+
<p class="mb-6 py-4">
100+
¿Está seguro que desea eliminar la noticia?
101+
</p>
102+
<div class="text-right">
103+
<button (click)="closeModal()" class="btn-cancel">
104+
Cancelar
105+
</button>
106+
<button (click)="deleteNews()" class="btn-success">
107+
Eliminar
108+
</button>
109+
</div>
110+
</app-modal>

src/app/pages/user/list-news-page/list-news-page.component.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ table th {
44
table td {
55
@apply border-t border-slate-300 border-solid px-4 py-2 text-center text-smvs;
66
}
7-
table button {
7+
table tr button {
88
@apply bg-rose-500 hover:bg-rose-400 px-2.5 py-2 text-white;
99
}
1010
table button svg {

src/app/pages/user/list-news-page/list-news-page.component.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Component, OnInit } from "@angular/core";
1+
import { Component, OnInit, inject } from "@angular/core";
2+
import { ToastrService } from "ngx-toastr";
23

34
import { NewsService } from "../../../core/services/news.service";
45

@@ -10,14 +11,45 @@ import { News } from "../../../core/interfaces/news.interface";
1011
styleUrl: "./list-news-page.component.scss",
1112
})
1213
export class ListNewsPageComponent implements OnInit {
14+
private toastrService = inject(ToastrService);
15+
16+
public idNews: string = "";
17+
public isOpen: boolean = false;
1318
public listNews: News[] = [];
1419

1520
constructor(
1621
private newsService: NewsService
1722
) {}
1823

1924
ngOnInit(): void {
25+
this.loadNews();
26+
}
27+
28+
loadNews() {
2029
this.newsService.getNewsByUser()
2130
.subscribe((news: News[]) => this.listNews = news);
2231
}
32+
33+
deleteNews() {
34+
this.newsService.deleteNews(this.idNews)
35+
.subscribe(
36+
msg => {
37+
this.listNews = [];
38+
this.loadNews();
39+
40+
this.isOpen = false;
41+
this.toastrService.success(msg);
42+
}
43+
);
44+
}
45+
46+
openModal(id: string) {
47+
this.idNews = id;
48+
this.isOpen = true;
49+
}
50+
51+
closeModal() {
52+
this.idNews = "";
53+
this.isOpen = false;
54+
}
2355
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@if (show) {
2+
<div class="bg-black bg-opacity-60 fixed flex inset-0 items-center justify-center">
3+
<div class="bg-white p-8 rounded-lg shadow-lg">
4+
<ng-content></ng-content>
5+
</div>
6+
</div>
7+
}

src/app/shared/modal/modal.component.scss

Whitespace-only changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Component, Input } from "@angular/core";
2+
3+
@Component({
4+
selector: "app-modal",
5+
templateUrl: "./modal.component.html",
6+
styleUrl: "./modal.component.scss",
7+
})
8+
export class ModalComponent {
9+
@Input()
10+
public show: boolean = false;
11+
}

src/app/shared/shared.module.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
import { NgModule } from "@angular/core";
22
import { CommonModule } from "@angular/common";
3-
import { LoaderComponent } from "./loader/loader.component";
43
import { NgxLoadingModule } from "ngx-loading";
54

5+
import { LoaderComponent } from "./loader/loader.component";
6+
import { ModalComponent } from "./modal/modal.component";
7+
68
@NgModule({
79
declarations: [
8-
LoaderComponent
10+
LoaderComponent,
11+
ModalComponent,
912
],
1013
imports: [
1114
CommonModule,
1215
NgxLoadingModule.forRoot({}),
1316
],
1417
exports: [
15-
LoaderComponent
18+
LoaderComponent,
19+
ModalComponent,
1620
],
1721
})
1822
export class SharedModule {}

0 commit comments

Comments
 (0)