import { Component , OnInit , Input , OnDestroy , Output , EventEmitter } from '@angular/core' ; import { OrganizationPermissionsEnum , ITimeSlot , IScreenshot , ITimeLog } from '@gauzy/contracts' ; import { NbDialogService } from '@nebular/theme' ; import { TimesheetService } from '../../timesheet.service' ; import { GalleryItem } from '../../../gallery/gallery.directive' ; import { isEmpty , progressStatus , toLocal } from '@gauzy/common-angular' ; import { ViewScreenshotsModalComponent } from '../view-screenshots-modal/view-screenshots-modal.component' ; import * as _ from 'underscore' ; import { GalleryService } from '../../../gallery/gallery.service' ; import { DEFAULT_SVG } from './../../../../@core/constants/app.constants' ; @ Component ( { selector : 'ngx-screenshots-item' , templateUrl : './screenshots-item.component.html' , styleUrls : [ './screenshots-item.component.scss' ] } ) export class ScreenshotsItemComponent implements OnInit , OnDestroy { private _screenshots : IScreenshot [ ] = [ ] ; private _timeSlot : ITimeSlot ; OrganizationPermissionsEnum = OrganizationPermissionsEnum ; progressStatus = progressStatus ; @ Input ( ) selectionMode = false ; @ Input ( ) galleryItems : GalleryItem [ ] = [ ] ; @ Input ( ) isSelected : boolean ; @ Output ( ) delete : EventEmitter < any > = new EventEmitter ( ) ; @ Output ( ) toggle : EventEmitter < any > = new EventEmitter ( ) ; @ Input ( ) public get timeSlot ( ) : ITimeSlot { return this . _timeSlot ; } public set timeSlot ( timeSlot ) { if ( timeSlot ) { timeSlot . localStartedAt = toLocal ( timeSlot . startedAt ) . toDate ( ) ; timeSlot . localStoppedAt = toLocal ( timeSlot . stoppedAt ) . toDate ( ) ; timeSlot . isAllowDelete = this . isEnableDelete ( timeSlot ) ; this . _timeSlot = timeSlot ; const screenshots = JSON . parse ( JSON . stringify ( timeSlot . screenshots ) ) ; this . _screenshots = _ . sortBy ( screenshots , 'createdAt' ) . reverse ( ) ; if ( this . _screenshots . length ) { const [ last ] = this . _screenshots ; this . lastScreenshot = last ; } } } private _lastScreenshot : IScreenshot ; public get lastScreenshot ( ) : IScreenshot { return this . _lastScreenshot ; } public set lastScreenshot ( screenshot ) { this . _lastScreenshot = screenshot ; } fallbackSvg = DEFAULT_SVG ; constructor ( private readonly nbDialogService , private readonly timesheetService , private readonly galleryService ) { } ngOnInit ( ) { } toggleSelect ( timeSlot ) { if ( timeSlot . isAllowDelete ) { const slotId = timeSlot . id ; this . toggle . emit ( slotId ) ; } } deleteSlot ( timeSlot ) { this . timesheetService . deleteTimeSlots ( [ timeSlot . id ] ) . then ( ( ) => { const screenshots = this . _screenshots . map ( ( screenshot ) => { return { thumbUrl : screenshot . thumbUrl , fullUrl : screenshot . fullUrl , ... screenshot } ; } ) ; this . galleryService . removeGalleryItems ( screenshots ) ; this . delete . emit ( ) ; } ) ; } viewInfo ( timeSlot ) { this . nbDialogService . open ( ViewScreenshotsModalComponent , { context : { timeSlot } } ) ; } isEnableDelete ( timeSlot ) { let isAllow = true ; if ( timeSlot . timeLogs . length > 0 ) { isAllow = ! timeSlot . timeLogs . find ( ( log ) => isEmpty ( log . stoppedAt ) ) ; } return isAllow ; } ngOnDestroy ( ) { } }