List discussions in the sidebar

This commit is contained in:
Michał Romaszkin 2020-06-14 00:49:03 +02:00
parent 13ecb753a5
commit e0931f2ca9
5 changed files with 39 additions and 12 deletions

View File

@ -11,6 +11,7 @@ import {
NbThemeModule, NbThemeModule,
NbSidebarModule, NbSidebarModule,
NbSidebarService, NbSidebarService,
NbMenuModule,
} from '@nebular/theme'; } from '@nebular/theme';
import { FrontPageModule } from './front-page/front-page.module'; import { FrontPageModule } from './front-page/front-page.module';
@ -25,6 +26,7 @@ import { SharedDataService } from './_services/shared-data.service';
HttpClientModule, HttpClientModule,
NbThemeModule.forRoot(), NbThemeModule.forRoot(),
NbSidebarModule.forRoot(), NbSidebarModule.forRoot(),
NbMenuModule.forRoot(),
NbEvaIconsModule, NbEvaIconsModule,
FrontPageModule, FrontPageModule,
], ],

View File

@ -3,11 +3,13 @@ import {
OnInit, OnInit,
OnDestroy, OnDestroy,
ChangeDetectionStrategy, ChangeDetectionStrategy,
AfterContentInit, EventEmitter,
Output,
} from '@angular/core'; } from '@angular/core';
import { SharedDataService } from '../../_services/shared-data.service'; import { SharedDataService } from '../../_services/shared-data.service';
import { Colors } from '../../_types/color'; import { Colors } from '../../_types/color';
import { Subscription } from 'rxjs'; import { Subscription } from 'rxjs';
import { NbMenuItem } from '@nebular/theme';
interface CustomForumData { interface CustomForumData {
id: string; id: string;
@ -30,15 +32,26 @@ export class DiscussionChooserComponent implements OnInit, OnDestroy {
public data: CustomForumData; public data: CustomForumData;
colors: Colors[] = ['primary', 'danger', 'info', 'success', 'warning']; colors: Colors[] = ['primary', 'danger', 'info', 'success', 'warning'];
private dataSub: Subscription; private dataSub: Subscription;
@Output() discussions = new EventEmitter<NbMenuItem[]>();
constructor(private sharedDataService: SharedDataService) {} constructor(private sharedDataService: SharedDataService) {}
ngOnInit(): void { ngOnInit(): void {
this.dataSub = this.sharedDataService.getData().subscribe((res) => { this.dataSub = this.sharedDataService.getData().subscribe((res) => {
if (res) { if (res) {
const obj = JSON.parse(res as string); const parsedObj = JSON.parse(res as string);
this.data = obj as CustomForumData; this.data = parsedObj as CustomForumData;
console.log(this.data);
const fetchedDiscussions = this.data.discussions.map(
(element): NbMenuItem => {
return {
title: element.title,
link: '/view/discussion',
};
}
);
this.discussions.emit(fetchedDiscussions);
} }
}); });
} }

View File

@ -16,8 +16,10 @@
</nb-actions> </nb-actions>
</div> </div>
</nb-layout-header> </nb-layout-header>
<nb-sidebar tag="main"></nb-sidebar> <nb-sidebar tag="main">
<nb-menu [items]="items"></nb-menu>
</nb-sidebar>
<nb-layout-column> <nb-layout-column>
<router-outlet></router-outlet> <router-outlet (activate)="onActivate($event)"></router-outlet>
</nb-layout-column> </nb-layout-column>
</nb-layout> </nb-layout>

View File

@ -1,23 +1,25 @@
import { Component, OnInit, OnDestroy } from '@angular/core'; import { Component, OnInit, OnDestroy } from '@angular/core';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { NbSidebarService } from '@nebular/theme'; import { NbSidebarService, NbMenuItem } from '@nebular/theme';
@Component({ @Component({
selector: 'app-main-view', selector: 'app-main-view',
templateUrl: './main-view.component.html', templateUrl: './main-view.component.html',
styleUrls: ['./main-view.component.scss'], styleUrls: ['./main-view.component.scss'],
}) })
export class MainViewComponent implements OnInit, OnDestroy { export class MainViewComponent {
items: NbMenuItem[] = [
{
title: 'Dyskusje',
link: '/view/discussions',
},
];
constructor( constructor(
private sidebarService: NbSidebarService, private sidebarService: NbSidebarService,
private router: Router private router: Router
) {} ) {}
ngOnInit(): void {}
ngOnDestroy(): void {}
toggleSidebar() { toggleSidebar() {
this.sidebarService.toggle(true, 'main'); this.sidebarService.toggle(true, 'main');
} }
@ -25,4 +27,10 @@ export class MainViewComponent implements OnInit, OnDestroy {
logout() { logout() {
this.router.navigate(['/']); this.router.navigate(['/']);
} }
onActivate(event: any) {
event.discussions.subscribe((res: NbMenuItem[]) => {
this.items[0].children = res;
});
}
} }

View File

@ -11,6 +11,7 @@ import {
NbSidebarModule, NbSidebarModule,
NbTooltipModule, NbTooltipModule,
NbCardModule, NbCardModule,
NbMenuModule,
} from '@nebular/theme'; } from '@nebular/theme';
@NgModule({ @NgModule({
@ -23,6 +24,7 @@ import {
NbSidebarModule, NbSidebarModule,
NbTooltipModule, NbTooltipModule,
NbCardModule, NbCardModule,
NbMenuModule,
], ],
}) })
export class MainViewModule {} export class MainViewModule {}