import { Component, EventEmitter, HostBinding, Input, OnInit, Output } from '@angular/core'; import { Store } from '@ngrx/store'; import { CatalogEntry, Class, ClassActions, ClassSelectors, ConfigActions, FieldActions, NamedQuery, NamedQueryActions, NamedQuerySelectors, SearchActions, ToolboxManifestService, ToolboxMenuService, LibraryTypes, Widget, WidgetManifest, YapVisibility, YapVisibilityType } from '@onepoint-yap/dashboard'; import { YapStepperComponent } from '@onepoint-yap/dashboard/components/stepper'; import { map, Observable, Subject } from 'rxjs'; import { v4 } from 'uuid'; import { RestitutionForm } from '../../model/restitution.model'; import { TranslateService } from '@ngx-translate/core'; @Component({ selector: 'yap-restitution', templateUrl: './restitution.component.html' }) export class YapRestitutionComponent implements OnInit { namedQueries$: Observable; types$: Observable; classes$: Observable; customManifest: WidgetManifest = { type: 'detail', header: true, layout: { x: 1, y: 1, width: 1, height: 1 }, options: { itemIds: [] as string[] } }; form: RestitutionForm = { selectedWidget: '', selectedNameQueries: [], title: '', image: '', description: '' }; visibility: YapVisibility = { type: YapVisibilityType.PRIVATE, users: [] }; openSettings$: Subject = new Subject(); users$: Observable | null = null; consultMode: boolean = false; @Input() selectedRestitution: Widget | null = null; @Input() edit: boolean = true; @Output() restitutionCreated = new EventEmitter(); widgetsList$: Observable; type: LibraryTypes = LibraryTypes.ILLUSTRATION; constructor( private toolboxManifestService: ToolboxManifestService, private store: Store, private toolboxMenuService: ToolboxMenuService, private translateService: TranslateService ) { this.store.dispatch(NamedQueryActions.load()); this.store.dispatch(ClassActions.load()); this.store.dispatch(ConfigActions.loadCustomization()); this.store.dispatch(FieldActions.load()); this.types$ = this.toolboxManifestService.types$.pipe( map((type) => type.filter((filteredType) => filteredType !== 'detail')) ); this.namedQueries$ = this.store.select(NamedQuerySelectors.getAllNamedQuery); this.classes$ = this.store.select(ClassSelectors.classes); this.widgetsList$ = this.toolboxMenuService.menuStructure$.pipe( map((menu) => menu .map((items) => [...items.sub.filter((s) => this.toolboxManifestService.isModel(s.type))]) .reduce((acc, items) => [...acc, ...items], []) ) ); } ngOnInit(): void { if (this.selectedRestitution) { if (!this.edit) { this.consultMode = true; } this.customManifest = JSON.parse(this.selectedRestitution.content); if (Array.isArray(this.customManifest.namedQuery)) { this.customManifest.namedQuery.forEach((nameQuery: string) => { this.store.dispatch(SearchActions.searchNamed({ id: nameQuery as string })); }); } else { this.store.dispatch(SearchActions.searchNamed({ id: this.customManifest.namedQuery as string })); } if (this.selectedRestitution.visibility) { this.visibility = this.selectedRestitution.visibility; } this.form.selectedNameQueries.forEach((nameQuery: string) => { this.store.dispatch(SearchActions.searchNamed({ id: nameQuery as string })); }); this.customManifest = JSON.parse(this.selectedRestitution.content); this.form = { selectedWidget: this.customManifest.type, selectedNameQueries: this.customManifest.namedQuery as string[], title: this.selectedRestitution.name, ...this.selectedRestitution }; } } cancel(stepper: YapStepperComponent) { this.form.selectedWidget = ''; this.form.selectedNameQueries = []; this.form.title = ''; this.form.description = ''; this.form.image = ''; if (this.consultMode) { this.restitutionCreated.emit(); return; } this.prev(stepper); } attributesChanged(namedQueries: Array) { this.form.selectedNameQueries = namedQueries; } submit(stepper: YapStepperComponent) { if (this.consultMode) { this.goNext(stepper); return; } this.customManifest = { ...this.customManifest, type: this.form.selectedWidget, title: this.form.title, namedQuery: this.form.selectedNameQueries, options: { ...this.customManifest.options, title: this.form.title } }; this.form.selectedNameQueries.forEach((nameQuery: string) => { this.store.dispatch(SearchActions.searchNamed({ id: nameQuery as string })); }); this.goNext(stepper); setTimeout(() => { this.openSettings$.next(); }, 150); } isSubmitDisabled() { return ( this.form.selectedWidget === '' || this.form.selectedNameQueries.length === 0 || this.form.title.trim() === '' ); } goNext(stepper: YapStepperComponent) { stepper.next(); } prev(stepper: YapStepperComponent) { stepper.prev(); } updateManifest($event: { widgetIndex: number; manifest: WidgetManifest }) { this.customManifest = $event.manifest; } lastStepCompleted() { if (this.edit) { this.store.dispatch( ConfigActions.saveWidget({ widget: { id: this.selectedRestitution?.id ?? v4(), content: JSON.stringify(this.customManifest), name: this.form.title, description: this.form.description, image: this.form.image ?? '', visibility: this.visibility, namedQueries: this.form.selectedNameQueries } }) ); } this.restitutionCreated.emit(); } translation = (item: any) => (item.length > 0 ? this.translateService.instant('@yap.toolbox.' + item) : ''); }