import { CommonModule } from '@angular/common'; import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'; import { NgbModule, NgbPopover } from '@ng-bootstrap/ng-bootstrap'; import { RatingReviewsRoutes } from './utils/svg-routes/ratings-reviews.routes'; import { AngularSvgIconModule } from 'angular-svg-icon'; import { HttpClientModule } from '@angular/common/http'; import { FormsModule, ReactiveFormsModule, UntypedFormBuilder, UntypedFormGroup, } from '@angular/forms'; //components import { CaAppTooltipV2Component } from '../ca-app-tooltip-v2/ca-app-tooltip-v2.component'; import { CaRatingsReviewsPopupComponent } from './components/ca-ratings-reviews-popup/ca-ratings-reviews-popup.component'; import { CaRatingReviewUserComponent } from './components/ca-rating-review-user/ca-rating-review-user.component'; import { CaInputComponent } from '../ca-input/ca-input.component'; //enums import { RatingReviewsEnum, RatingReviewsTypeEnum, } from './enums/rating-reviews-type.enums'; //config import { ICaInput } from '../ca-input/config/ca-input.config'; import { getRatingReviewSearch } from './utils/config/rating-review-search.config'; //models import { RatingReviewModel } from './models/rating-review.model'; //pipes import { FilterByTitlePipe } from './utils/pipe/filter-title.pipe'; //helpers import { RatingReviewHelper } from './utils/helpers/sort-data.helper'; @Component({ selector: 'lib-ca-rating-review', templateUrl: './ca-rating-review.component.html', styleUrls: ['./ca-rating-review.component.scss'], imports: [ // modules CommonModule, NgbModule, AngularSvgIconModule, HttpClientModule, NgbModule, FormsModule, ReactiveFormsModule, // Components CaAppTooltipV2Component, CaRatingsReviewsPopupComponent, CaInputComponent, CaRatingReviewUserComponent, //pipes FilterByTitlePipe, ] }) export class CaRatingReviewComponent implements OnInit { @Input() ratingReviewList: RatingReviewModel[] = []; @Input() companyUser!: RatingReviewModel; @Input() isFilter: boolean = false; @Input() isSeaarch: boolean = false; @Input() isNewReview: boolean = false; @Output() ratingReviewData = new EventEmitter<{ data: RatingReviewModel; action: string; }>(); public likes: number = 0; public disLikes: number = 0; public reviews: number = 0; public filterList: RatingReviewModel[] = []; public isPopoverOpen: boolean = false; public RatingReviewsType: RatingReviewsTypeEnum = RatingReviewsTypeEnum.All; public isLikeClicked: boolean = false; public isDisLikesClicked: boolean = false; public isReviewsClicked: boolean = false; public isSortedByDate: boolean = false; public searchForm!: UntypedFormGroup; constructor(private formBuilder: UntypedFormBuilder) {} ngOnInit(): void { this.createForm(); this.getListDate(); } public createForm(): void { this.searchForm = this.formBuilder.group({ search: null, }); } public getListDate(): void { this.filterList = this.ratingReviewList; if (this.ratingReviewList?.length) { this.checkLikesReviewNumner(); } } public getSvgPath(propertyName: keyof typeof RatingReviewsRoutes): string { return RatingReviewsRoutes[propertyName] as string; } get ratingReviewSearch(): ICaInput { return getRatingReviewSearch.getRatingReviewSearch(); } public data(event: { data: RatingReviewModel; action: string }): void { if (event.action === RatingReviewsEnum.DELETE) { this.ratingReviewList = this.ratingReviewList.filter( (ratingsReviews: RatingReviewModel) => ratingsReviews.id !== event.data.id ); this.filterList = this.ratingReviewList; this.ratingReviewData.emit(event); } else { if (!this.companyUser?.user) { this.ratingReviewList?.map((ratingReview: RatingReviewModel) => { if (ratingReview.id === event.data.id) { ratingReview = { ...ratingReview, ...event.data, }; } }); } else { this.ratingReviewList?.push(event.data); } this.ratingReviewData.emit(event); } this.isNewReview = false; this.checkLikesReviewNumner(); } public checkLikesReviewNumner(): void { this.likes = this.ratingReviewList.filter( (ratingsReview: RatingReviewModel) => { return ratingsReview.thumb === 1; } ).length; this.disLikes = this.ratingReviewList.filter( (ratingsReview: RatingReviewModel) => { return ratingsReview.thumb === -1; } ).length; this.reviews = this.ratingReviewList.filter( (ratingsReview: RatingReviewModel) => { return ratingsReview.comment; } ).length; } public sortByDate(): void { this.isSortedByDate = !this.isSortedByDate; RatingReviewHelper.sortByDate(this.isSortedByDate, this.ratingReviewList); } public openPopover(t2: NgbPopover): void { t2.open(); this.isPopoverOpen = true; } public closePopover(t2: NgbPopover): void { this.isPopoverOpen = false; t2.close(); } public removeFilters(): void { this.isLikeClicked = false; this.isDisLikesClicked = false; this.isReviewsClicked = false; this.filterList = this.ratingReviewList; } public showFilters(filter: string): void { this.isLikeClicked = false; this.isDisLikesClicked = false; this.isReviewsClicked = false; if (filter === RatingReviewsEnum.LIKES) { this.filterList = this.ratingReviewList.filter( (ratingsReview: RatingReviewModel) => { return ratingsReview.thumb === 1; } ); this.isLikeClicked = true; } else if (filter === RatingReviewsEnum.DISLIKES) { this.filterList = this.ratingReviewList.filter( (ratingsReview: RatingReviewModel) => { return ratingsReview.thumb === -1; } ); this.isDisLikesClicked = true; } else if (filter === RatingReviewsEnum.REVIEWS) { this.filterList = this.ratingReviewList.filter( (ratingsReview: RatingReviewModel) => { return ratingsReview.comment; } ); this.isReviewsClicked = true; } } }