@@ -7,89 +7,96 @@ import { InputTextModule } from "primeng/inputtext";
7
7
import { ButtonModule } from "primeng/button" ;
8
8
9
9
@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" ,
21
21
} )
22
22
export class SettingsDialogComponent implements OnInit {
23
- visible = false ;
24
- blogURL : string = "hashnode.anguhashblog.com" ;
23
+ visible = false ;
24
+ blogURL : string = "hashnode.anguhashblog.com" ;
25
25
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 ) ;
32
32
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
+ }
41
37
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 ;
49
43
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
+ }
53
49
54
- } else {
55
- this . newBlogURL = this . newBlogInput ;
56
- }
50
+ // Clean and parse the blog URL
51
+ this . newBlogURL = this . cleanBlogURL ( this . newBlogInput ) ;
57
52
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 ) {
77
54
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
+ }
81
81
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
+ }
82
90
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
+ }
91
94
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
+ }
95
102
}
0 commit comments