@@ -7,89 +7,96 @@ import { InputTextModule } from "primeng/inputtext";
77import { ButtonModule } from "primeng/button" ;
88
99@Component ( {
10- selector : "app-settings-dialog" ,
11- standalone : true ,
12- imports : [
13- DialogModule ,
14- InputTextModule ,
15- ButtonModule ,
16- FormsModule ,
17- ReactiveFormsModule ,
18- ] ,
19- templateUrl : "./settings-dialog.component.html" ,
20- styleUrl : "./settings-dialog.component.scss" ,
10+ selector : "app-settings-dialog" ,
11+ standalone : true ,
12+ imports : [
13+ DialogModule ,
14+ InputTextModule ,
15+ ButtonModule ,
16+ FormsModule ,
17+ ReactiveFormsModule ,
18+ ] ,
19+ templateUrl : "./settings-dialog.component.html" ,
20+ styleUrl : "./settings-dialog.component.scss" ,
2121} )
2222export class SettingsDialogComponent implements OnInit {
23- visible = false ;
24- blogURL : string = "hashnode.anguhashblog.com" ;
23+ visible = false ;
24+ blogURL : string = "hashnode.anguhashblog.com" ;
2525 newBlogInput : string = "" ;
26- newBlogURL : string = "" ;
27- blogURLChanged : boolean = false ;
28- noBlogFound : boolean = false ;
29- emptyInput : boolean = false ;
30- invalidInput : boolean = false ;
31- blogService : BlogService = inject ( BlogService ) ;
26+ newBlogURL : string = "" ;
27+ blogURLChanged : boolean = false ;
28+ noBlogFound : boolean = false ;
29+ emptyInput : boolean = false ;
30+ invalidInput : boolean = false ;
31+ blogService : BlogService = inject ( BlogService ) ;
3232
33- ngOnInit ( ) {
34- this . blogURL = this . blogService . getBlogURL ( ) ;
35- if ( this . blogURL === "hashnode.anguhashblog.com" ) {
36- this . blogURLChanged = false ;
37- } else {
38- this . blogURLChanged = true ;
39- }
40- }
33+ ngOnInit ( ) {
34+ this . blogURL = this . blogService . getBlogURL ( ) ;
35+ this . blogURLChanged = this . blogURL !== "hashnode.anguhashblog.com" ;
36+ }
4137
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 ;
38+ changeBlogURL ( ) : void {
39+ // Reset flags
40+ this . noBlogFound = false ;
41+ this . emptyInput = false ;
42+ this . invalidInput = false ;
4943
50- if ( this . newBlogInput . includes ( "https://" ) || this . newBlogInput . endsWith ( "/" ) ) {
51- const cleanedBlogURL = this . newBlogInput . split ( "https://" ) [ 1 ] ;
52- this . newBlogURL = cleanedBlogURL . split ( "/" ) [ 0 ] ;
44+ // Validate input
45+ if ( ! this . newBlogInput . trim ( ) ) {
46+ this . emptyInput = true ;
47+ return ;
48+ }
5349
54- } else {
55- this . newBlogURL = this . newBlogInput ;
56- }
50+ // Clean and parse the blog URL
51+ this . newBlogURL = this . cleanBlogURL ( this . newBlogInput ) ;
5752
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" ) {
72- this . blogURLChanged = false ;
73- } else {
74- this . noBlogFound = true ;
75- this . emptyInput = false ;
76- this . blogURLChanged = false ;
53+ if ( ! this . newBlogURL ) {
7754 this . invalidInput = true ;
78- this . newBlogInput = "" ;
79- }
80- }
55+ return ;
56+ }
57+
58+ // Check if it's the default URL case
59+ if ( this . newBlogURL === "hashnode.anguhashblog.com" ) {
60+ this . blogURLChanged = false ;
61+ return ;
62+ }
63+
64+ // Validate blog URL via the service
65+ this . blogService . getBlogInfo ( this . newBlogURL ) . subscribe ( ( blogInfo ) => {
66+ if ( blogInfo === null ) {
67+ // Blog not found
68+ this . noBlogFound = true ;
69+ this . blogURLChanged = false ;
70+ this . newBlogInput = "" ;
71+ } else {
72+ // Valid blog found
73+ this . blogService . setBlogURL ( this . newBlogURL ) ;
74+ this . blogURL = this . blogService . getBlogURL ( ) ;
75+ this . blogURLChanged = true ;
76+ this . visible = false ;
77+ window . location . reload ( ) ;
78+ }
79+ } ) ;
80+ }
8181
82+ resetBlogURL ( ) : void {
83+ this . blogService . resetBlogURL ( ) ;
84+ this . blogURL = this . blogService . getBlogURL ( ) ;
85+ this . emptyInput = false ;
86+ this . blogURLChanged = false ;
87+ this . visible = false ;
88+ window . location . reload ( ) ;
89+ }
8290
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- }
91+ showDialog ( ) {
92+ this . visible = true ;
93+ }
9194
92- showDialog ( ) {
93- this . visible = true ;
94- }
95+ private cleanBlogURL ( input : string ) : string {
96+ // Strip "https://" and trailing slashes if present
97+ if ( input . includes ( "https://" ) ) {
98+ input = input . split ( "https://" ) [ 1 ] ;
99+ }
100+ return input . endsWith ( "/" ) ? input . slice ( 0 , - 1 ) : input ;
101+ }
95102}
0 commit comments