import { Component } from '@angular/core'; import { DomSanitizer, SafeHtml } from '@angular/platform-browser'; import { ActivatedRoute, Router } from '@angular/router'; import { KeycloakService } from 'keycloak-angular'; import onChange from 'on-change'; import { MessageService } from 'primeng/api'; import { GalleriaModule } from 'primeng/galleria'; import { lastValueFrom } from 'rxjs'; import { CommercialPropertyListing, User } from '../../../../../../bizmatch-server/src/models/db.model'; import { ErrorResponse, KeycloakUser, ListingCriteria, MailInfo } from '../../../../../../bizmatch-server/src/models/main.model'; import { environment } from '../../../../environments/environment'; import { HistoryService } from '../../../services/history.service'; import { ImageService } from '../../../services/image.service'; import { ListingsService } from '../../../services/listings.service'; import { MailService } from '../../../services/mail.service'; import { SelectOptionsService } from '../../../services/select-options.service'; import { UserService } from '../../../services/user.service'; import { SharedModule } from '../../../shared/shared/shared.module'; import { getCriteriaStateObject, getSessionStorageHandler, map2User } from '../../../utils/utils'; @Component({ selector: 'app-details-commercial-property-listing', standalone: true, imports: [SharedModule, GalleriaModule], providers: [MessageService], templateUrl: './details-commercial-property-listing.component.html', styleUrl: './details-commercial-property-listing.component.scss', }) export class DetailsCommercialPropertyListingComponent { responsiveOptions = [ { breakpoint: '1199px', numVisible: 1, numScroll: 1, }, { breakpoint: '991px', numVisible: 2, numScroll: 1, }, { breakpoint: '767px', numVisible: 1, numScroll: 1, }, ]; private id: string | undefined = this.activatedRoute.snapshot.params['id'] as string | undefined; listing: CommercialPropertyListing; criteria: ListingCriteria; mailinfo: MailInfo; environment = environment; keycloakUser: KeycloakUser; user: User; listingUser: User; description: SafeHtml; ts = new Date().getTime(); env = environment; errorResponse: ErrorResponse; constructor( private activatedRoute: ActivatedRoute, private listingsService: ListingsService, private router: Router, private userService: UserService, public selectOptions: SelectOptionsService, private mailService: MailService, private messageService: MessageService, private sanitizer: DomSanitizer, public historyService: HistoryService, public keycloakService: KeycloakService, private imageService: ImageService, ) { this.mailinfo = { sender: {}, userId: '', email: '', url: environment.mailinfoUrl }; this.criteria = onChange(getCriteriaStateObject(), getSessionStorageHandler); } async ngOnInit() { const token = await this.keycloakService.getToken(); this.keycloakUser = map2User(token); if (this.keycloakUser) { this.user = await this.userService.getByMail(this.keycloakUser.email); this.mailinfo.sender = { name: `${this.user.firstname} ${this.user.lastname}`, email: this.user.email, phoneNumber: this.user.email, state: this.user.companyLocation }; } this.listing = await lastValueFrom(this.listingsService.getListingById(this.id, 'commercialProperty')); this.listingUser = await this.userService.getById(this.listing.userId); this.description = this.sanitizer.bypassSecurityTrustHtml(this.listing.description); } isAdmin() { return this.keycloakService.getUserRoles(true).includes('ADMIN'); } async mail() { this.mailinfo.email = this.listingUser.email; this.mailinfo.userId = this.listing.userId; this.mailinfo.listing = this.listing; const result = await this.mailService.mail(this.mailinfo); if (result) { this.errorResponse = result as ErrorResponse; } else { this.errorResponse = null; this.messageService.add({ severity: 'info', summary: 'Confirmed', detail: 'Your message has been sent to the creator of the listing', life: 3000 }); } } containsError(fieldname: string) { return this.errorResponse?.fields.map(f => f.fieldname).includes(fieldname); } }