clean code

This commit is contained in:
Chneemann 2024-03-27 11:40:26 +01:00
parent b54060fe06
commit b05c9ee97a
8 changed files with 140 additions and 46 deletions

View file

@ -0,0 +1,8 @@
<section>
<div class="header">
<div class="title">Contacts</div>
<div class="blue-bar"></div>
<div class="metrics-txt">Better with a team</div>
</div>
<p>{{ userService.displayUserName(paramsId) }}</p>
</section>

View file

@ -0,0 +1,31 @@
section {
position: absolute;
top: -64px;
left: 339px;
width: calc(100vw - 635px);
min-height: calc(100vh - 128px);
padding: 64px 48px 48px 48px;
box-shadow: 2px 0px 2px 0px rgba(0, 0, 0, 0.1);
}
.header {
display: flex;
align-items: center;
position: relative;
height: 73px;
.title {
font-size: 61px;
font-weight: 700;
padding-right: 24px;
}
.metrics-txt {
font-size: 27px;
font-weight: 400;
padding-left: 24px;
}
.blue-bar {
height: 80%;
width: 3px;
background-color: var(--light-blue);
}
}

View file

@ -0,0 +1,23 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ContactDetailComponent } from './contact-detail.component';
describe('ContactDetailComponent', () => {
let component: ContactDetailComponent;
let fixture: ComponentFixture<ContactDetailComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [ContactDetailComponent]
})
.compileComponents();
fixture = TestBed.createComponent(ContactDetailComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

View file

@ -0,0 +1,29 @@
import { Component, Input } from '@angular/core';
import { ContactsComponent } from '../contacts.component';
import { ActivatedRoute } from '@angular/router';
import { UserService } from '../../../services/user.service';
@Component({
selector: 'app-contact-detail',
standalone: true,
imports: [],
templateUrl: './contact-detail.component.html',
styleUrl: './contact-detail.component.scss',
})
export class ContactDetailComponent {
paramsId = '';
constructor(public userService: UserService, private route: ActivatedRoute) {}
ngOnInit(): void {
this.routeUserId();
}
routeUserId() {
if (this.route.params.subscribe()) {
this.route.params.subscribe((params) => {
this.paramsId = params['id'];
});
}
}
}

View file

@ -20,7 +20,7 @@
<div <div
class="circle" class="circle"
[ngStyle]="{ [ngStyle]="{
'background-color': displayUserDetails( 'background-color': userService.displayUserDetails(
usersByFirstLetter[sortLetter][index], usersByFirstLetter[sortLetter][index],
'color' 'color'
) )
@ -28,7 +28,7 @@
> >
<div class="initials"> <div class="initials">
{{ {{
displayUserDetails( userService.displayUserDetails(
usersByFirstLetter[sortLetter][index], usersByFirstLetter[sortLetter][index],
"initials" "initials"
) )
@ -36,17 +36,25 @@
</div> </div>
</div> </div>
<div class="details"> <div class="details">
<p>{{ displayUserName(usersByFirstLetter[sortLetter][index]) }}</p> <p>
<a href="tel:">asdasdasd</a> {{
userService.displayUserName(usersByFirstLetter[sortLetter][index])
}}
</p>
<span>
{{
userService.displayUserDetails(
usersByFirstLetter[sortLetter][index],
"email"
)
}}
</span>
</div> </div>
</div> </div>
} } } }
</div> </div>
</div> </div>
@if (paramsId) { @if (paramsId) {
<div class="right-frame"> <app-contact-detail></app-contact-detail>
<h2>Contacts</h2>
<p>{{ displayUserName(paramsId) }}</p>
</div>
} }
</section> </section>

View file

@ -94,7 +94,7 @@ section {
font-weight: 400; font-weight: 400;
margin-bottom: 6px; margin-bottom: 6px;
} }
a { span {
font-size: 16px; font-size: 16px;
font-weight: 400; font-weight: 400;
text-decoration: none; text-decoration: none;
@ -102,16 +102,6 @@ section {
} }
} }
.right-frame {
position: absolute;
top: -64px;
left: 339px;
width: calc(100vw - 635px);
min-height: calc(100vh - 128px);
padding: 64px 48px 48px 48px;
box-shadow: 2px 0px 2px 0px rgba(0, 0, 0, 0.1);
}
@media screen and (max-width: 910px) { @media screen and (max-width: 910px) {
.left-frame { .left-frame {
position: fixed; position: fixed;

View file

@ -1,13 +1,14 @@
import { Component } from '@angular/core'; import { Component, ViewChild } from '@angular/core';
import { UserService } from '../../services/user.service'; import { UserService } from '../../services/user.service';
import { User } from '../../interfaces/user.interface'; import { User } from '../../interfaces/user.interface';
import { CommonModule } from '@angular/common'; import { CommonModule } from '@angular/common';
import { ActivatedRoute, RouterLink } from '@angular/router'; import { ActivatedRoute, RouterLink } from '@angular/router';
import { ContactDetailComponent } from './contact-detail/contact-detail.component';
@Component({ @Component({
selector: 'app-contacts', selector: 'app-contacts',
standalone: true, standalone: true,
imports: [CommonModule, RouterLink], imports: [CommonModule, RouterLink, ContactDetailComponent],
templateUrl: './contacts.component.html', templateUrl: './contacts.component.html',
styleUrl: './contacts.component.scss', styleUrl: './contacts.component.scss',
}) })
@ -18,10 +19,9 @@ export class ContactsComponent {
userMap: { [key: string]: User } = {}; userMap: { [key: string]: User } = {};
paramsId = ''; paramsId = '';
constructor(private userService: UserService, private route: ActivatedRoute) { constructor(public userService: UserService, private route: ActivatedRoute) {
this.userService.subUserList().subscribe(() => { this.userService.subUserList().subscribe(() => {
this.allUsers = this.loadAllUser(); this.allUsers = this.loadAllUser();
this.organizeUserData();
this.sortAllUsers(); this.sortAllUsers();
this.sortUsersFirstLetter(); this.sortUsersFirstLetter();
this.sortUsersByFirstLetter(); this.sortUsersByFirstLetter();
@ -40,13 +40,6 @@ export class ContactsComponent {
} }
} }
organizeUserData() {
this.userMap = {};
this.allUsers.forEach((user) => {
this.userMap[user.id] = user;
});
}
loadAllUser(): User[] { loadAllUser(): User[] {
// Without Guest // Without Guest
return this.userService.allUsers.filter((user, index) => index !== 0); return this.userService.allUsers.filter((user, index) => index !== 0);
@ -77,19 +70,4 @@ export class ContactsComponent {
this.usersByFirstLetter[firstLetter].push(user.id); this.usersByFirstLetter[firstLetter].push(user.id);
}); });
} }
displayUserName(id: string) {
if (this.userMap[id]) {
return this.userMap[id].firstName + ', ' + this.userMap[id].lastName;
}
return '';
}
displayUserDetails(id: string, query: keyof User) {
if (this.userMap[id]) {
const user = this.userMap[id];
return user[query];
}
return '';
}
} }

View file

@ -10,8 +10,13 @@ export class UserService {
firestore: Firestore = inject(Firestore); firestore: Firestore = inject(Firestore);
allUsers: User[] = []; allUsers: User[] = [];
userMap: { [key: string]: User } = {};
constructor() {} constructor() {
this.subUserList().subscribe(() => {
this.organizeUserData();
});
}
subUserList() { subUserList() {
return new Observable<void>((observer) => { return new Observable<void>((observer) => {
@ -29,4 +34,26 @@ export class UserService {
return () => unsubscribe(); return () => unsubscribe();
}); });
} }
organizeUserData() {
this.userMap = {};
this.allUsers.forEach((user) => {
this.userMap[user.id] = user;
});
}
displayUserName(id: string) {
if (this.userMap[id]) {
return this.userMap[id].firstName + ', ' + this.userMap[id].lastName;
}
return 'sd';
}
displayUserDetails(id: string, query: keyof User) {
if (this.userMap[id]) {
const user = this.userMap[id];
return user[query];
}
return '';
}
} }