zgo/src/app/settings/settings.component.ts

442 lines
15 KiB
TypeScript
Raw Normal View History

2021-10-28 20:34:48 +00:00
import { Inject, Component, OnInit, OnDestroy, ViewEncapsulation } from '@angular/core';
import { MatDialog, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
2022-03-07 19:46:00 +00:00
import { MatSlideToggleChange } from '@angular/material/slide-toggle';
2022-07-13 12:20:47 +00:00
import { UntypedFormBuilder, Validators, UntypedFormGroup, FormControl } from '@angular/forms';
import { Subject, Observable } from 'rxjs';
import { Owner } from '../owner.model';
2022-08-10 19:12:28 +00:00
import { XeroService } from '../xero.service';
2022-12-23 20:40:24 +00:00
import { WoocommerceService } from '../woocommerce.service';
2022-08-09 22:23:58 +00:00
import { NotifierService } from '../notifier.service';
import { faCopy } from '@fortawesome/free-solid-svg-icons';
import { LanguageService } from '../language.service';
import { LanguageData } from '../language.model';
2021-10-28 20:34:48 +00:00
@Component({
selector: 'app-settings',
templateUrl: './settings.component.html',
styleUrls: ['/settings.component.css']
})
export class SettingsComponent implements OnInit {
// ------------------------------------
//
faCopy = faCopy;
// ------------------------------------
2022-07-13 12:20:47 +00:00
settingsForm: UntypedFormGroup;
accCodForm: UntypedFormGroup;
2021-10-28 20:34:48 +00:00
owner: Owner;
2022-03-07 19:46:00 +00:00
useZats: boolean;
proVersion: boolean = false;
useVKey: boolean = false;
linkMsg: string = '';
2022-09-06 20:44:01 +00:00
xeroAccCod: string = '';
saveAccOk: boolean = false;
2023-05-09 16:16:09 +00:00
private viewkey: string = '';
2021-11-22 20:37:45 +00:00
coins = [
{
label: 'US Dollar',
symbol: 'usd'
},{
label: 'Euro',
symbol: 'eur'
},{
label: 'British Pound',
symbol: 'gbp'
},{
label: 'Canadian Dollar',
symbol: 'cad'
},{
label: 'Australian Dollar',
symbol: 'aud'
2022-09-06 15:06:22 +00:00
},{
label: 'New Zealand Dollar',
symbol: 'nzd'
2021-11-22 20:37:45 +00:00
}
];
2022-08-09 22:23:58 +00:00
xeroLink: string = '';
localToken: string = '';
2022-08-10 19:12:28 +00:00
clientId: string = '';
2022-12-23 20:40:24 +00:00
wooOwner: string = '';
wooToken: string = '';
wooUrl: string = '';
wooOwnerUpdate: Observable<string>;
wooTokenUpdate: Observable<string>;
wooUrlUpdate: Observable<string>;
2022-08-10 19:12:28 +00:00
clientIdUpdate: Observable<string>;
2022-09-06 20:44:01 +00:00
accCodeUpdate: Observable<string>;
linked2Xero : boolean = false;
pmtServiceURL : string = '';
// -------------------------------------
//
// Language Support
//
vE = {
settingsAcodeInvalid : '',
settingsAcodeLbl : '',
settingsAcodeNotsaved : '',
settingsAcodeSaved : '',
settingsCloseBtn : '',
settingsConfirmPayments : '',
settingsCopyNotavail : '',
settingsCurrencyLbl : '',
settingsLink2Xero : '',
settingsNameLbl : '',
settingsNamePlaceholder : '',
settingsNotservClose : '',
settingsNotservError : '',
settingsNotservSuccess : '',
settingsNotservWarning : '',
settingsOwneridCopied : '',
settingsOwneridNotcopied : '',
settingsPmtservURL : '',
settingsRelink2Xero : '',
settingsSaveBtn : '',
settingsTabIntegrations : '',
settingsTabMainlbl : '',
settingsURLCopied : '',
settingsUseSatoshi : '',
settingsViewTitle : '',
settingsVkeyLbl : '',
settingsVkeyPlaceholder : '',
settingsWCClosebtn : '',
settingsWCGentoken : '',
settingsWCLbl : '',
settingsWCOwnerlbl : '',
settingsWCTokenCopied : '',
settingsWCTokenGenerated : '',
settingsWCTokenGenfail : '',
settingsWCTokenNotcopied : '',
settingsXeroClosebtn : '',
settingsXeroLbl : '',
settingsXeropmtConfirmdis : '',
settingsXeroSavebtn : ''
};
//
languageRdy = new Subject<boolean>();
2021-10-28 20:34:48 +00:00
// ------------------------------------------------------------
2021-10-28 20:34:48 +00:00
constructor(
private languageService : LanguageService,
private notifierService : NotifierService,
2022-07-13 12:20:47 +00:00
private fb: UntypedFormBuilder,
2022-08-10 19:12:28 +00:00
public xeroService: XeroService,
2022-12-23 20:40:24 +00:00
public wooService: WoocommerceService,
2021-10-28 20:34:48 +00:00
private dialogRef: MatDialogRef<SettingsComponent>,
2023-05-09 16:16:09 +00:00
@Inject(MAT_DIALOG_DATA) public data: {o: Owner, v: string}) {
this.useZats = data.o.zats;
this.useVKey = data.o.payconf;
this.settingsForm = fb.group({
2023-05-09 16:16:09 +00:00
name: [data.o.name, Validators.required],
currency: [data.o.currency, Validators.required],
useZats: [data.o.zats, Validators.required],
useVKey: [data.o.payconf, Validators.required],
// proVersion: [data.invoices, Validators.required],
2023-05-09 16:16:09 +00:00
vKey: [data.v]
});
this.accCodForm = fb.group ({
xAcc: [this.xeroAccCod]
});
2023-05-09 16:16:09 +00:00
if (data.o.payconf) {
this.settingsForm.get('vKey')!.enable();
}
2023-05-09 16:16:09 +00:00
this.owner = data.o;
this.viewkey = data.v;
this.proVersion = this.owner.invoices;
if ( this.owner.crmToken !== '' ) {
this.linked2Xero = true;
}
2022-08-10 19:12:28 +00:00
this.clientIdUpdate = xeroService.clientIdUpdate;
xeroService.getXeroConfig();
this.clientIdUpdate.subscribe(clientId => {
this.clientId = clientId;
2022-09-07 22:00:01 +00:00
this.xeroLink = `https://login.xero.com/identity/connect/authorize?response_type=code&client_id=${this.clientId}&redirect_uri=http%3A%2F%2Flocalhost%3A4200%2Fxeroauth&scope=accounting.transactions offline_access&state=${this.owner.address.substring(0, 6)}`
2022-08-10 19:12:28 +00:00
});
2022-09-06 20:44:01 +00:00
this.accCodeUpdate = xeroService.accCodeUpdate;
xeroService.getXeroAccountCode(this.owner.address);
this.accCodeUpdate.subscribe(accData => {
this.xeroAccCod = accData;
2023-05-10 16:05:15 +00:00
//console.log("xeroAccCod -> [" + this.xeroAccCod + "]");
this.accCodForm.get('xAcc')!.setValue(this.xeroAccCod);
2022-09-06 20:44:01 +00:00
});
2022-12-23 20:40:24 +00:00
this.wooOwnerUpdate = wooService.ownerUpdate;
this.wooTokenUpdate = wooService.tokenUpdate;
this.wooUrlUpdate = wooService.siteurlUpdate;
wooService.getWooToken(this.owner._id!);
this.wooOwnerUpdate.subscribe(owData => {
this.wooOwner = owData;
});
this.wooTokenUpdate.subscribe(tkData => {
this.wooToken = tkData;
});
this.wooUrlUpdate.subscribe(uData => {
this.wooUrl = uData;
});
2021-10-28 20:34:48 +00:00
}
2021-10-28 20:34:48 +00:00
ngOnInit() {
this.settingsForm.get('vKey')!.disable();
//
this.chgUILanguage();
2023-05-10 16:05:15 +00:00
//console.log('SETTINGS: Return from chgUILanguage()');
//
this.languageRdy.subscribe (
data => {
this.linkMsg = this.vE.settingsLink2Xero;
this.pmtServiceURL + '';
if ( this.linked2Xero ) {
this.linkMsg = this.vE.settingsRelink2Xero; //'Relink to Xero';
this.pmtServiceURL =
'https://zgo.cash/pmtservice?owner=' +
this.owner._id +
'&invoiceNo=[INVOICENUMBER]&currency=[CURRENCY]&amount=[AMOUNTDUE]&shortCode=[SHORTCODE]';
}
});
2022-08-09 22:23:58 +00:00
}
safeURL(s: string){
return s.replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_');
2021-10-28 20:34:48 +00:00
}
close() {
this.dialogRef.close();
}
closeIntegration() {
2022-09-08 16:35:55 +00:00
if ( (this.xeroAccCod == '') && (this.linked2Xero) )
this.notifierService
.showNotification(this.vE.settingsXeropmtConfirmdis ,
this.vE.settingsNotservClose,'warning',
this.vE.settingsNotservWarning);
2021-10-28 20:34:48 +00:00
this.dialogRef.close();
}
save() {
this.owner.name = this.settingsForm.value.name;
2021-11-22 20:37:45 +00:00
this.owner.currency = this.settingsForm.value.currency;
this.owner.zats = this.settingsForm.value.useZats;
2022-07-20 16:10:24 +00:00
this.owner.payconf = this.settingsForm.value.useVKey;
2023-05-09 16:16:09 +00:00
this.viewkey = this.settingsForm.value.vKey;
//this.owner.invoices = this.settingsForm.value.proVersion
2021-10-28 20:34:48 +00:00
this.dialogRef.close(this.owner);
}
2022-03-07 19:46:00 +00:00
onChange(ob: MatSlideToggleChange) {
this.useZats = ob.checked;
}
onChangeProVersion(ob: MatSlideToggleChange) {
this.proVersion = ob.checked;
}
onChangeVKeyOn(ob: MatSlideToggleChange) {
// console.log("Viewing key switch is " +
// ( ob.checked ? "[ON]." : "[OFF]." ) );
this.useVKey = ob.checked;
if ( ob.checked )
2022-07-20 15:31:23 +00:00
this.settingsForm.get('vKey')!.enable();
else
2022-07-20 15:31:23 +00:00
this.settingsForm.get('vKey')!.disable();
}
copyUrl() {
// console.log("Inside copyUrl()");
if (navigator.clipboard) {
};
try {
navigator.clipboard.writeText(this.pmtServiceURL);
this.notifierService
.showNotification(this.vE.settingsURLCopied,
this.vE.settingsNotservClose,
'success',
this.vE.settingsNotservSuccess);
} catch (err) {
// console.error("Error", err);
this.notifierService
.showNotification(this.vE.settingsCopyNotavail,
this.vE.settingsNotservClose,
'error',
this.vE.settingsNotservError);
}
}
2022-12-23 20:40:24 +00:00
copyWooOwner(){
try {
navigator.clipboard.writeText(this.wooOwner);
this.notifierService.showNotification(this.vE.settingsOwneridCopied,
this.vE.settingsNotservClose,
"success",
this.vE.settingsNotservSuccess);
2022-12-23 20:40:24 +00:00
} catch (err) {
this.notifierService.showNotification(this.vE.settingsOwneridNotcopied,
this.vE.settingsNotservClose,
"error",
this.vE.settingsNotservError);
2022-12-23 20:40:24 +00:00
}
}
copyWooToken(){
try {
navigator.clipboard.writeText(this.wooToken);
this.notifierService.showNotification(this.vE.settingsWCTokenCopied,
this.vE.settingsNotservClose,
"success",
this.vE.settingsNotservSuccess);
2022-12-23 20:40:24 +00:00
} catch (err) {
this.notifierService.showNotification(this.vE.settingsWCTokenNotcopied,
this.vE.settingsNotservClose,
"error",
this.vE.settingsNotservError);
2022-12-23 20:40:24 +00:00
}
}
generateWooToken(){
2023-01-04 20:50:52 +00:00
this.wooService.createWooToken(this.owner._id!).subscribe(responseData => {
if (responseData.status == 202) {
this.notifierService.showNotification(
this.vE.settingsWCTokenGenerated,
this.vE.settingsNotservClose,
"success",
this.vE.settingsNotservSuccess);
2023-01-04 20:50:52 +00:00
this.wooService.getWooToken(this.owner._id!);
this.wooOwnerUpdate.subscribe(owData => {
this.wooOwner = owData;
});
this.wooTokenUpdate.subscribe(tkData => {
this.wooToken = tkData;
});
this.wooUrlUpdate.subscribe(uData => {
this.wooUrl = uData;
});
close();
} else {
this.notifierService.showNotification(
this.vE.settingsWCTokenGenfail,
this.vE.settingsNotservClose,
"error",
this.vE.settingsNotservError);
2023-01-04 20:50:52 +00:00
}
});
2022-12-23 20:40:24 +00:00
}
saveAccCod() {
2022-09-07 22:04:07 +00:00
this.xeroAccCod = this.accCodForm.value.xAcc;
2023-05-10 16:05:15 +00:00
//console.log(">>> " + this.xeroAccCod);
if ( this.xeroAccCod.length <= 10 ) {
const obs = this.xeroService
.setXeroAccountCode(this.owner.address,
this.xeroAccCod);
2023-05-10 16:05:15 +00:00
obs.subscribe({ next: responseData => {
if (responseData.status == 202) {
//console.log('Account saved');
this.notifierService
.showNotification(this.vE.settingsAcodeSaved,
this.vE.settingsNotservClose,
"success",
this.vE.settingsNotservSuccess);
} else {
//console.log('Account not saved -> status[' + responseData.status + ']');
this.notifierService
.showNotification(
this.vE.settingsAcodeNotsaved,
this.vE.settingsNotservClose,
"error",
this.vE.settingsNotservError);
}
},
error: error => {
//console.log('Error saving Account Code -> ' + error.msg)
}});
} else {
this.notifierService
.showNotification(
this.vE.settingsAcodeInvalid,
this.vE.settingsNotservClose,
"error",
this.vE.settingsNotservError);
2022-09-07 22:04:07 +00:00
};
}
/*
xeroAccCodChanged( arg: any ) {
console.log("Account Code changed: " + arg.target.value);
// console.log(arg);
this.saveAccOk = (arg.target.value != this.xeroAccCod );
}
*/
checkStatus( arg : any ) {
2023-05-10 16:05:15 +00:00
//console.log('onChange - checkStatus');
//console.log(arg.target.value);
this.saveAccOk = (arg.target.value != this.xeroAccCod );
}
chgUILanguage(){
2023-05-10 16:05:15 +00:00
//console.log('SETTINGS.chgUILanguage Called ');
this.languageService.getViewElements('settings').subscribe({
next: response => {
//console.log('Received >> ', response );
//console.log('Language Code : ', response.language);
//console.log('Component Name : ',response.component);
//console.log('Language data : ',response.data);
this.vE.settingsViewTitle = response.data.settings_view_title;
this.vE.settingsTabMainlbl = response.data.settings_tab_mainlbl;
this.vE.settingsNameLbl = response.data.settings_name_lbl;
this.vE.settingsCurrencyLbl = response.data.settings_currency_lbl;
this.vE.settingsNamePlaceholder = response.data.settings_name_placeholder;
this.vE.settingsUseSatoshi = response.data.settings_use_satoshi;
this.vE.settingsVkeyLbl = response.data.settings_vkey_lbl;
this.vE.settingsVkeyPlaceholder = response.data.settings_vkey_placeholder;
this.vE.settingsConfirmPayments = response.data.settings_confirm_payments;
this.vE.settingsCloseBtn = response.data.settings_close_btn;
this.vE.settingsSaveBtn = response.data.settings_save_btn;
this.vE.settingsTabIntegrations = response.data.settings_tab_integrations;
this.vE.settingsXeroLbl = response.data.settings_xero_lbl;
this.vE.settingsLink2Xero = response.data.settings_link_2xero;
this.vE.settingsRelink2Xero = response.data.settings_relink_2xero;
this.vE.settingsXeropmtConfirmdis = response.data.settings_xeropmt_confirmdis;
this.vE.settingsNotservClose = response.data.settings_notserv_close;
this.vE.settingsNotservWarning = response.data.settings_notserv_warning;
this.vE.settingsPmtservURL = response.data.settings_pmtserv_url;
this.vE.settingsAcodeLbl = response.data.settings_acode_lbl;
this.vE.settingsXeroClosebtn = response.data.settings_xero_closebtn;
this.vE.settingsXeroSavebtn = response.data.settings_xero_savebtn;
this.vE.settingsWCLbl = response.data.settings_wc_lbl;
this.vE.settingsWCGentoken = response.data.settings_wc_gentoken;
this.vE.settingsWCOwnerlbl = response.data.settings_wc_ownerlbl;
this.vE.settingsWCClosebtn = response.data.settings_wc_closebtn;
this.vE.settingsURLCopied = response.data.settings_url_copied;
this.vE.settingsNotservSuccess = response.data.settings_notserv_success;
this.vE.settingsNotservError = response.data.settings_notserv_error;
this.vE.settingsCopyNotavail = response.data.settings_copy_notavail;
this.vE.settingsOwneridCopied = response.data.settings_ownerid_copied;
this.vE.settingsOwneridNotcopied = response.data.settings_ownerid_notcopied;
this.vE.settingsWCTokenCopied = response.data.settings_wctoken_copied;
this.vE.settingsWCTokenNotcopied = response.data.settings_wctoken_notcopied;
this.vE.settingsWCTokenGenerated = response.data.settings_wctoken_generated;
this.vE.settingsWCTokenGenfail = response.data.settings_wctoken_genfail;
this.vE.settingsAcodeSaved = response.data.settings_acode_saved;
this.vE.settingsAcodeNotsaved = response.data.settings_acode_notsaved;
this.vE.settingsAcodeInvalid = response.data.settings_acode_invalid;
//
this.languageRdy.next(true);
},
2023-05-10 16:05:15 +00:00
error: error => { console.log('Error >> ',error); }
});
}
2021-10-28 20:34:48 +00:00
}