From b83e994f06a2067b3566fbf4888658f43fcdfaa5 Mon Sep 17 00:00:00 2001 From: itzMiney Date: Mon, 30 Dec 2024 20:56:47 +0100 Subject: [PATCH] Removed redundant meta tag handling, updated routing --- src/app/about/about.component.ts | 3 + src/app/admin/admin.component.ts | 5 +- src/app/app.component.ts | 32 +------ src/app/app.routes.ts | 96 ++----------------- .../article-page/article-page.component.ts | 42 ++------ src/app/blog/blog.component.ts | 16 +++- src/app/login/login.component.ts | 3 + src/app/not-found/not-found.component.ts | 3 + src/app/portfolio/portfolio.component.ts | 6 +- src/app/services/update-meta.service.ts | 31 ------ 10 files changed, 51 insertions(+), 186 deletions(-) diff --git a/src/app/about/about.component.ts b/src/app/about/about.component.ts index 728deac..877759d 100644 --- a/src/app/about/about.component.ts +++ b/src/app/about/about.component.ts @@ -2,6 +2,7 @@ import {AfterViewInit, Component, OnDestroy, OnInit, PLATFORM_ID, Inject} from ' import {isPlatformBrowser, NgStyle} from '@angular/common'; import {VantaBackgroundService} from '../services/vanta-background.service'; import {DeviceDetectorService} from '../services/device-detector.service'; +import {Title} from '@angular/platform-browser'; @Component({ selector: 'app-about', @@ -17,10 +18,12 @@ export class AboutComponent implements OnInit, OnDestroy, AfterViewInit { constructor( private vantaService: VantaBackgroundService, private deviceService: DeviceDetectorService, + private titleService: Title, @Inject(PLATFORM_ID) private platformId: Object ) {} ngOnInit() { + this.titleService.setTitle('itzMiney\'s Home'); this.isMobile = this.deviceService.isMobile; if (isPlatformBrowser(this.platformId)) { window.addEventListener('resize', this.onResize.bind(this)); diff --git a/src/app/admin/admin.component.ts b/src/app/admin/admin.component.ts index bd80399..fecea40 100644 --- a/src/app/admin/admin.component.ts +++ b/src/app/admin/admin.component.ts @@ -5,6 +5,7 @@ import {NgFor, NgIf, NgStyle} from '@angular/common'; import {Observer} from 'rxjs'; import {FormsModule} from '@angular/forms'; import {DeviceDetectorService} from '../services/device-detector.service'; +import {Title} from '@angular/platform-browser'; @Component({ selector: 'app-admin', @@ -28,11 +29,13 @@ export class AdminComponent implements OnInit { constructor( private articleService: ArticleService, private router: Router, - private deviceService: DeviceDetectorService + private deviceService: DeviceDetectorService, + private titleService: Title ) {} // Fetch articles from backend ngOnInit() { + this.titleService.setTitle('Admin Panel | itzMiney') this.isMobile = this.deviceService.isMobile; if (!this.token) { this.navigateToLogin(); diff --git a/src/app/app.component.ts b/src/app/app.component.ts index a09f5d1..ecf59e0 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,12 +1,10 @@ import {Component, OnInit} from '@angular/core'; import {NgIf} from '@angular/common'; -import {ActivatedRoute, NavigationEnd, Router, RouterOutlet} from '@angular/router'; +import {ActivatedRoute, Router, RouterOutlet} from '@angular/router'; import {NavbarComponent} from './navbar/navbar.component'; import {MobileNavbarComponent} from './mobile-navbar/mobile-navbar.component'; import {FooterComponent} from './footer/footer.component'; -import {filter, map, mergeMap} from 'rxjs/operators'; import {DeviceDetectorService} from './services/device-detector.service'; -import { UpdateMetaService } from './services/update-meta.service'; @Component({ selector: 'app-root', @@ -23,41 +21,15 @@ export class AppComponent implements OnInit { private router: Router, private activatedRoute: ActivatedRoute, private deviceService: DeviceDetectorService, - private updateMetaService: UpdateMetaService, ) {} ngOnInit() { + this.router.events.subscribe(() => this.checkRoute()) this.isMobile = this.deviceService.isMobile; - - this.router.events - .pipe( - filter(event => event instanceof NavigationEnd), - map(() => this.activatedRoute), - map(route => { - while (route.firstChild) route = route.firstChild; - return route; - }), - mergeMap(route => route.data) - ) - .subscribe(data => { - const meta = data['meta'] || {}; - if (this.router.url === '/about') { - // fallback meta data - meta.title = meta.title || 'itzMiney\'s Home'; - meta.description = meta.description || 'Welcome to itzMiney\'s Homepage! Here you can find my portfolio and other cool stuff.'; - meta.image = meta.image || 'https://itzminey.dev/assets/ogimg.png'; - meta.url = meta.url || 'https://itzminey.dev'; - meta.color = meta.color || '#eb284c'; - } - this.updateMetaService.updateMetaTags(meta); - this.checkRoute(); - }); } checkRoute() { const currentRoute = this.activatedRoute.snapshot.firstChild?.routeConfig?.path; - - switch (currentRoute) { case 'admin': { this.showNavbar = false; diff --git a/src/app/app.routes.ts b/src/app/app.routes.ts index 573d87a..ca7c573 100644 --- a/src/app/app.routes.ts +++ b/src/app/app.routes.ts @@ -8,92 +8,12 @@ import {AdminComponent} from './admin/admin.component'; import {ArticlePageComponent} from './article-page/article-page.component'; export const routes: Routes = [ - { - path: '', - component: AboutComponent, - data: { - meta: { - title: 'itzMiney\'s Home', - description: 'Welcome to itzMiney\'s Homepage! Here you can find my portfolio and other cool stuff.', - image: 'https://itzminey.dev/assets/ogimg.png', - url: 'https://itzminey.dev/about', - color: '#eb284c' - } - } - }, // Redirect default route to 'about' - { - path: 'about', - redirectTo: '/', - pathMatch: 'full', - }, - { - path: 'portfolio', - component: PortfolioComponent, - data: { - meta: { - title: 'Portfolio | itzMiney', - description: 'This is my portfolio where you can find examples of my designs and other projects!', - image: 'https://itzminey.dev/assets/ogimg.png', - url: 'https://itzminey.dev/portfolio', - color: '#eb284c' - } - } - }, - { - path: 'blog', - component: BlogComponent, - data: { - meta: { - title: 'Blog | itzMiney', - description: 'Welcome to my blog! I\'ll post occasional updates here on the stuff I\'m currently up to.', - image: 'https://itzminey.dev/assets/ogimg.png', - url: 'https://itzminey.dev/blog', - color: '#eb284c' - } - } - }, - { - path: 'login', - component: LoginComponent, - data: { - meta: { - title: 'Login | itzMiney', - description: 'Admin Panel Login', - image: 'https://itzminey.dev/assets/ogimg.png', - url: 'https://itzminey.dev/login', - color: '#eb284c' - } - } - }, - { - path: 'admin', - component: AdminComponent, - data: { - meta: { - title: 'Admin Panel | itzMiney', - description: 'Admin Panel', - image: 'https://itzminey.dev/assets/ogimg.png', - url: 'https://itzminey.dev/admin', - color: '#eb284c' - } - } - }, - { - path: 'blog/:slug', - component: ArticlePageComponent - }, - { - path: '**', - pathMatch: 'full', - component: NotFoundComponent, - data: { - meta: { - title: '404 | Page Not Found', - description: 'How the hell did you end up here?', - image: 'https://itzminey.dev/assets/ogimg.png', - url: 'https://itzminey.dev/404', - color: '#eb284c' - } - } - } + { path: '', component: AboutComponent }, + { path: 'about', redirectTo: '/', pathMatch: 'full' }, + { path: 'portfolio', component: PortfolioComponent }, + { path: 'blog', component: BlogComponent }, + { path: 'login', component: LoginComponent }, + { path: 'admin', component: AdminComponent }, + { path: 'blog/:slug', component: ArticlePageComponent }, + { path: '**', pathMatch: 'full', component: NotFoundComponent } ]; diff --git a/src/app/article-page/article-page.component.ts b/src/app/article-page/article-page.component.ts index 3775371..6bffbba 100644 --- a/src/app/article-page/article-page.component.ts +++ b/src/app/article-page/article-page.component.ts @@ -4,7 +4,7 @@ import {DatePipe, isPlatformBrowser, NgIf} from '@angular/common'; import {VantaBackgroundService} from '../services/vanta-background.service'; import {ArticleService} from '../services/article.service'; import {ActivatedRoute} from '@angular/router'; -import {UpdateMetaService} from '../services/update-meta.service'; +import {Title} from '@angular/platform-browser'; interface Article { id: number; @@ -36,7 +36,7 @@ export class ArticlePageComponent implements OnInit, OnDestroy, AfterViewInit { private deviceService: DeviceDetectorService, private vantaService: VantaBackgroundService, private articleService: ArticleService, - private updateMetaService: UpdateMetaService, + private titleService: Title, private route: ActivatedRoute, @Inject(PLATFORM_ID) private platformId: Object ) { @@ -48,15 +48,9 @@ export class ArticlePageComponent implements OnInit, OnDestroy, AfterViewInit { this.route.params.subscribe((params) => { const slug = params['slug']; if (slug) { - this.loadArticleMetaBySlug(slug); + this.loadTitleBySlug(slug); } else { - this.updateMetaService.updateMetaTags({ - title: 'Article | itzMiney', - description: 'An article on itzMiney\'s Blog.', - image: 'https://itzminey.dev/assets/ogimg.png', - url: 'https://itzminey.dev/blog', - color: '#ffffff' - }); + this.titleService.setTitle('Article | itzMiney'); } }); @@ -92,38 +86,20 @@ export class ArticlePageComponent implements OnInit, OnDestroy, AfterViewInit { this.vantaService.resizeVanta(this.elementId); } - loadArticleMetaBySlug(slug: string) { + loadTitleBySlug(slug: string) { this.articleService.getArticleBySlug(slug).subscribe( (article) => { if (article) { this.currentArticle = article; - this.updateMetaService.updateMetaTags({ - title: article.title, - description: article.content.slice(0, 150), // First 150 characters - image: 'https://itzminey.dev/assets/ogimg.png', // Use article image or default - url: `https://itzminey.dev/blog/${article.slug}`, - color: '#ffffff' - }); + this.titleService.setTitle( article.title ); } else { console.warn('Article not found for slug:', slug); - this.updateMetaService.updateMetaTags({ - title: 'Article Not Found', - description: 'The requested article could not be found.', - image: 'https://itzminey.dev/assets/ogimg.png', - url: 'https://itzminey.dev', - color: '#ff0000' - }); + this.titleService.setTitle('Article Not Found'); } }, (error) => { - console.error('Failed to load article metadata', error); - this.updateMetaService.updateMetaTags({ - title: 'Error', - description: 'An error occurred while loading this article.', - image: 'https://itzminey.dev/assets/ogimg.png', - url: 'https://itzminey.dev', - color: '#ff0000' - }); + console.error('Failed to load article title', error); + this.titleService.setTitle('Error loading Article'); } ); } diff --git a/src/app/blog/blog.component.ts b/src/app/blog/blog.component.ts index 0bb1263..4338fa7 100644 --- a/src/app/blog/blog.component.ts +++ b/src/app/blog/blog.component.ts @@ -5,6 +5,7 @@ import {VantaBackgroundService} from '../services/vanta-background.service'; import { ArticleService } from '../services/article.service'; import { Router } from '@angular/router'; import {Observable} from 'rxjs'; +import {Title} from '@angular/platform-browser'; @Component({ selector: 'app-blog', @@ -28,10 +29,12 @@ export class BlogComponent implements OnInit, OnDestroy, AfterViewInit { private vantaService: VantaBackgroundService, private articleService: ArticleService, private router: Router, + private titleService: Title, @Inject(PLATFORM_ID) private platformId: Object ) {} ngOnInit() { + this.titleService.setTitle('Blog | itzMiney') this.isMobile = this.deviceService.isMobile; if (isPlatformBrowser(this.platformId)) { window.addEventListener('resize', this.onResize.bind(this)); @@ -40,7 +43,18 @@ export class BlogComponent implements OnInit, OnDestroy, AfterViewInit { } navigateToArticle(slug: string): void { - this.router.navigate([`/blog/${slug}`]); + this.router.navigate([`/blog/${slug}`]).then( + (success) => { + if (success) { + console.log('Navigation to article succeeded'); + } else { + console.warn('Navigation to article failed'); + } + }, + (error) => { + console.error('Navigation error:', error); + } + ); } ngAfterViewInit() { diff --git a/src/app/login/login.component.ts b/src/app/login/login.component.ts index a3aeaef..aa62558 100644 --- a/src/app/login/login.component.ts +++ b/src/app/login/login.component.ts @@ -6,6 +6,7 @@ import {NgIf} from '@angular/common'; import {VantaBackgroundService} from '../services/vanta-background.service'; import {DeviceDetectorService} from '../services/device-detector.service'; import {isPlatformBrowser} from '@angular/common'; +import {Title} from '@angular/platform-browser'; @Component({ selector: 'app-login', @@ -29,10 +30,12 @@ export class LoginComponent implements OnInit, OnDestroy, AfterViewInit { private router: Router, private vantaService: VantaBackgroundService, private deviceService: DeviceDetectorService, + private titleService: Title, @Inject(PLATFORM_ID) private platformId: Object ) {} ngOnInit() { + this.titleService.setTitle('Login | itzMiney') this.isMobile = this.deviceService.isMobile; if (isPlatformBrowser(this.platformId)) { window.addEventListener('resize', this.onResize.bind(this)); diff --git a/src/app/not-found/not-found.component.ts b/src/app/not-found/not-found.component.ts index 90f4ecf..2cbab1d 100644 --- a/src/app/not-found/not-found.component.ts +++ b/src/app/not-found/not-found.component.ts @@ -2,6 +2,7 @@ import {AfterViewInit, Component, Inject, OnDestroy, OnInit, PLATFORM_ID} from ' import {DeviceDetectorService} from '../services/device-detector.service'; import {VantaBackgroundService} from '../services/vanta-background.service'; import {isPlatformBrowser} from '@angular/common'; +import {Title} from '@angular/platform-browser'; @Component({ selector: 'app-not-found', @@ -16,11 +17,13 @@ export class NotFoundComponent implements OnInit, OnDestroy, AfterViewInit { constructor( private vantaService: VantaBackgroundService, private deviceService: DeviceDetectorService, + private titleService: Title, @Inject(PLATFORM_ID) private platformId: Object ) { } ngOnInit() { + this.titleService.setTitle('404 | Page Not Found') this.isMobile = this.deviceService.isMobile; if (isPlatformBrowser(this.platformId)) { window.addEventListener('resize', this.onResize.bind(this)); diff --git a/src/app/portfolio/portfolio.component.ts b/src/app/portfolio/portfolio.component.ts index 7720ee2..d1c160f 100644 --- a/src/app/portfolio/portfolio.component.ts +++ b/src/app/portfolio/portfolio.component.ts @@ -2,6 +2,7 @@ import {AfterViewInit, Component, Inject, OnDestroy, OnInit, PLATFORM_ID} from ' import {VantaBackgroundService} from '../services/vanta-background.service'; import {DeviceDetectorService} from '../services/device-detector.service'; import {isPlatformBrowser, NgStyle} from '@angular/common'; +import {Title} from '@angular/platform-browser'; @Component({ selector: 'app-portfolio', @@ -17,11 +18,12 @@ export class PortfolioComponent implements OnInit, OnDestroy, AfterViewInit { constructor( private vantaService: VantaBackgroundService, private deviceService: DeviceDetectorService, + private titleService: Title, @Inject(PLATFORM_ID) private platformId: Object - ) { - } + ) {} ngOnInit() { + this.titleService.setTitle('Portfolio | itzMiney'); this.isMobile = this.deviceService.isMobile; if (isPlatformBrowser(this.platformId)) { window.addEventListener('resize', this.onResize.bind(this)); diff --git a/src/app/services/update-meta.service.ts b/src/app/services/update-meta.service.ts index b7e0a6f..e69de29 100644 --- a/src/app/services/update-meta.service.ts +++ b/src/app/services/update-meta.service.ts @@ -1,31 +0,0 @@ -import { Injectable } from '@angular/core'; -import { Title } from '@angular/platform-browser'; -import { Meta } from '@angular/platform-browser'; - -@Injectable({ - providedIn: 'root' -}) -export class UpdateMetaService { - - constructor(private titleService: Title, private metaService: Meta) {} - - updateMetaTags(meta: { title?: string; description?: string; image?: string; url?: string; color?: string }) { - if (meta.title) { - this.titleService.setTitle(meta.title); - this.metaService.updateTag({ name: 'og:title', content: meta.title }); - } - if (meta.description) { - this.metaService.updateTag({ name: 'description', content: meta.description }); - this.metaService.updateTag({ name: 'og:description', content: meta.description }); - } - if (meta.image) { - this.metaService.updateTag({ name: 'og:image', content: meta.image }); - } - if (meta.url) { - this.metaService.updateTag({ name: 'og:url', content: meta.url }); - } - if (meta.color) { - this.metaService.updateTag({ name: 'theme-color', content: meta.color }); - } - } -}