Skip to content

Commit 2efbbb8

Browse files
refactor interface
1 parent fac6b6b commit 2efbbb8

File tree

11 files changed

+62
-68
lines changed

11 files changed

+62
-68
lines changed

src/app/components/common/form/form.component.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ import {
33
EventEmitter,
44
Input,
55
Output, SimpleChange,
6-
WritableSignal
76
} from '@angular/core';
87
import {FormControl, FormGroup, ReactiveFormsModule} from "@angular/forms";
9-
import {ApiShow, ApiUpdate} from "@interface/api";
8+
import {ApiItem} from "@interface/api";
109
import {AsyncPipe, NgIf} from "@angular/common";
1110
import {DeleteComponent} from "@components/common/delete/delete.component";
1211

@@ -23,22 +22,21 @@ import {DeleteComponent} from "@components/common/delete/delete.component";
2322
})
2423
export class FormComponent {
2524
@Input() fields: Array<{ name: string; type: string }> = [];
26-
@Input() itemToUpdate!: ApiUpdate|ApiShow|undefined;
25+
@Input() itemToUpdate!: ApiItem;
2726
@Output() submit = new EventEmitter
2827
@Output() delete = new EventEmitter
2928
public formGroup: FormGroup = new FormGroup<any>({})
3029

3130
ngOnChanges(changes: SimpleChange) {
3231
this.formGroup = this.createFormGroup()
33-
console.log('ngOnChanges', this.itemToUpdate)
3432
}
3533

3634
createFormGroup() {
3735
const group: { [key: string]: FormControl<string | null | undefined> } = {}
38-
this.fields.forEach(field => {
36+
this.fields.forEach(field => {
3937
let value;
4038
if (this.itemToUpdate) {
41-
value = this.itemToUpdate[field?.name as keyof ApiShow]
39+
value = this.itemToUpdate[field?.name as keyof ApiItem]
4240
}
4341
group[field.name] = new FormControl(value)
4442
})

src/app/components/common/table/table.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {FormsModule} from "@angular/forms";
44
import {RouterLink} from "@angular/router";
55
import {ShowSvgComponent} from "@components/svg/show-svg/show-svg.component";
66
import {EditSvgComponent} from "@components/svg/edit-svg/edit-svg.component";
7-
import {Hero} from "@interface/hero.model";
7+
import {Api, ApiItem} from "@interface/api";
88

99
@Component({
1010
selector: 'app-table',
@@ -19,7 +19,7 @@ import {Hero} from "@interface/hero.model";
1919
templateUrl: './table.component.html',
2020
})
2121
export class TableComponent {
22-
@Input() items!: WritableSignal<Hero[]| undefined>;
22+
@Input() items!: WritableSignal<ApiItem[]>;
2323
@Input() bulk!: Array<string>;
2424
@Output() addToBulkList = new EventEmitter<string>()
2525
@Output() selectedAll = new EventEmitter<Function>()

src/app/components/foo/create/create.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export class CreateComponent {
2222
private apiService: ApiService = inject(ApiService)
2323
private location: Location = inject(Location)
2424
public isLoading: WritableSignal<boolean> = signal(false)
25+
2526
public formType: Array<{ name: string; type: string }> = [
2627
{
2728
name: 'name',

src/app/components/foo/edit/edit.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
} @else {
1313

1414

15-
<h1 class="text-3xl my-4">Edit {{ item()?.['name'] }} <span class="text-xl">{{ item()?.["@id"] }} </span></h1>
15+
<h1 class="text-3xl my-4">Edit {{ item()['name'] }} <span class="text-xl">{{ item()["@id"] }} </span></h1>
1616
<app-form [fields]="formType"
1717
[itemToUpdate]="item()"
1818
(submit)="onSubmit($event)"

src/app/components/foo/edit/edit.component.ts

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {FormsModule, ReactiveFormsModule} from "@angular/forms";
44
import {Router, RouterLink} from "@angular/router";
55
import {DeleteComponent} from "@components/common/delete/delete.component";
66
import {FormComponent} from "@components/common/form/form.component";
7-
import {ApiShow, ApiUpdate} from "@interface/api";
7+
import {ApiItem} from "@interface/api";
88
import {ApiService} from "@service/api.service";
99

1010

@@ -22,7 +22,7 @@ import {ApiService} from "@service/api.service";
2222
templateUrl: './edit.component.html',
2323
})
2424
export class EditComponent implements OnInit {
25-
public item: WritableSignal<ApiShow | ApiUpdate | undefined> = signal({} as ApiShow | ApiUpdate | undefined);
25+
public item: WritableSignal<ApiItem> = signal({} as ApiItem);
2626
public isLoading: WritableSignal<Boolean> = signal(false)
2727
public error: WritableSignal<string> = signal('')
2828
public formType: Array<{ name: string; type: string }> = [
@@ -40,35 +40,33 @@ export class EditComponent implements OnInit {
4040
}
4141

4242
ngOnInit() {
43-
const splitUrl = this.router.url.split('/edit')[0]
43+
const uri = this.router.url.split('/edit')[0]
4444
this.isLoading.set(true)
45-
this.apiService.getHero(splitUrl)
45+
this.apiService.fetchData(uri)
4646
.subscribe(value => {
4747
this.item.set(value)
4848
this.isLoading.set(false)
4949
})
5050
}
5151

52-
// Interception of changes
53-
ngOnChanges(changes: SimpleChange) {
54-
}
55-
56-
get itemId() {
57-
return computed(() => this.item()?.["@id"])
58-
}
59-
6052
onSubmit(data: any) {
61-
return this.apiService.putHero(this.itemId(), {
62-
...this.item,
63-
...data
64-
}).subscribe(
65-
() => this.location.back()
66-
)
53+
return this.apiService
54+
.putHero(
55+
this.item()['@id']!,
56+
{
57+
...this.item,
58+
...data
59+
})
60+
.subscribe(
61+
() => this.location.back()
62+
)
6763
}
6864

6965
delete() {
70-
return this.apiService.delete(this.itemId()).subscribe(
71-
() => this.location.back()
72-
)
66+
return this.apiService
67+
.delete(this.item()['@id']!)
68+
.subscribe(
69+
() => this.location.back()
70+
)
7371
}
7472
}

src/app/components/foo/list/list.component.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import {Component, inject, OnInit, signal, WritableSignal} from '@angular/core';
2-
import {RouterLink} from "@angular/router";
31
import {AsyncPipe, Location, NgFor, NgIf} from "@angular/common";
2+
import {Component, DestroyRef, inject, OnInit, signal, WritableSignal} from '@angular/core';
3+
import {RouterLink} from "@angular/router";
4+
import {takeUntilDestroyed} from "@angular/core/rxjs-interop";
45
import {DeleteComponent} from "@components/common/delete/delete.component";
56
import {TableComponent} from "@components/common/table/table.component";
6-
import {Hero} from "@interface/hero.model";
7+
import {ApiItem} from "@interface/api";
78
import {ApiService} from "@service/api.service";
89

910
@Component({
@@ -21,18 +22,19 @@ import {ApiService} from "@service/api.service";
2122
})
2223
export class ListComponent implements OnInit {
2324
public isLoading: WritableSignal<Boolean> = signal(false)
24-
public items: WritableSignal<Hero[]> = signal([])
25+
public items: WritableSignal<ApiItem[]> = signal([])
2526
public error: WritableSignal<String> = signal('')
2627
public bulk: WritableSignal<Array<string>> = signal([])
2728

2829
private apiService: ApiService = inject(ApiService)
2930
private location: Location = inject(Location)
30-
31+
private destroy: DestroyRef = inject(DestroyRef)
3132

3233
ngOnInit() {
3334
this.toggleIsLoading()
3435
this.apiService
35-
.getHeroes('/heroes')
36+
.fetchDataList('/heroes')
37+
.pipe(takeUntilDestroyed(this.destroy))
3638
.subscribe(
3739
(items) => {
3840
this.items.set(items['hydra:member'])
@@ -41,7 +43,7 @@ export class ListComponent implements OnInit {
4143
this.toggleIsLoading()
4244
}
4345

44-
addToBulk(id: string) {
46+
public addToBulk(id: string) {
4547
if (this.isInBulkList(id)) {
4648
const bulkFilter =
4749
this.bulk()
@@ -52,7 +54,7 @@ export class ListComponent implements OnInit {
5254
this.bulk.update(uri => [...uri, id])
5355
}
5456

55-
selectedAll() {
57+
public selectedAll() {
5658
if (!this.bulk().length) {
5759
this.items().forEach(item => {
5860
this.bulk().push(<string>item["@id"])
@@ -62,7 +64,7 @@ export class ListComponent implements OnInit {
6264
}
6365
}
6466

65-
delete() {
67+
public delete() {
6668
Promise.all(this.bulk())
6769
.then(
6870
items =>

src/app/components/foo/show/show.component.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
Back to list
99
</a>
1010
<div class="flex">
11-
<a [routerLink]="[item()?.['@id'], 'edit']"
11+
<a [routerLink]="[item()['@id'], 'edit']"
1212
class="px-6 py-2 mr-2 bg-green-600 text-white text-xs rounded shadow-md hover:bg-green-700">
1313
Edit
1414
</a>
15-
<app-delete (delete)="delete(item()?.['@id'])"></app-delete>
15+
<app-delete (delete)="delete(item()['@id'])"></app-delete>
1616
</div>
1717
</div>
1818

19-
<h1 class="text-3xl my-4">Edit {{ item()?.['name'] }} <span class="text-xl">{{ item()?.["@id"] }} </span></h1>
19+
<h1 class="text-3xl my-4">Edit {{ item()['name'] }} <span class="text-xl">{{ item()["@id"] }} </span></h1>
2020

2121
<div class="bg-white overflow-x-auto">
2222
<table class="min-w-full">
@@ -40,7 +40,7 @@ <h1 class="text-3xl my-4">Edit {{ item()?.['name'] }} <span class="text-xl">{{ i
4040
name
4141
</th>
4242
<td class="px-6 py-4 whitespace-nowrap text-sm">
43-
{{ item()?.name }}
43+
{{ item()['name'] }}
4444
</td>
4545
} @else {
4646
<td>Rien</td>

src/app/components/foo/show/show.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {Component, inject, OnInit, signal, WritableSignal} from '@angular/core';
33
import {Router, RouterLink} from "@angular/router";
44
import {DeleteComponent} from "@components/common/delete/delete.component";
55
import {ApiService} from "@service/api.service";
6-
import {ApiShow, ApiUpdate} from "@interface/api";
6+
import {ApiItem} from "@interface/api";
77

88
@Component({
99
selector: 'app-show',
@@ -20,15 +20,15 @@ export class ShowComponent implements OnInit {
2020
private router: Router = inject(Router)
2121
private location: Location = inject(Location)
2222

23-
public item: WritableSignal<ApiShow| ApiUpdate | undefined> = signal({} as ApiShow )
23+
public item: WritableSignal<ApiItem> = signal({} as ApiItem)
2424
public isLoading: WritableSignal<boolean> = signal(false)
2525
public error: WritableSignal<string> = signal('')
2626

2727
ngOnInit() {
2828
this.toggleIsLoading()
2929
const id = this.router.url
3030
this.apiService
31-
.getHero(id)
31+
.fetchData(id)
3232
.subscribe(item => this.item.set(item))
3333
this.toggleIsLoading()
3434
}

src/app/interface/api.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
import {Hero} from "./hero.model";
21

32
export interface Api {
43
"@context"?: string,
54
"@id"?: string,
65
"@type"?: string,
76
}
87

9-
export interface ApiList extends Api {
8+
export interface ApiList {
109
"hydra:totalItems": Number,
11-
"hydra:member": Hero[]
10+
"hydra:member": [],
11+
"hydra:view": object,
12+
"hydra:search": object
1213
}
1314

1415
export interface ApiShow extends Api {
@@ -21,6 +22,6 @@ export interface ApiUpdate extends ApiShow {
2122
}
2223

2324

24-
export interface ApiCreate {
25+
export interface ApiItem extends Api {
2526
[key: string] : string | null | undefined
2627
}

src/app/interface/hero.model.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/app/service/api.service.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {HttpClient, HttpErrorResponse, HttpHeaders} from "@angular/common/http";
22
import {catchError, Observable, throwError} from "rxjs";
33
import {inject, Injectable, Signal} from "@angular/core";
4-
import {ApiList, ApiShow, ApiUpdate} from "@interface/api";
4+
import {ApiItem, ApiList, ApiShow, ApiUpdate} from "@interface/api";
55

66
@Injectable({providedIn: 'root'})
77
export class ApiService {
@@ -13,9 +13,9 @@ export class ApiService {
1313
}
1414
public http: HttpClient = inject(HttpClient)
1515

16-
add(id: string, data: { name: string | null }) {
16+
public add(id: string, data: ApiItem) {
1717
return this.http
18-
.post<Foo>(
18+
.post<ApiItem>(
1919
this.baseUrl + id,
2020
data,
2121
this.httpOptions
@@ -25,25 +25,25 @@ export class ApiService {
2525
)
2626
}
2727

28-
getHeroes(id: string): Observable<ApiList> {
28+
public fetchDataList(id: string): Observable<ApiList> {
29+
const url = this.baseUrl + id
2930
return this.http
30-
.get<ApiList>(this.baseUrl + id)
31+
.get<ApiList>(url)
3132
.pipe(
32-
catchError(
33-
this.handleError
34-
)
33+
catchError(this.handleError)
3534
)
3635
}
3736

38-
getHero(id: string | Observable<string | undefined>): Observable<ApiShow | ApiUpdate | undefined> {
37+
public fetchData(id: string): Observable<ApiItem> {
38+
const url = this.baseUrl + id
3939
return this.http
40-
.get<ApiShow | ApiUpdate | undefined>(this.baseUrl + id)
40+
.get<ApiItem>(url)
4141
.pipe(
4242
catchError(this.handleError)
4343
)
4444
}
4545

46-
putHero(id: Signal<string | undefined> | string | undefined, data: ApiShow | null) {
46+
public putHero(id: string, data: ApiItem) {
4747
return this.http
4848
.put<ApiShow>(
4949
this.baseUrl + id,
@@ -56,7 +56,7 @@ export class ApiService {
5656
}
5757

5858

59-
delete(id: string | undefined | null) {
59+
public delete(id: string | undefined | null) {
6060
return this.http
6161
.delete(this.baseUrl + id)
6262
.pipe(

0 commit comments

Comments
 (0)