Skip to content

Commit 679e11f

Browse files
author
Salma Alam-Naylor
committed
Use a bespoke meta service to generate dynamic home page meta titles and meta descriptions
1 parent e765a8a commit 679e11f

File tree

3 files changed

+90
-21
lines changed

3 files changed

+90
-21
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { TestBed } from '@angular/core/testing';
2+
import { MetaService } from './meta.service';
3+
import { FretMapService } from '../fret-map/fret-map.service';
4+
5+
describe('MetaService', () => {
6+
let fretMapService: FretMapService;
7+
let metaService: MetaService;
8+
9+
beforeEach(() => {
10+
fretMapService = TestBed.inject(FretMapService);
11+
metaService = new MetaService(fretMapService);
12+
});
13+
14+
it('should be created', () => {
15+
expect(metaService).toBeTruthy();
16+
});
17+
18+
it('should call the fret map service for the title and return the correct string', () => {
19+
const result = metaService.generateHomePageTitle('c', 'natural', 'lydian');
20+
expect(result).toBe('Fretonator - the ultimate interactive free guitar theory tool | C Lydian');
21+
});
22+
23+
it('should call the fret map service for the description and return the correct string', () => {
24+
const result = metaService.generateHomePageMetaDescription('c', 'natural', 'lydian');
25+
expect(result).toBe('Learn the C Lydian mode on the guitar and play along to a jam track with the Fretonator. Choose a starting note, pick a mode, check out the fretboard and have a jam!');
26+
});
27+
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { Injectable } from '@angular/core';
2+
import { FretMapService } from '../fret-map/fret-map.service';
3+
4+
@Injectable({
5+
providedIn: 'root'
6+
})
7+
export class MetaService {
8+
9+
constructor(private fretMapService: FretMapService) {
10+
}
11+
12+
getBaseTitle = (): string => {
13+
return 'Fretonator - the ultimate interactive free guitar theory tool';
14+
};
15+
16+
getGenericEndDescription = (): string => {
17+
return 'Choose a starting note, pick a mode, check out the fretboard and have a jam!';
18+
}
19+
20+
generateHomePageTitle = (note,
21+
noteExtenderString,
22+
mode): string => {
23+
return this.getBaseTitle() + ' | ' +
24+
this.fretMapService.convertFretMapConfigurationToDisplayString(
25+
note,
26+
noteExtenderString,
27+
mode
28+
);
29+
};
30+
31+
generateHomePageMetaDescription = (note,
32+
noteExtenderString,
33+
mode): string => {
34+
return 'Learn the ' + this.fretMapService.convertFretMapConfigurationToDisplayString(
35+
note,
36+
noteExtenderString,
37+
mode
38+
) + ' mode on the guitar and play along to a jam track with the Fretonator. ' + this.getGenericEndDescription();
39+
};
40+
}

apps/fretonator-web/src/app/pages/home/home-index/home-index.component.ts

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Meta, Title } from '@angular/platform-browser';
55
import { AbstractDataService } from '../../../common/abstract-data/abstract-data.service';
66
import { ActivatedRoute } from '@angular/router';
77
import { FretMapService } from '../../../common/fret-map/fret-map.service';
8+
import { MetaService } from '../../../common/meta/meta.service';
89

910
@Component({
1011
selector: 'app-home-index',
@@ -20,24 +21,21 @@ export class HomeIndexComponent implements OnInit {
2021
modeSelectorObjects = ModeSelectorObjects;
2122
octave = Octave;
2223
showHowTo;
23-
metaExtender;
24-
24+
2525
constructor(
2626
private title: Title,
2727
private meta: Meta,
2828
private localStorage: AbstractDataService,
2929
private activatedRoute: ActivatedRoute,
30-
private fretMapService: FretMapService
31-
) {}
30+
private fretMapService: FretMapService,
31+
private metaService: MetaService
32+
) {
33+
}
3234

3335
ngOnInit(): void {
3436
this.activatedRoute.params.subscribe(() => this.onRouteChange());
35-
36-
this.title.setTitle('Fretonator - the ultimate interactive free guitar theory tool' + this.metaExtender);
37-
this.meta.updateTag({
38-
name: 'description',
39-
content: 'The ultimate interactive free guitar theory tool. Choose a starting note, pick a mode, check out the fretboard and have a jam!'
40-
});
37+
this.setHomePageTitle();
38+
this.setHomePageMetaDescription();
4139

4240
const _showHowTo = this.localStorage.getItem('showHowTo');
4341
switch (_showHowTo) {
@@ -71,21 +69,25 @@ export class HomeIndexComponent implements OnInit {
7169
this.noteExtender = NoteExtenderSymbol.natural;
7270
}
7371

74-
this.metaExtender =
75-
' | ' +
76-
this.fretMapService.convertFretMapConfigurationToDisplayString(
77-
this.note,
78-
this.noteExtenderString,
79-
this.mode
80-
);
81-
this.title.setTitle(
82-
'Fretonator - the ultimate interactive free guitar theory tool' +
83-
this.metaExtender
84-
);
72+
this.setHomePageTitle();
73+
this.setHomePageMetaDescription();
8574
}
8675

8776
toggleHowTo() {
8877
this.showHowTo = !this.showHowTo;
8978
this.localStorage.setItem('showHowTo', this.showHowTo);
9079
}
80+
81+
setHomePageTitle() {
82+
this.title.setTitle(
83+
this.metaService.generateHomePageTitle(this.note, this.noteExtenderString, this.mode)
84+
);
85+
}
86+
87+
setHomePageMetaDescription() {
88+
this.meta.updateTag({
89+
name: 'description',
90+
content: this.metaService.generateHomePageMetaDescription(this.note, this.noteExtenderString, this.mode)
91+
});
92+
}
9193
}

0 commit comments

Comments
 (0)