mirror of
https://github.com/Raghu-Ch/angular-challenges.git
synced 2026-02-12 22:03:03 -05:00
refactor: move libs
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
import { NgIf } from '@angular/common';
|
||||
import { Component, signal } from '@angular/core';
|
||||
import { FormsModule } from '@angular/forms';
|
||||
import { MatFormFieldModule } from '@angular/material/form-field';
|
||||
import { MatInputModule } from '@angular/material/input';
|
||||
import { generateList } from './generateList';
|
||||
import { PersonService } from './list.service';
|
||||
import { PersonListComponent } from './person-list.component';
|
||||
|
||||
@Component({
|
||||
standalone: true,
|
||||
imports: [
|
||||
NgIf,
|
||||
PersonListComponent,
|
||||
FormsModule,
|
||||
MatFormFieldModule,
|
||||
MatInputModule,
|
||||
],
|
||||
providers: [PersonService],
|
||||
selector: 'app-root',
|
||||
template: `
|
||||
<button
|
||||
(click)="loadList.set(true)"
|
||||
class="rounded-md border border-black p-2">
|
||||
Load List
|
||||
</button>
|
||||
|
||||
<app-person-list
|
||||
*ngIf="loadList()"
|
||||
class="w-3/4 max-w-2xl"
|
||||
[persons]="persons()" />
|
||||
`,
|
||||
host: {
|
||||
class: 'flex items-center flex-col gap-5',
|
||||
},
|
||||
})
|
||||
export class AppComponent {
|
||||
readonly persons = signal(generateList());
|
||||
readonly loadList = signal(false);
|
||||
|
||||
label = '';
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import { ApplicationConfig } from '@angular/core';
|
||||
import { provideAnimations } from '@angular/platform-browser/animations';
|
||||
|
||||
export const appConfig: ApplicationConfig = {
|
||||
providers: [provideAnimations()],
|
||||
};
|
||||
@@ -0,0 +1,15 @@
|
||||
import { randEmail, randFirstName } from '@ngneat/falso';
|
||||
import { Person } from './person.model';
|
||||
|
||||
export function generateList() {
|
||||
const arr: Person[] = [];
|
||||
|
||||
for (let i = 0; i < 100000; i++) {
|
||||
arr.push({
|
||||
email: randEmail(),
|
||||
name: randFirstName(),
|
||||
});
|
||||
}
|
||||
|
||||
return arr;
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import { Injectable, inject, signal } from '@angular/core';
|
||||
import { randEmail, randFirstName } from '@ngneat/falso';
|
||||
import { generateList } from './generateList';
|
||||
import { Person } from './person.model';
|
||||
|
||||
@Injectable()
|
||||
export class PersonService {
|
||||
private readonly fakeBackend = inject(FakeBackendService);
|
||||
readonly persons = signal<Person[]>([]);
|
||||
|
||||
loadPersons() {
|
||||
this.persons.set(generateList());
|
||||
}
|
||||
|
||||
deletePerson(email: string) {
|
||||
this.persons.set(
|
||||
this.fakeBackend
|
||||
.returnNewList(this.persons())
|
||||
.filter((p) => p.email !== email),
|
||||
);
|
||||
}
|
||||
|
||||
updatePerson(email: string) {
|
||||
this.persons.set(
|
||||
this.fakeBackend
|
||||
.returnNewList(this.persons())
|
||||
.map((p) => (p.email === email ? { email, name: randFirstName() } : p)),
|
||||
);
|
||||
}
|
||||
|
||||
addPerson(name: string) {
|
||||
this.persons.set([
|
||||
{ email: randEmail(), name },
|
||||
...this.fakeBackend.returnNewList(this.persons()),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class FakeBackendService {
|
||||
returnNewList = (input: Person[]): Person[] => [
|
||||
...input.map((i) => ({ ...i })),
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import { ChangeDetectionStrategy, Component, Input } from '@angular/core';
|
||||
|
||||
import { NgForTrackByModule } from '@angular-challenges/shared/directives';
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { Person } from './person.model';
|
||||
|
||||
@Component({
|
||||
selector: 'app-person-list',
|
||||
standalone: true,
|
||||
imports: [CommonModule, NgForTrackByModule],
|
||||
template: `
|
||||
<div class="relative h-[300px] overflow-hidden">
|
||||
<div class="absolute inset-0 overflow-scroll">
|
||||
<div
|
||||
*ngFor="let person of persons; trackByProp: 'email'"
|
||||
class="flex h-9 items-center justify-between border-b">
|
||||
<h3>{{ person.name }}</h3>
|
||||
<p>{{ person.email }}</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
`,
|
||||
host: {
|
||||
class: 'w-full flex flex-col',
|
||||
},
|
||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||
})
|
||||
export class PersonListComponent {
|
||||
@Input() persons: Person[] = [];
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
export interface Person {
|
||||
email: string;
|
||||
name: string;
|
||||
}
|
||||
BIN
apps/performance/37-optimize-big-list/src/favicon.ico
Normal file
BIN
apps/performance/37-optimize-big-list/src/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
13
apps/performance/37-optimize-big-list/src/index.html
Normal file
13
apps/performance/37-optimize-big-list/src/index.html
Normal file
@@ -0,0 +1,13 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>performance-ngfor-biglist</title>
|
||||
<base href="/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<link rel="icon" type="image/x-icon" href="favicon.ico" />
|
||||
</head>
|
||||
<body>
|
||||
<app-root></app-root>
|
||||
</body>
|
||||
</html>
|
||||
7
apps/performance/37-optimize-big-list/src/main.ts
Normal file
7
apps/performance/37-optimize-big-list/src/main.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { bootstrapApplication } from '@angular/platform-browser';
|
||||
import { AppComponent } from './app/app.component';
|
||||
import { appConfig } from './app/app.config';
|
||||
|
||||
bootstrapApplication(AppComponent, appConfig).catch((err) =>
|
||||
console.error(err),
|
||||
);
|
||||
5
apps/performance/37-optimize-big-list/src/styles.scss
Normal file
5
apps/performance/37-optimize-big-list/src/styles.scss
Normal file
@@ -0,0 +1,5 @@
|
||||
@tailwind base;
|
||||
@tailwind components;
|
||||
@tailwind utilities;
|
||||
|
||||
/* You can add global styles to this file, and also import other style files */
|
||||
Reference in New Issue
Block a user