Add Frontend
This commit is contained in:
33
frontend/src/app/components/item-form/item-form.html
Normal file
33
frontend/src/app/components/item-form/item-form.html
Normal file
@@ -0,0 +1,33 @@
|
||||
<div class="container">
|
||||
<mat-card>
|
||||
<mat-card-title>{{ isEditMode ? 'Edit Item' : 'Create Item' }}</mat-card-title>
|
||||
<mat-card-content>
|
||||
<form [formGroup]="itemForm" (ngSubmit)="onSubmit()">
|
||||
<mat-form-field appearance="fill">
|
||||
<mat-label>Name</mat-label>
|
||||
<input matInput formControlName="name">
|
||||
</mat-form-field>
|
||||
|
||||
<mat-form-field appearance="fill">
|
||||
<mat-label>Display Name</mat-label>
|
||||
<input matInput formControlName="displayName">
|
||||
</mat-form-field>
|
||||
|
||||
<mat-form-field appearance="fill">
|
||||
<mat-label>Target</mat-label>
|
||||
<input matInput formControlName="target">
|
||||
</mat-form-field>
|
||||
|
||||
<mat-form-field appearance="fill">
|
||||
<mat-label>Icon URL</mat-label>
|
||||
<input matInput formControlName="iconUrl">
|
||||
</mat-form-field>
|
||||
|
||||
<div class="actions">
|
||||
<button type="button" mat-stroked-button (click)="cancel()">Cancel</button>
|
||||
<button type="submit" mat-raised-button color="primary" [disabled]="itemForm.invalid">Save</button>
|
||||
</div>
|
||||
</form>
|
||||
</mat-card-content>
|
||||
</mat-card>
|
||||
</div>
|
||||
21
frontend/src/app/components/item-form/item-form.scss
Normal file
21
frontend/src/app/components/item-form/item-form.scss
Normal file
@@ -0,0 +1,21 @@
|
||||
.container {
|
||||
padding: 20px;
|
||||
max-width: 500px;
|
||||
margin: auto;
|
||||
}
|
||||
|
||||
mat-card {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
mat-form-field {
|
||||
width: 100%;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
.actions {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
gap: 10px;
|
||||
margin-top: 20px;
|
||||
}
|
||||
23
frontend/src/app/components/item-form/item-form.spec.ts
Normal file
23
frontend/src/app/components/item-form/item-form.spec.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
|
||||
import { ItemForm } from './item-form';
|
||||
|
||||
describe('ItemForm', () => {
|
||||
let component: ItemForm;
|
||||
let fixture: ComponentFixture<ItemForm>;
|
||||
|
||||
beforeEach(async () => {
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [ItemForm]
|
||||
})
|
||||
.compileComponents();
|
||||
|
||||
fixture = TestBed.createComponent(ItemForm);
|
||||
component = fixture.componentInstance;
|
||||
await fixture.whenStable();
|
||||
});
|
||||
|
||||
it('should create', () => {
|
||||
expect(component).toBeTruthy();
|
||||
});
|
||||
});
|
||||
64
frontend/src/app/components/item-form/item-form.ts
Normal file
64
frontend/src/app/components/item-form/item-form.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import { Component, OnInit } from '@angular/core';
|
||||
import { ActivatedRoute, Router } from '@angular/router';
|
||||
import { FormBuilder, FormGroup, Validators, ReactiveFormsModule } from '@angular/forms';
|
||||
import { ApiService } from '../../services/api';
|
||||
import { Item } from '../../models/item';
|
||||
import { MaterialModule } from '../../material.module';
|
||||
import { CommonModule } from '@angular/common';
|
||||
|
||||
@Component({
|
||||
selector: 'app-item-form',
|
||||
templateUrl: './item-form.html',
|
||||
styleUrls: ['./item-form.scss'],
|
||||
standalone: true,
|
||||
imports: [MaterialModule, CommonModule, ReactiveFormsModule],
|
||||
})
|
||||
export class ItemFormComponent implements OnInit {
|
||||
itemForm: FormGroup;
|
||||
isEditMode = false;
|
||||
itemId: number | null = null;
|
||||
|
||||
constructor(
|
||||
private fb: FormBuilder,
|
||||
private apiService: ApiService,
|
||||
private router: Router,
|
||||
private route: ActivatedRoute
|
||||
) {
|
||||
this.itemForm = this.fb.group({
|
||||
name: ['', Validators.required],
|
||||
displayName: ['', Validators.required],
|
||||
target: ['', Validators.required],
|
||||
iconUrl: ['']
|
||||
});
|
||||
}
|
||||
|
||||
ngOnInit(): void {
|
||||
const idParam = this.route.snapshot.params['id'];
|
||||
if (idParam) {
|
||||
this.isEditMode = true;
|
||||
this.itemId = +idParam; // Convert string to number
|
||||
this.apiService.getItem(this.itemId).subscribe(item => {
|
||||
this.itemForm.patchValue(item);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
onSubmit(): void {
|
||||
if (this.itemForm.valid) {
|
||||
const itemData: Item = { id: this.itemId, ...this.itemForm.value };
|
||||
if (this.isEditMode) {
|
||||
this.apiService.updateItem(this.itemId!, itemData).subscribe(() => {
|
||||
this.router.navigate(['/']);
|
||||
});
|
||||
} else {
|
||||
this.apiService.createItem(itemData).subscribe(() => {
|
||||
this.router.navigate(['/']);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cancel(): void {
|
||||
this.router.navigate(['/']);
|
||||
}
|
||||
}
|
||||
53
frontend/src/app/components/item-list/item-list.html
Normal file
53
frontend/src/app/components/item-list/item-list.html
Normal file
@@ -0,0 +1,53 @@
|
||||
<div class="container">
|
||||
<button mat-fab color="primary" (click)="createItem()" class="fab-button">
|
||||
<mat-icon>add</mat-icon>
|
||||
</button>
|
||||
|
||||
<mat-card>
|
||||
<mat-card-title>Dashboard Items</mat-card-title>
|
||||
<mat-card-content>
|
||||
<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">
|
||||
|
||||
<!-- ID Column -->
|
||||
<ng-container matColumnDef="id">
|
||||
<th mat-header-cell *matHeaderCellDef> ID </th>
|
||||
<td mat-cell *matCellDef="let element"> {{element.id}} </td>
|
||||
</ng-container>
|
||||
|
||||
<!-- Name Column -->
|
||||
<ng-container matColumnDef="name">
|
||||
<th mat-header-cell *matHeaderCellDef> Name </th>
|
||||
<td mat-cell *matCellDef="let element"> {{element.name}} </td>
|
||||
</ng-container>
|
||||
|
||||
<!-- Display Name Column -->
|
||||
<ng-container matColumnDef="displayName">
|
||||
<th mat-header-cell *matHeaderCellDef> Display Name </th>
|
||||
<td mat-cell *matCellDef="let element"> {{element.displayName}} </td>
|
||||
</ng-container>
|
||||
|
||||
<!-- Target Column -->
|
||||
<ng-container matColumnDef="target">
|
||||
<th mat-header-cell *matHeaderCellDef> Target </th>
|
||||
<td mat-cell *matCellDef="let element"> {{element.target}} </td>
|
||||
</ng-container>
|
||||
|
||||
<!-- Actions Column -->
|
||||
<ng-container matColumnDef="actions">
|
||||
<th mat-header-cell *matHeaderCellDef> Actions </th>
|
||||
<td mat-cell *matCellDef="let element">
|
||||
<button mat-icon-button color="primary" (click)="editItem(element.id)">
|
||||
<mat-icon>edit</mat-icon>
|
||||
</button>
|
||||
<button mat-icon-button color="warn" (click)="deleteItem(element.id)">
|
||||
<mat-icon>delete</mat-icon>
|
||||
</button>
|
||||
</td>
|
||||
</ng-container>
|
||||
|
||||
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
|
||||
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
|
||||
</table>
|
||||
</mat-card-content>
|
||||
</mat-card>
|
||||
</div>
|
||||
14
frontend/src/app/components/item-list/item-list.scss
Normal file
14
frontend/src/app/components/item-list/item-list.scss
Normal file
@@ -0,0 +1,14 @@
|
||||
.container {
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.fab-button {
|
||||
position: fixed;
|
||||
bottom: 20px;
|
||||
right: 20px;
|
||||
z-index: 100;
|
||||
}
|
||||
|
||||
table {
|
||||
width: 100%;
|
||||
}
|
||||
23
frontend/src/app/components/item-list/item-list.spec.ts
Normal file
23
frontend/src/app/components/item-list/item-list.spec.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
|
||||
import { ItemList } from './item-list';
|
||||
|
||||
describe('ItemList', () => {
|
||||
let component: ItemList;
|
||||
let fixture: ComponentFixture<ItemList>;
|
||||
|
||||
beforeEach(async () => {
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [ItemList]
|
||||
})
|
||||
.compileComponents();
|
||||
|
||||
fixture = TestBed.createComponent(ItemList);
|
||||
component = fixture.componentInstance;
|
||||
await fixture.whenStable();
|
||||
});
|
||||
|
||||
it('should create', () => {
|
||||
expect(component).toBeTruthy();
|
||||
});
|
||||
});
|
||||
48
frontend/src/app/components/item-list/item-list.ts
Normal file
48
frontend/src/app/components/item-list/item-list.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { Component, OnInit } from '@angular/core';
|
||||
import { ApiService } from '../../services/api';
|
||||
import { Item } from '../../models/item';
|
||||
import { Router } from '@angular/router';
|
||||
import { MaterialModule } from '../../material.module';
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { MatTableDataSource } from '@angular/material/table';
|
||||
|
||||
@Component({
|
||||
selector: 'app-item-list',
|
||||
templateUrl: './item-list.html',
|
||||
styleUrls: ['./item-list.scss'],
|
||||
standalone: true,
|
||||
imports: [MaterialModule, CommonModule],
|
||||
})
|
||||
export class ItemListComponent implements OnInit {
|
||||
displayedColumns: string[] = ['id', 'name', 'displayName', 'target', 'actions'];
|
||||
dataSource = new MatTableDataSource<Item>();
|
||||
|
||||
constructor(private apiService: ApiService, private router: Router) { }
|
||||
|
||||
ngOnInit(): void {
|
||||
this.loadItems();
|
||||
}
|
||||
|
||||
loadItems(): void {
|
||||
this.apiService.getItems().subscribe(items => {
|
||||
console.log('Items received from API:', items);
|
||||
this.dataSource.data = items;
|
||||
});
|
||||
}
|
||||
|
||||
editItem(id: number): void {
|
||||
this.router.navigate(['/edit', id]);
|
||||
}
|
||||
|
||||
deleteItem(id: number): void {
|
||||
if (confirm('Are you sure you want to delete this item?')) {
|
||||
this.apiService.deleteItem(id).subscribe(() => {
|
||||
this.loadItems();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
createItem(): void {
|
||||
this.router.navigate(['/create']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user