1
0
Fork 0
forked from lthn/blockchain
blockchain/src/gui/qt-daemon/html_source/src/app/add-contacts/add-contacts.component.ts
zetov 53d5b2362a Lock/unlock transaction + contact (#86)
* test details & close btn

* compiled

* confirm modal

* delete "cancel" btn from aliases

* fix copy btn

* text alias fix

* confirm pop up

* lock & unlock transaction

* confirm pop up comment fix

* compiled

* rebuild html

* contact service

* rebuild html

* fix add contact + rebuild html
2019-08-14 10:16:56 +02:00

189 lines
6 KiB
TypeScript

import { Component, OnInit, NgZone, OnDestroy } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms';
import { BackendService } from '../_helpers/services/backend.service';
import { VariablesService } from '../_helpers/services/variables.service';
import { ModalService } from '../_helpers/services/modal.service';
import { Location } from '@angular/common';
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-add-contacts',
templateUrl: './add-contacts.component.html',
styleUrls: ['./add-contacts.component.scss']
})
export class AddContactsComponent implements OnInit, OnDestroy {
id: number;
queryRouting;
addContactForm = new FormGroup({
address: new FormControl('', [
Validators.required,
(g: FormControl) => {
if (g.value) {
this.backend.validateAddress(g.value, valid_status => {
this.ngZone.run(() => {
if (valid_status === false) {
g.setErrors(
Object.assign({ address_not_valid: true }, g.errors)
);
} else {
if (g.hasError('address_not_valid')) {
delete g.errors['address_not_valid'];
if (Object.keys(g.errors).length === 0) {
g.setErrors(null);
}
}
}
});
});
return g.hasError('address_not_valid')
? { address_not_valid: true }
: null;
}
return null;
},
(g: FormControl) => {
const isDublicated = this.variablesService.contacts.findIndex(
contact => contact.address === g.value
);
if (isDublicated !== -1 && !(this.id === isDublicated)) {
return { dublicated: true };
}
return null;
}
]),
notes: new FormControl('', [
(g: FormControl) => {
if (g.value) {
if (g.value.length > this.variablesService.maxCommentLength) {
return { maxLength: true };
} else {
return null;
}
} else {
return null;
}
}
]),
name: new FormControl('', [
Validators.required,
Validators.pattern(/^[\w\s-_.]{4,25}$/),
(g: FormControl) => {
if (g.value) {
const isDublicated = this.variablesService.contacts.findIndex(
contact => contact.name === g.value.trim()
);
if (isDublicated !== -1 && !(this.id === isDublicated)) {
return { dublicated: true };
}
return null;
}
}
])
});
constructor(
private route: ActivatedRoute,
private backend: BackendService,
public variablesService: VariablesService,
private modalService: ModalService,
private ngZone: NgZone,
private location: Location
) {}
ngOnInit() {
this.queryRouting = this.route.queryParams.subscribe(params => {
if (params.id) {
this.id = parseInt(params.id, 10);
this.addContactForm.reset({
name: this.variablesService.contacts[params.id]['name'],
address: this.variablesService.contacts[params.id]['address'],
notes: this.variablesService.contacts[params.id]['notes']
});
} else {
this.addContactForm.reset({
name: this.variablesService.newContact['name'],
address: this.variablesService.newContact['address'],
notes: this.variablesService.newContact['notes']
});
}
});
}
add() {
if (!this.variablesService.appPass) {
this.modalService.prepareModal(
'error',
'CONTACTS.FORM_ERRORS.SET_MASTER_PASSWORD'
);
} else {
if (this.addContactForm.valid) {
this.backend.validateAddress(
this.addContactForm.get('address').value,
valid_status => {
if (valid_status === false) {
this.ngZone.run(() => {
this.addContactForm
.get('address')
.setErrors({ address_not_valid: true });
});
} else {
if (this.id || this.id === 0) {
this.variablesService.contacts.forEach((contact, index) => {
if (index === this.id) {
contact.name = this.addContactForm.get('name').value.trim();
contact.address = this.addContactForm.get('address').value;
contact.notes =
this.addContactForm.get('notes').value || '';
}
});
this.backend.storeSecureAppData();
this.backend.getContactAlias();
this.modalService.prepareModal(
'success',
'CONTACTS.SUCCESS_SAVE'
);
} else {
this.variablesService.contacts.push({
name: this.addContactForm.get('name').value.trim(),
address: this.addContactForm.get('address').value,
notes: this.addContactForm.get('notes').value || ''
});
this.backend.storeSecureAppData();
this.backend.getContactAlias();
this.modalService.prepareModal(
'success',
'CONTACTS.SUCCESS_SENT'
);
this.variablesService.newContact = {
name: null,
address: null,
notes: null
};
this.addContactForm.reset({
name: null,
address: null,
notes: null
});
}
}
}
);
}
}
}
back() {
this.location.back();
}
ngOnDestroy() {
if (!(this.id || this.id === 0)) {
this.variablesService.newContact = {
name: this.addContactForm.get('name').value,
address: this.addContactForm.get('address').value,
notes: this.addContactForm.get('notes').value
};
}
this.queryRouting.unsubscribe();
}
}