Skip to content

Commit 42f56d9

Browse files
feature(settings-dialog): input validations and error handeling
1 parent 047da65 commit 42f56d9

File tree

3 files changed

+60
-23
lines changed

3 files changed

+60
-23
lines changed

angular-primeng-app/src/app/partials/settings-dialog/settings-dialog.component.html

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
<p-dialog [(visible)]="visible" [style]="{width: '27vw'}">
55
@if (blogURLChanged) {
66
<div class="dialog-content">
7-
<p>Blog URL changed and set in local storage</p>
8-
<p>Reload the page to see your content loading</p>
9-
<p>When resetting you may need to click on</p>
10-
<p>the logo image to load the content again</p>
7+
<p>Blog URL was changed</p>
8+
<p>You are seeing blog posts</p>
9+
<p>from the following blog:</p>
10+
<h3>{{blogURL}}</h3>
1111
<div class="dialog-actions">
1212
<p-button label="Reset" (click)="resetBlogURL()" class="p-button-secondary"></p-button>
1313
</div>
@@ -18,15 +18,15 @@ <h3>Try with your Blog</h3>
1818
<p>try AnguHashBlog</p>
1919
<small>with another Hashnode blog </small>
2020
<span class="p-input-icon-right">
21-
@if (newBlogURL) {
22-
<i class="pi pi-times" (click)="newBlogURL=''"></i>
21+
@if (newBlogInput) {
22+
<i class="pi pi-times" (click)="newBlogInput=''"></i>
2323
}
24-
<input type="text" pInputText placeholder="Type a Blog URL" [(ngModel)]="newBlogURL" />
24+
<input type="text" pInputText placeholder="Type a Blog URL" [(ngModel)]="newBlogInput" />
2525
</span>
2626
</div>
2727
<div class="dialog-actions">
2828
<div class="dialog-actions">
29-
<p-button label="Change" (click)="changeBlogURL(); newBlogURL=''" class="p-button-secondary"></p-button>
29+
<p-button label="Change" (click)="changeBlogURL(); newBlogInput=''" class="p-button-secondary"></p-button>
3030
</div>
3131
</div>
3232
}

angular-primeng-app/src/app/partials/settings-dialog/settings-dialog.component.scss

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ p-dialog {
1515
}
1616

1717
p-button {
18-
margin: 0.5rem 0;
18+
margin: 1.5rem 0;
1919
}
2020

2121
p {
@@ -32,6 +32,5 @@ p-dialog {
3232
margin : 1rem 0;
3333
}
3434
}
35-
3635
}
3736

angular-primeng-app/src/app/partials/settings-dialog/settings-dialog.component.ts

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@ import { ButtonModule } from "primeng/button";
2222
export class SettingsDialogComponent implements OnInit {
2323
visible = false;
2424
blogURL: string = "hashnode.anguhashblog.com";
25+
newBlogInput: string = "";
2526
newBlogURL: string = "";
2627
blogURLChanged: boolean = false;
28+
noBlogFound: boolean = false;
29+
emptyInput: boolean = false;
30+
invalidInput: boolean = false;
2731
blogService: BlogService = inject(BlogService);
2832

2933
ngOnInit() {
@@ -35,21 +39,55 @@ export class SettingsDialogComponent implements OnInit {
3539
}
3640
}
3741

38-
changeBlogURL(): void {
39-
this.blogService.setBlogURL(this.newBlogURL);
40-
this.blogURL = this.blogService.getBlogURL();
41-
if (this.blogURL === "hashnode.anguhashblog.com") {
42+
changeBlogURL(): void {
43+
this.noBlogFound = false;
44+
if (this.newBlogInput === "") {
45+
this.emptyInput = true;
46+
return;
47+
} else if ( this.newBlogInput !== "") {
48+
this.emptyInput = false;
49+
50+
if (this.newBlogInput.includes("https://") || this.newBlogInput.endsWith("/")) {
51+
const cleanedBlogURL = this.newBlogInput.split("https://")[1];
52+
this.newBlogURL = cleanedBlogURL.split("/")[0];
53+
54+
} else {
55+
this.newBlogURL = this.newBlogInput;
56+
}
57+
58+
this.blogService.getBlogInfo(this.newBlogURL).subscribe((blogInfo) => {
59+
if (blogInfo === null) {
60+
this.noBlogFound = true;
61+
this.blogURLChanged = false;
62+
this.newBlogInput = "";
63+
} else {
64+
this.blogService.setBlogURL(this.newBlogURL);
65+
this.blogURL = this.blogService.getBlogURL();
66+
this.blogURLChanged = true;
67+
this.visible = false;
68+
window.location.reload();
69+
}
70+
});
71+
} else if (this.blogURL === "hashnode.anguhashblog.com") {
4272
this.blogURLChanged = false;
4373
} else {
44-
this.blogURLChanged = true;
45-
}
46-
}
47-
48-
resetBlogURL(): void {
49-
this.blogService.resetBlogURL();
50-
this.blogURL = this.blogService.getBlogURL();
51-
this.blogURLChanged = false;
52-
}
74+
this.noBlogFound = true;
75+
this.emptyInput = false;
76+
this.blogURLChanged = false;
77+
this.invalidInput = true;
78+
this.newBlogInput = "";
79+
}
80+
}
81+
82+
83+
resetBlogURL(): void {
84+
this.blogService.resetBlogURL();
85+
this.blogURL = this.blogService.getBlogURL();
86+
this.emptyInput = false;
87+
this.blogURLChanged = false;
88+
this.visible = false;
89+
window.location.reload();
90+
}
5391

5492
showDialog() {
5593
this.visible = true;

0 commit comments

Comments
 (0)