added new user to firebase

This commit is contained in:
Chneemann 2024-05-18 10:10:45 +02:00
parent ec447ee3ad
commit 64b6c49850
5 changed files with 74 additions and 42 deletions

View file

@ -50,6 +50,7 @@
<div class="form"> <div class="form">
<app-contact-form <app-contact-form
(inititalsEmitter)="inititalsEmitter($event)" (inititalsEmitter)="inititalsEmitter($event)"
[randomColor]="randomColor"
></app-contact-form> ></app-contact-form>
</div> </div>
</div> </div>

View file

@ -20,22 +20,10 @@ import { User } from '../../../interfaces/user.interface';
styleUrl: './contact-edit-new.component.scss', styleUrl: './contact-edit-new.component.scss',
}) })
export class ContactEditNewComponent implements OnInit { export class ContactEditNewComponent implements OnInit {
@Input() currentUserId: string | undefined; @Input() currentUserId: string = '';
randomColor: string = ''; randomColor: string = '';
userInitials: string = ''; userInitials: string = '';
userData: User = {
uId: '',
firstName: '',
lastName: '',
email: '',
phone: '',
initials: '',
color: this.randomColor,
status: false,
lastLogin: 0,
};
constructor( constructor(
public firebaseService: FirebaseService, public firebaseService: FirebaseService,
public sharedService: SharedService public sharedService: SharedService

View file

@ -1,6 +1,7 @@
<section> <section>
<form <form
(ngSubmit)="onSubmit(contactForm)" (ngSubmit)="onSubmit(contactForm)"
(keyup)="updateFormData()"
#contactForm="ngForm" #contactForm="ngForm"
onsubmit="return false" onsubmit="return false"
class="contact-formular" class="contact-formular"
@ -19,7 +20,6 @@
}}" }}"
[(ngModel)]="contactData.firstName" [(ngModel)]="contactData.firstName"
autocomplete="off" autocomplete="off"
(keyup)="updateFormData()"
required required
/> />
@if (sharedService.isContactViewMedia) { @if (sharedService.isContactViewMedia) {
@ -42,7 +42,6 @@
}}" }}"
[(ngModel)]="contactData.lastName" [(ngModel)]="contactData.lastName"
autocomplete="off" autocomplete="off"
(keyup)="updateFormData()"
required required
/> />
</div> </div>

View file

@ -3,6 +3,7 @@ import {
EventEmitter, EventEmitter,
Input, Input,
OnChanges, OnChanges,
OnInit,
Output, Output,
SimpleChanges, SimpleChanges,
} from '@angular/core'; } from '@angular/core';
@ -11,6 +12,7 @@ import { TranslateModule } from '@ngx-translate/core';
import { SharedService } from '../../../../services/shared.service'; import { SharedService } from '../../../../services/shared.service';
import { FirebaseService } from '../../../../services/firebase.service'; import { FirebaseService } from '../../../../services/firebase.service';
import { FormBtnComponent } from '../../../../shared/components/buttons/form-btn/form-btn.component'; import { FormBtnComponent } from '../../../../shared/components/buttons/form-btn/form-btn.component';
import { User } from '../../../../interfaces/user.interface';
@Component({ @Component({
selector: 'app-contact-form', selector: 'app-contact-form',
@ -19,8 +21,9 @@ import { FormBtnComponent } from '../../../../shared/components/buttons/form-btn
templateUrl: './contact-form.component.html', templateUrl: './contact-form.component.html',
styleUrl: './contact-form.component.scss', styleUrl: './contact-form.component.scss',
}) })
export class ContactFormComponent implements OnChanges { export class ContactFormComponent implements OnInit, OnChanges {
@Input() currentUserId: string | undefined; @Input() currentUserId: string = '';
@Input() randomColor: string = '';
@Output() inititalsEmitter = new EventEmitter<string>(); @Output() inititalsEmitter = new EventEmitter<string>();
constructor( constructor(
@ -37,45 +40,78 @@ export class ContactFormComponent implements OnChanges {
phone: '', 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) { ngOnChanges(changes: SimpleChanges) {
if (changes['currentUserId']) { if (changes['currentUserId']) {
this.updateContactData(); this.updateContactData();
} }
} }
addInitials() { updateFormData() {
const initials = this.contactData if (!this.currentUserId) {
? this.contactData.firstName.slice(0, 1) + this.updateInitials();
this.contactData.lastName.slice(0, 1) this.updateUserData();
: ''; }
this.inititalsEmitter.emit(initials);
} }
updateFormData() { updateInitials() {
console.log(this.contactData);
this.addInitials();
const initials = this.contactData const initials = this.contactData
? this.contactData.firstName.slice(0, 1).toUpperCase() + ? this.contactData.firstName.slice(0, 1).toUpperCase() +
this.contactData.lastName.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() { private updateContactData() {
if (this.currentUserId) { this.contactData.firstName = this.firebaseService
this.contactData.firstName = this.firebaseService .getUserDetails(this.currentUserId, 'firstName')
.getUserDetails(this.currentUserId, 'firstName') .join(', ');
.join(', '); this.contactData.lastName = this.firebaseService
this.contactData.lastName = this.firebaseService .getUserDetails(this.currentUserId, 'lastName')
.getUserDetails(this.currentUserId, 'lastName') .join(', ');
.join(', '); this.contactData.email = this.firebaseService
this.contactData.email = this.firebaseService .getUserDetails(this.currentUserId, 'email')
.getUserDetails(this.currentUserId, 'email') .join(', ');
.join(', '); this.contactData.phone = this.firebaseService
this.contactData.phone = this.firebaseService .getUserDetails(this.currentUserId, 'phone')
.getUserDetails(this.currentUserId, 'phone') .join(', ');
.join(', ');
}
} }
onSubmit(ngForm: NgForm) { onSubmit(ngForm: NgForm) {
@ -86,7 +122,9 @@ export class ContactFormComponent implements OnChanges {
this.contactData this.contactData
); );
} else { } else {
console.log('new contact'); const { id, ...taskWithoutIds } = this.userData;
this.firebaseService.addNewUser(taskWithoutIds);
console.log('add new contact');
} }
this.closeEditDialog(); this.closeEditDialog();
} }

View file

@ -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() { ngOnDestroy() {
this.unsubTask(); this.unsubTask();
this.unsubUser(); this.unsubUser();