Advertisement
niamulhasan

suralist.component.ts

Apr 25th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. import {Component, OnInit} from '@angular/core';
  2. import {ApiService, Chapter, ChapterTranslation, ChapterType} from '../api.service';
  3.  
  4. @Component({
  5. selector: 'app-suralist',
  6. templateUrl: './suralist.component.html',
  7. styleUrls: ['./suralist.component.css']
  8. })
  9. export class SuralistComponent implements OnInit {
  10. chapters: Chapter[];
  11. suralist$: ChapterTranslation[];
  12. loading = true;
  13.  
  14. constructor(private api: ApiService) { }
  15.  
  16. ngOnInit() {
  17. const appLanguage = 'bn'; // from settings
  18. // Let see how strict typing help, and why OOP is this popular
  19. // and it is gold when you use strict typing + a good IDE such as: WebStorm, Visual Studio, VS Code
  20. // That's how be productive in work
  21. this.api.getChapters(appLanguage).subscribe(value => {
  22. this.chapters = value.data.chapters;
  23. console.log(this.chapters);
  24. const madaniChapters = this.chapters.filter(chapter => chapter.type === ChapterType.MADANI);
  25. console.log('Madani Chapters:');
  26. console.log(madaniChapters);
  27. // Lets grab translated Names
  28. const translatedNames = this.chapters.map(chapter => chapter.translation.name);
  29. console.log(translatedNames);
  30. // Let's be more dynamic // When language-based content not found
  31. // example if we change our appLanguage to a such language that does not exist
  32. // Add name_translated: string; in Chapter interface
  33. this.chapters = this.chapters.map(chapter => {
  34. chapter.name_translated = chapter.translation != null ? chapter.translation.name : chapter.name;
  35. // And now always use in ui/view: chapter.name_translated for a multi lang support app it is a technique
  36. this.loading = false;
  37. return chapter;
  38. });
  39. });
  40. // Your result
  41. // this.api.getChapterTranslations(appLanguage).subscribe(value => {
  42. // this.suralist$ = value.data.chapter_translations;
  43. // this.loading = false;
  44. // });
  45. }
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement