File

src/app/timesheets-list/timesheets-list.component.ts

Implements

OnInit

Metadata

Index

Properties
Methods

Constructor

constructor(apiService: ApiService, spinner: NgxSpinnerService, router: Router, toastr: ToastrService)
Parameters :
Name Type Optional
apiService ApiService No
spinner NgxSpinnerService No
router Router No
toastr ToastrService No

Methods

addTimesheet
addTimesheet()
Returns : void
mapMonth
mapMonth(month: number)
Parameters :
Name Type Optional
month number No
Returns : string
mapStatus
mapStatus(status: string)
Parameters :
Name Type Optional
status string No
Returns : string
mapTotalHours
mapTotalHours(details: TimesheetDetails[])
Parameters :
Name Type Optional
details TimesheetDetails[] No
Returns : number
ngOnInit
ngOnInit()
Returns : void

Properties

Public timesheetList
Type : Timesheet[]
Default value : []
Private userId
Type : string
import { Component, OnInit } from '@angular/core';
import { ApiService } from '../services/api.service';
import { User } from '../../app/models/user'
import { Timesheet } from '../models/timesheet';
import { mapMonth, mapStatus } from './timesheet-list.utils';
import { NgxSpinnerService } from 'ngx-spinner';
import { Router } from '@angular/router';
import { TimesheetDetails } from '../models/timesheet-details';
import { ToastrService } from 'ngx-toastr';

@Component({
  selector: 'app-timesheets-list',
  templateUrl: './timesheets-list.component.html',
  styleUrls: ['./timesheets-list.component.css']
})
export class TimesheetsListComponent implements OnInit {

  private userId: string;
  public timesheetList: Timesheet[] = []

  constructor(private apiService: ApiService, private spinner: NgxSpinnerService, private router: Router, private toastr: ToastrService,) { }

  ngOnInit(): void {
    this.spinner.show()
    this.userId = (JSON.parse(localStorage.getItem("currentUser"))as unknown as User).id.toString()
    this.apiService.GetTimesheetList(this.userId).subscribe(response => {
      this.timesheetList = response
      this.spinner.hide()
    })
  }

  addTimesheet(){
    let currentDate =new Date()
    const currentMonth= currentDate.getMonth() + 1
    const currentYear = currentDate.getFullYear()
    if(!this.timesheetList.map(sheet => sheet.month).includes(currentMonth)){
      this.apiService.CreateTimesheet({month: currentMonth, year: currentYear, timesheetStatus: 'D', userId: this.userId} as Timesheet).subscribe(
        val => {
          this.toastr.success("Timesheed added succesfully!")
          this.router.navigate([`/timesheet/${(val as Timesheet).timesheetID}`])
        }
      )
    } else if(!this.timesheetList.map(sheet => sheet.month).includes(currentMonth + 1)){
      currentDate.setMonth(currentDate.getMonth() + 1)
      this.apiService.CreateTimesheet({month: currentDate.getMonth() + 1, year: currentYear, timesheetStatus: 'D', userId: this.userId} as Timesheet).subscribe(
        val => {
          this.toastr.success("Timesheed added succesfully!")
          this.router.navigate([`/timesheet/${(val as Timesheet).timesheetID}`])
        }
      )
    } else {
      this.toastr.warning("You already have enough timesheets!")
    }
  }

  mapMonth(month: number): string {
    return mapMonth(month);
  }

  mapStatus(status: string): string {
    return mapStatus(status);
  }

  mapTotalHours(details: TimesheetDetails[]): number{
    let hoursReporterd = 0;
    details.forEach(
      detail => {
        hoursReporterd += detail.registeredHours;
      }
    )
    return hoursReporterd;
  }

}
<div class="page-header pb-10 page-header-dark bg-primary">
    <div class="container-fluid">
        <div class="page-header-content">
            <h1 class="page-header-title">
                <div class="page-header-icon"><i class="fas fa-calendar"></i></div>
                <span>Timesheets</span>
                <div class="btn btn-success ml-5" style="text-align: right; justify-content: right;" (click)="addTimesheet()">
                    <i class="fas fa-plus-circle"></i>&nbsp;&nbsp; Add new Timesheet
                </div>
            </h1>
            <div class="page-header-subtitle">Your Timesheets!</div>
        </div>
    </div>
</div>
<ngx-spinner bdOpacity=0.5 bdColor="rgba(51,51,51,0.85)" size="medium" color="#fff" type="line-scale" [fullScreen]="true">
    <p style="color: white"> Openning connection to our database... </p>
</ngx-spinner>
<div class="container-fluid mt-n10">
        <div class="card card-header-actions mb-2 lift" *ngFor="let timesheet of timesheetList, let i = index">
            <div class="card-header" [routerLink]="['/timesheet', timesheet.timesheetID]">
                <h1 class="mt-2">{{mapMonth(timesheet.month)}} {{timesheet.year}}</h1>
                <div style="text-align: right; justify-content: right; display:inline-block;">
                    <!-- <button class="btn btn-outline-primary mr-3" [routerLink]="['/timesheet', timesheet.timesheetID]" style="text-align: center; justify-content: center;">
                        Details
                    </button> -->
                    <button disabled class="btn btn-primary mr-3" style="justify-content: right; display:inline-block; width: 160px;">
                        {{mapTotalHours(timesheet.timesheetDetails)}}h Reported
                    </button>  
                    <button disabled [ngClass]="timesheet.timesheetStatus == 'A' ? 'btn btn-success' :  
                        timesheet.timesheetStatus == 'P' ?
                        'btn btn-secondary' :
                        timesheet.timesheetStatus == 'D' ?
                        'btn btn-warning' :
                        'btn btn-danger'" style="display:inline-block; width: 160px;">
                        {{mapStatus(timesheet.timesheetStatus)}}
                    </button>
                </div>
            </div>
        </div>
</div>

./timesheets-list.component.css

Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""