From 64b6c49850c7e6b495e3bffe843aca3c60d8acae Mon Sep 17 00:00:00 2001 From: Chneemann Date: Sat, 18 May 2024 10:10:45 +0200 Subject: [PATCH] added new user to firebase --- .../contact-edit-new.component.html | 1 + .../contact-edit-new.component.ts | 14 +-- .../contact-form/contact-form.component.html | 3 +- .../contact-form/contact-form.component.ts | 92 +++++++++++++------ src/app/services/firebase.service.ts | 6 ++ 5 files changed, 74 insertions(+), 42 deletions(-) diff --git a/src/app/components/contacts/contact-edit-new/contact-edit-new.component.html b/src/app/components/contacts/contact-edit-new/contact-edit-new.component.html index d3f349a..e5f2011 100644 --- a/src/app/components/contacts/contact-edit-new/contact-edit-new.component.html +++ b/src/app/components/contacts/contact-edit-new/contact-edit-new.component.html @@ -50,6 +50,7 @@
diff --git a/src/app/components/contacts/contact-edit-new/contact-edit-new.component.ts b/src/app/components/contacts/contact-edit-new/contact-edit-new.component.ts index fa16c6a..e0ada6e 100644 --- a/src/app/components/contacts/contact-edit-new/contact-edit-new.component.ts +++ b/src/app/components/contacts/contact-edit-new/contact-edit-new.component.ts @@ -20,22 +20,10 @@ import { User } from '../../../interfaces/user.interface'; styleUrl: './contact-edit-new.component.scss', }) export class ContactEditNewComponent implements OnInit { - @Input() currentUserId: string | undefined; + @Input() currentUserId: string = ''; randomColor: string = ''; userInitials: string = ''; - userData: User = { - uId: '', - firstName: '', - lastName: '', - email: '', - phone: '', - initials: '', - color: this.randomColor, - status: false, - lastLogin: 0, - }; - constructor( public firebaseService: FirebaseService, public sharedService: SharedService diff --git a/src/app/components/contacts/contact-edit-new/contact-form/contact-form.component.html b/src/app/components/contacts/contact-edit-new/contact-form/contact-form.component.html index 730dd15..cc577b4 100644 --- a/src/app/components/contacts/contact-edit-new/contact-form/contact-form.component.html +++ b/src/app/components/contacts/contact-edit-new/contact-form/contact-form.component.html @@ -1,6 +1,7 @@
@if (sharedService.isContactViewMedia) { @@ -42,7 +42,6 @@ }}" [(ngModel)]="contactData.lastName" autocomplete="off" - (keyup)="updateFormData()" required /> diff --git a/src/app/components/contacts/contact-edit-new/contact-form/contact-form.component.ts b/src/app/components/contacts/contact-edit-new/contact-form/contact-form.component.ts index d2b89d4..191c39b 100644 --- a/src/app/components/contacts/contact-edit-new/contact-form/contact-form.component.ts +++ b/src/app/components/contacts/contact-edit-new/contact-form/contact-form.component.ts @@ -3,6 +3,7 @@ import { EventEmitter, Input, OnChanges, + OnInit, Output, SimpleChanges, } from '@angular/core'; @@ -11,6 +12,7 @@ import { TranslateModule } from '@ngx-translate/core'; import { SharedService } from '../../../../services/shared.service'; import { FirebaseService } from '../../../../services/firebase.service'; import { FormBtnComponent } from '../../../../shared/components/buttons/form-btn/form-btn.component'; +import { User } from '../../../../interfaces/user.interface'; @Component({ selector: 'app-contact-form', @@ -19,8 +21,9 @@ import { FormBtnComponent } from '../../../../shared/components/buttons/form-btn templateUrl: './contact-form.component.html', styleUrl: './contact-form.component.scss', }) -export class ContactFormComponent implements OnChanges { - @Input() currentUserId: string | undefined; +export class ContactFormComponent implements OnInit, OnChanges { + @Input() currentUserId: string = ''; + @Input() randomColor: string = ''; @Output() inititalsEmitter = new EventEmitter(); constructor( @@ -37,45 +40,78 @@ export class ContactFormComponent implements OnChanges { phone: '', }; + userData: User = { + uId: '', + firstName: '', + lastName: '', + email: '', + phone: '', + initials: '', + color: '', + status: false, + lastLogin: 0, + }; + + ngOnInit() { + this.userData = { + ...this.userData, + color: this.randomColor, + lastLogin: new Date().getTime(), + }; + } + ngOnChanges(changes: SimpleChanges) { if (changes['currentUserId']) { this.updateContactData(); } } - addInitials() { - const initials = this.contactData - ? this.contactData.firstName.slice(0, 1) + - this.contactData.lastName.slice(0, 1) - : ''; - this.inititalsEmitter.emit(initials); + updateFormData() { + if (!this.currentUserId) { + this.updateInitials(); + this.updateUserData(); + } } - updateFormData() { - console.log(this.contactData); - this.addInitials(); + updateInitials() { const initials = this.contactData ? this.contactData.firstName.slice(0, 1).toUpperCase() + this.contactData.lastName.slice(0, 1).toUpperCase() : ''; - console.log(initials); + this.userData = { + ...this.userData, + initials: initials, + }; + this.inititalsEmitter.emit(initials); + } + + updateUserData() { + this.userData = { + ...this.userData, + firstName: + this.contactData.firstName.charAt(0).toUpperCase() + + this.contactData.firstName.slice(1).toLowerCase(), + lastName: + this.contactData.lastName.charAt(0).toUpperCase() + + this.contactData.lastName.slice(1).toLowerCase(), + email: this.contactData.email, + phone: this.contactData.phone, + }; } private updateContactData() { - if (this.currentUserId) { - this.contactData.firstName = this.firebaseService - .getUserDetails(this.currentUserId, 'firstName') - .join(', '); - this.contactData.lastName = this.firebaseService - .getUserDetails(this.currentUserId, 'lastName') - .join(', '); - this.contactData.email = this.firebaseService - .getUserDetails(this.currentUserId, 'email') - .join(', '); - this.contactData.phone = this.firebaseService - .getUserDetails(this.currentUserId, 'phone') - .join(', '); - } + this.contactData.firstName = this.firebaseService + .getUserDetails(this.currentUserId, 'firstName') + .join(', '); + this.contactData.lastName = this.firebaseService + .getUserDetails(this.currentUserId, 'lastName') + .join(', '); + this.contactData.email = this.firebaseService + .getUserDetails(this.currentUserId, 'email') + .join(', '); + this.contactData.phone = this.firebaseService + .getUserDetails(this.currentUserId, 'phone') + .join(', '); } onSubmit(ngForm: NgForm) { @@ -86,7 +122,9 @@ export class ContactFormComponent implements OnChanges { this.contactData ); } else { - console.log('new contact'); + const { id, ...taskWithoutIds } = this.userData; + this.firebaseService.addNewUser(taskWithoutIds); + console.log('add new contact'); } this.closeEditDialog(); } diff --git a/src/app/services/firebase.service.ts b/src/app/services/firebase.service.ts index 7ed8514..a1cdeb4 100644 --- a/src/app/services/firebase.service.ts +++ b/src/app/services/firebase.service.ts @@ -144,6 +144,12 @@ export class FirebaseService implements OnDestroy { }); } + async addNewUser(userData: User) { + await addDoc(collection(this.firestore, 'users'), userData).catch((err) => { + console.error(err); + }); + } + ngOnDestroy() { this.unsubTask(); this.unsubUser();