import { Component, ViewChild, OnInit, OnDestroy } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { NgForm } from '@angular/forms';
import { TimeRecord } from 'src/app/models/timerecords.model';
import { TimerecordsService } from 'src/app/services/timerecords.service';
import { merge, of as observableOf } from 'rxjs';
import { catchError, startWith, switchMap } from 'rxjs/operators';
@Component({
selector: 'app-edit-timerecord',
templateUrl: './edit-timerecord.component.html',
styleUrls: ['./edit-timerecord.component.css']
})
export class EditTimerecordComponent implements OnInit, OnDestroy {
//*get form reference in .html
@ViewChild('f', { static: false }) inputForm: NgForm;
//*data
public timeRecord: TimeRecord;
//--< mapping >--
idtimeRecord: number; //*map to idtimeRecord
iduserguid: String;
project: String;
activity: String;
dtStart: Date; //*2021-01-20T08:00:00 Json, ISO Dates (Date-Time)
dtEnd: Date;
DrivingTime: number;
PauseTime: number;
sDateStart: String;
sTimeStart: String;
sTimeEnd: String;
sTimeDriving: String;
sTimePause: String;
sDuration: String
dtEdit: Date;
//--</ mapping >--
//< variables >
public resultsLength = 0;
public isLoadingResults = true;
public isHttpError = false;
public sError_Message="Web Data error or http request error";
//</ variables >
// public color: ThemePalette = 'primary';
// public mode: ProgressSpinnerMode = 'indeterminate';
constructor(
public timerecordsService: TimerecordsService,
private route: ActivatedRoute, //*get ID
) { }
ngOnInit(): void {
//--------< ngOnInit() >--------
this.route.params.subscribe(params => {
//< get ID >
//*get ID from URL like timerecords/1
this.idtimeRecord = +params['id']; // (+) converts string 'id' to a number
console.log(this.idtimeRecord);
//</ get ID >
//< get Dataset >
this.load_DataSource();
//</ get Dataset >
});
//--------</ ngOnInit() >--------
}
ngOnDestroy() {
}
//#region
//====< functions >====
load_DataSource() {
//--------< load_DataSource() >--------
..
//--------</ load_DataSource() >--------
}
save_create({ value, valid }) {
if (valid) {
this.set_Dataset(); //set local Data to Dataset
this.timerecordsService.update_create_Dataset(this.idtimeRecord,this.timeRecord).subscribe(res => {
if (this.sError_Message == 'Error when saving on server') {
this.isHttpError = true;
setTimeout(function() {
this.errorMsg = false;
});//.bind(this), 2000);
} else {
this.isHttpError = false;
setTimeout(function() {
this.successMsg = false;
}); //.bind(this), 2000);
//this.load_DataSource(); //refresh Data from server
}
});
} else {
console.log('Form is not valid.');
}
}
map_Data() {
//--------< map_Data() >--------
//*map Recordset to local variables
console.log("timerecord=" + this.timeRecord);
this.idtimeRecord = this.timeRecord.idtimeRecord;
this.iduserguid = this.timeRecord.iduserguid;
this.project = this.timeRecord.project;
this.activity = this.timeRecord.activity;
this.dtStart = new Date(this.timeRecord.dtStart); //
this.dtEnd = new Date(this.timeRecord.dtEnd);
//console.log("dtStart=" + this.dtStart.toString());
console.log("dtEnd=" + this.dtEnd.toString());
//< convert iso Date >
//*convert Iso-Date-Time to Javascript Date: 2021-01-20T08:00:00
this.sDateStart = this.get_Date_String_from_Date(this.dtStart);
this.sTimeStart = this.get_Time_String_from_Date(this.dtStart);
this.sTimeEnd = this.get_Time_String_from_Date(this.dtEnd);
this.sTimeDriving = this.get_Time_String_from_Float(this.DrivingTime);
this.sTimePause = this.get_Time_String_from_Float(this.PauseTime);
this.calculate_Duration();
//</ convert iso Date >
this.idtimeRecord = this.timeRecord.idtimeRecord;
//--------</ map_Data() >--------
}
set_Dataset() {
//--------< set_Dataset() >--------
//*map Recordset to local variables
console.log("set_dataset:");
this.timeRecord.project=this.project;
this.timeRecord.activity=this.activity;
this.timeRecord.dtStart=this.dtStart;
this.timeRecord.dtEnd=this.dtEnd;
//--------</ set_Dataset() >--------
}
onInput_Change($event) {
//--------< onInput_Change() >--------
//*Create a Date from ISO String
//*String "2021-01-20" + "T" + "08:00:00"
this.dtStart = new Date(this.sDateStart + "T" + this.sTimeStart + ":00");
this.dtEnd = new Date(this.sDateStart + "T" + this.sTimeEnd + ":00");
this.DrivingTime = this.get_DecimalNumber_from_Time_String(this.sTimeDriving);
this.PauseTime = this.get_DecimalNumber_from_Time_String(this.sTimePause);
this.calculate_Duration();
//--------</ onInput_Change() >--------
}
|