Skip to content

Day 8 #1356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Day 8 #1356

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .postcssrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"plugins": {
"@tailwindcss/postcss": {}
}
}
1 change: 1 addition & 0 deletions apps/angular/1-projection/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { TeacherCardComponent } from './component/teacher-card/teacher-card.comp

@Component({
selector: 'app-root',
standalone: true,
template: `
<div class="grid grid-cols-3 gap-3">
<app-teacher-card />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,38 @@
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Component, OnInit } from '@angular/core';
import { CityStore } from '../../data-access/city.store';
import { FakeHttpService } from '../../data-access/fake-http.service';
import { CardType } from '../../model/card.model';
import { CardComponent } from '../../ui/card/card.component';

@Component({
selector: 'app-city-card',
template: 'TODO City',
imports: [],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<app-card [list]="cities()" [type]="cardType" [template]="images">
<ng-template #images>
<img ngSrc="assets/img/student.webp" width="200" height="200" />
</ng-template>
</app-card>
`,
standalone: true,
imports: [CommonModule, CardComponent],
// changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CityCardComponent {}
export class CityCardComponent implements OnInit {
// private http = inject(FakeHttpService);
// private store = inject(CityStore);

constructor(
private http: FakeHttpService,
private store: CityStore,
) {}

cities = this.store.cities;
cardType = CardType.CITY;

ngOnInit() {
this.http.fetchCities$.subscribe((c) => {
this.store.addAll(c);
});
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CommonModule } from '@angular/common';
import {
ChangeDetectionStrategy,
Component,
Expand All @@ -15,7 +16,12 @@ import { CardComponent } from '../../ui/card/card.component';
<app-card
[list]="students()"
[type]="cardType"
[template]="images"
customClass="bg-light-green" />

<ng-template #images>
<img ngSrc="assets/img/student.webp" width="200" height="200" />
</ng-template>
`,
styles: [
`
Expand All @@ -24,7 +30,8 @@ import { CardComponent } from '../../ui/card/card.component';
}
`,
],
imports: [CardComponent],
imports: [CardComponent, CommonModule],
standalone: true,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class StudentCardComponent implements OnInit {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { CommonModule } from '@angular/common';
import { Component, inject, OnInit } from '@angular/core';
import { FakeHttpService } from '../../data-access/fake-http.service';
import { TeacherStore } from '../../data-access/teacher.store';
import { CardType } from '../../model/card.model';
import { CardComponent } from '../../ui/card/card.component';

@Component({
standalone: true,
selector: 'app-teacher-card',
template: `
<app-card
[list]="teachers()"
[type]="cardType"
customClass="bg-light-red"></app-card>
[template]="images"
customClass="bg-light-red">
<ng-template #images>
<img ngSrc="assets/img/student.webp" width="200" height="200" />
</ng-template>
</app-card>
`,
styles: [
`
Expand All @@ -19,7 +26,7 @@ import { CardComponent } from '../../ui/card/card.component';
}
`,
],
imports: [CardComponent],
imports: [CommonModule, CardComponent],
})
export class TeacherCardComponent implements OnInit {
private http = inject(FakeHttpService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { City } from '../model/city.model';
providedIn: 'root',
})
export class CityStore {
private cities = signal<City[]>([]);
public cities = signal<City[]>([]);

addAll(cities: City[]) {
this.cities.set(cities);
Expand Down
29 changes: 18 additions & 11 deletions apps/angular/1-projection/src/app/ui/card/card.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { NgOptimizedImage } from '@angular/common';
import { Component, inject, input } from '@angular/core';
import { randStudent, randTeacher } from '../../data-access/fake-http.service';
import { CommonModule } from '@angular/common';
import { Component, inject, input, Input, TemplateRef } from '@angular/core';
import { CityStore } from '../../data-access/city.store';
import {
randomCity,
randStudent,
randTeacher,
} from '../../data-access/fake-http.service';
import { StudentStore } from '../../data-access/student.store';
import { TeacherStore } from '../../data-access/teacher.store';
import { CardType } from '../../model/card.model';
Expand All @@ -12,12 +17,8 @@ import { ListItemComponent } from '../list-item/list-item.component';
<div
class="flex w-fit flex-col gap-3 rounded-md border-2 border-black p-4"
[class]="customClass()">
@if (type() === CardType.TEACHER) {
<img ngSrc="assets/img/teacher.png" width="200" height="200" />
}
@if (type() === CardType.STUDENT) {
<img ngSrc="assets/img/student.webp" width="200" height="200" />
}
<
<ng-container [ngTemplateOutlet]="template"></ng-container>

<section>
@for (item of list(); track item) {
Expand All @@ -27,19 +28,23 @@ import { ListItemComponent } from '../list-item/list-item.component';
[type]="type()"></app-list-item>
}
</section>

<img ngSrc="assets/img/teacher.png" width="200" height="200" />
<button
class="rounded-sm border border-blue-500 bg-blue-300 p-2"
(click)="addNewItem()">
Add
</button>
</div>
`,
imports: [ListItemComponent, NgOptimizedImage],
imports: [ListItemComponent, CommonModule],
standalone: true,
})
export class CardComponent {
@Input() template!: TemplateRef<any>;
private teacherStore = inject(TeacherStore);
private studentStore = inject(StudentStore);
private cityStore = inject(CityStore);
store = input<any | null>(null);

readonly list = input<any[] | null>(null);
readonly type = input.required<CardType>();
Expand All @@ -53,6 +58,8 @@ export class CardComponent {
this.teacherStore.addOne(randTeacher());
} else if (type === CardType.STUDENT) {
this.studentStore.addOne(randStudent());
} else if (type === CardType.CITY) {
this.cityStore.addOne(randomCity());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import {
ChangeDetectionStrategy,
Component,
inject,
input,
} from '@angular/core';
import { StudentStore } from '../../data-access/student.store';
import { TeacherStore } from '../../data-access/teacher.store';
import { ChangeDetectionStrategy, Component, input } from '@angular/core';
import { CardType } from '../../model/card.model';
import { CityStore } from './../../data-access/city.store';
import { StudentStore } from './../../data-access/student.store';
import { TeacherStore } from './../../data-access/teacher.store';

@Component({
selector: 'app-list-item',
standalone: true,
template: `
<div class="border-grey-300 flex justify-between border px-2 py-1">
{{ name() }}
Expand All @@ -21,8 +18,15 @@ import { CardType } from '../../model/card.model';
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ListItemComponent {
private teacherStore = inject(TeacherStore);
private studentStore = inject(StudentStore);
// private teacherStore = inject(TeacherStore);
// private studentStore = inject(StudentStore);
// private CityStore= inject(CityStore)

constructor(
private teacherStore: TeacherStore,
private studentStore: StudentStore,
private cityStore: CityStore,
) {}

readonly id = input.required<number>();
readonly name = input.required<string>();
Expand All @@ -32,6 +36,8 @@ export class ListItemComponent {
const type = this.type();
if (type === CardType.TEACHER) {
this.teacherStore.deleteOne(id);
} else if (type === CardType.CITY) {
this.cityStore.deleteOne(id);
} else if (type === CardType.STUDENT) {
this.studentStore.deleteOne(id);
}
Expand Down
62 changes: 31 additions & 31 deletions apps/angular/5-crud-application/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
import { CommonModule } from '@angular/common';
import { HttpClient } from '@angular/common/http';
import { Component, inject, OnInit } from '@angular/core';
import { randText } from '@ngneat/falso';
import { Component, OnInit } from '@angular/core';
import { randText, Todo } from '@ngneat/falso';
import { AppService } from '../services/app.service';

@Component({
imports: [CommonModule],
selector: 'app-root',
template: `
@for (todo of todos; track todo.id) {
{{ todo.title }}
<button (click)="update(todo)">Update</button>
}
<div style="display: flex; flex-direction: column; padding: 12px; gap:8px">
@for (todo of todos; track todo.id) {
<div>
{{ todo.title }}
<button (click)="update(todo)">Update</button>
<button (click)="delete(todo)">Delete</button>
</div>
}
</div>
`,
styles: [],
})
export class AppComponent implements OnInit {
private http = inject(HttpClient);
constructor(
private appService: AppService,
private http: HttpClient,
) {}

todos!: any[];

ngOnInit(): void {
this.http
.get<any[]>('https://jsonplaceholder.typicode.com/todos')
.subscribe((todos) => {
this.todos = todos;
});
this.appService.getTodos();
this.appService.todosBehavior.subscribe((todos) => {
this.todos = todos;
});
}

update(todo: any) {
this.http
.put<any>(
`https://jsonplaceholder.typicode.com/todos/${todo.id}`,
JSON.stringify({
todo: todo.id,
title: randText(),
body: todo.body,
userId: todo.userId,
}),
{
headers: {
'Content-type': 'application/json; charset=UTF-8',
},
},
)
.subscribe((todoUpdated: any) => {
this.todos[todoUpdated.id - 1] = todoUpdated;
});
update(todo: Todo) {
const updatedTodo: Todo = {
id: todo.id,
title: randText(),
completed: false,
};

this.appService.updateTodo(todo.id, updatedTodo);
}
delete(todo: Todo) {
this.appService.deleteTodo(todo.id);
}
}
7 changes: 6 additions & 1 deletion apps/angular/5-crud-application/src/app/app.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { provideHttpClient } from '@angular/common/http';
import { ApplicationConfig } from '@angular/core';

import {
provideTanStackQuery,
QueryClient,
} from '@tanstack/angular-query-experimental';

export const appConfig: ApplicationConfig = {
providers: [provideHttpClient()],
providers: [provideHttpClient(), provideTanStackQuery(new QueryClient())],
};
Loading
Loading