parent
34c236cad8
commit
37d30df53d
@ -0,0 +1 @@
|
||||
<p>user-detail works!</p>
|
@ -0,0 +1,23 @@
|
||||
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
|
||||
import { UserDetailComponent } from './user-detail.component';
|
||||
|
||||
describe('UserDetailComponent', () => {
|
||||
let component: UserDetailComponent;
|
||||
let fixture: ComponentFixture<UserDetailComponent>;
|
||||
|
||||
beforeEach(async () => {
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [UserDetailComponent]
|
||||
})
|
||||
.compileComponents();
|
||||
|
||||
fixture = TestBed.createComponent(UserDetailComponent);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
it('should create', () => {
|
||||
expect(component).toBeTruthy();
|
||||
});
|
||||
});
|
@ -0,0 +1,23 @@
|
||||
import { Component } from '@angular/core';
|
||||
|
||||
import { ActivatedRoute } from '@angular/router';
|
||||
import { UserService } from '../../services/user-service';
|
||||
import { User } from '../../models/user.model';
|
||||
|
||||
@Component({
|
||||
selector: 'app-user-detail',
|
||||
standalone: true,
|
||||
imports: [],
|
||||
templateUrl: './user-detail.component.html',
|
||||
styleUrl: './user-detail.component.css'
|
||||
})
|
||||
export class UserDetailComponent {
|
||||
selectedUser!: User | undefined;
|
||||
|
||||
constructor(protected userService: UserService, private activatedRoute: ActivatedRoute) {}
|
||||
|
||||
ngOnInit() {
|
||||
const id = this.activatedRoute.snapshot.params["id"];
|
||||
this.selectedUser = this.userService.getUserById(id);
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
<h2>User list</h2>
|
||||
|
||||
<ul>
|
||||
<li *ngFor="let user of users">
|
||||
<a routerLink="/user/{{ user.id }}">{{ user.login }}</a>
|
||||
</li>
|
||||
</ul>
|
@ -0,0 +1,24 @@
|
||||
import { Component } from '@angular/core';
|
||||
import { RouterModule } from '@angular/router';
|
||||
import { NgFor } from '@angular/common';
|
||||
|
||||
import { User } from '../../models/user.model';
|
||||
import { UserService } from '../../services/user-service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-user-list',
|
||||
standalone: true,
|
||||
imports: [RouterModule,NgFor,],
|
||||
templateUrl: './user-list.component.html',
|
||||
styleUrl: './user-list.component.css'
|
||||
})
|
||||
export class UserListComponent {
|
||||
users: User[] = [];
|
||||
|
||||
constructor(protected userService: UserService) {
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
this.users = this.userService.getAll();
|
||||
}
|
||||
}
|
@ -1,4 +1,8 @@
|
||||
export interface User {
|
||||
id: number
|
||||
login: string,
|
||||
password: string,
|
||||
streak: number | null,
|
||||
points: number | null,
|
||||
streaks: number[],
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
|
||||
import { Injectable } from "@angular/core";
|
||||
import { User } from "../models/user.model";
|
||||
|
||||
@Injectable()
|
||||
export class UserService {
|
||||
private users: User[];
|
||||
private readonly userApiUrl = 'https://664ba07f35bbda10987d9f99.mockapi.io/api/users';
|
||||
|
||||
public constructor(private http: HttpClient){
|
||||
this.users = [];
|
||||
|
||||
this.http.get<User[]>(this.userApiUrl).subscribe(users => {
|
||||
users.forEach(user => {
|
||||
this.addUserToLocal(user);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public getAll(): User[]{
|
||||
return this.users;
|
||||
}
|
||||
|
||||
public getUserById(id: number): User | undefined{
|
||||
return this.users.find(user => user.id === id);
|
||||
let user: User;
|
||||
this.http.get<User>(this.userApiUrl).subscribe(u => {
|
||||
user = u;
|
||||
});
|
||||
}
|
||||
|
||||
private addUserToLocal(user: User): void{
|
||||
console.log('addBookToLocal', user);
|
||||
this.users.push(user);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue