import { Component, ViewChild, ElementRef, OnInit, OnDestroy } from '@angular/core'; import { AlertController, ToastController, Events } from 'ionic-angular'; import { DualValue,HourRange, DateIdUtil } from '@canvuus-internal/mvp0-task-core'; import { AppUser, AppUserHelper } from '@canvuus-internal/mvp0-task-base'; import { UserProfile, UserPreference, UserProfileHelper, UserPreferenceHelper } from '@canvuus-internal/mvp0-task-data'; import { CommonColorAvatarComponent } from '../common-color-avatar/common-color-avatar'; import { CommonEvents } from '../../common/events/common-events'; @Component({ selector: 'mvp0-user-settings', template: ` My Profile App Preferences Morning Range 0 24 Evening Range 0 24 Data Settings Delete all data Export data ` }) export class Mvp0UserSettingsComponent implements OnInit, OnDestroy { @ViewChild('exportedDataArea') dataArea; private _originalUserProfile: UserProfile; userProfile: UserProfile; private _originalUserPreference: UserPreference; userPreference: UserPreference; // // temporary. userPreference.HourRange does not seem to work with ngModel data binding... // amHours: DualValue; // pmHours: DualValue; constructor( private elementRef: ElementRef, private alertCtrl: AlertController, private toastCtrl: ToastController, private events: Events) { // console.log('Mvp0UserSettingsComponent: Hello Mvp0UserSettings Component'); // temporary this.userProfile = new UserProfile(); this.userPreference = new UserPreference(); // .... // // temporary. Initialized them to avoid null excetions. // this.amHours = { "lower": 8, "upper": 8 }; // this.pmHours = { "lower": 18, "upper": 18 }; } ngOnInit() { // console.log('Mvp0UserSettingsComponent: ::: ngOnInit()'); if(this.dataArea) { this.dataArea.value = ''; } } ngOnDestroy() { // console.log('Mvp0UserSettingsComponent: ::: ngOnDestroy()'); } // temporary // amHours = { "lower": 3, "upper": 4 }; // pmHours = { "lower": 10, "upper": 12 }; // tbd refreshData(_userProfile: (UserProfile | null), _userPreference: (UserPreference | null)): void { console.log('Mvp0UserSettingsComponent: >>> refreshData() _userProfile = ' + _userProfile + '; _userPreference = ' + _userPreference); if (_userProfile) { this.userProfile = _userProfile.clone(); // ?? this._originalUserProfile = this.userProfile.clone(); console.log('Mvp0UserSettingsComponent: >>> refreshData() this.userProfile = ' + this.userProfile); } else { // Ingore // this.userProfile = new UserProfile(); } if (_userPreference) { this.userPreference = _userPreference.clone(); // ?? this._originalUserPreference = this.userPreference.clone(); console.log('Mvp0UserSettingsComponent: >>> refreshData() this.userPreference = ' + this.userPreference); } else { // Ignore // this.userPreference = new UserPreference(); } // // Refresh UI vars. // this.amHours = this.userPreference.amHours; // this.pmHours = this.userPreference.pmHours; } get isDirty(): boolean { return ((this.userProfile && this.userProfile.isDirty) || (this.userPreference && this.userPreference.isDirty)); } cancelChange() { // console.log('Mvp0UserSettingsComponent: cancelChange()'); if(this._originalUserProfile) { this.userProfile = this._originalUserProfile.clone(); } // else ? if(this._originalUserPreference) { this.userPreference = this._originalUserPreference.clone(); } // else ? } saveChange() { // console.log('Mvp0UserSettingsComponent: saveChange()'); // tbd: // save the data first... this.events.publish(CommonEvents.MVP0_DATA_USERSETTINGS_UPDATE, this.userProfile, this.userPreference); // then // --> This should be done by the parent Page by calling refreshData() // ... // this._originalUserProfile = this.userProfile.clone(); // this._originalUserPreference = this.userPreference.clone(); } onUserProfileDataChange(event) { console.log('Mvp0UserSettingsComponent: onUserProfileDataChange(): event = ' + event); if (this.userProfile) { this.userProfile.isDirty = true; } } onUserPreferenceDataChange(event) { console.log('Mvp0UserSettingsComponent: onUserPreferenceDataChange(): event = ' + event); if (this.userPreference) { this.userPreference.isDirty = true; console.log("this.userPreference = " + this.userPreference); } } handleExportData(event) { // console.log("handleExportData() event = " + event); this.events.publish(CommonEvents.MVP0_DB_EXPORT_DATA); } handlePurgeData(event) { // console.log("handlePurgeData() event = " + event); this.events.publish(CommonEvents.MVP0_DB_PURGE_DATA); } setExportedDataContent(exportedData: string) { if(this.dataArea && this.dataArea.nativeElement) { this.dataArea.nativeElement.value = exportedData; } } resetExportedDataContent() { this.setExportedDataContent(''); } // Testing updatePrimaryPhoto() { this.events.publish(CommonEvents.MVP0_UI_USERPROFILE_PRIMARY_PHOTO_UPDATE); // Arg? } }