Add Frontend
This commit is contained in:
15
frontend/src/app/app.config.ts
Normal file
15
frontend/src/app/app.config.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { ApplicationConfig, provideBrowserGlobalErrorListeners } from '@angular/core';
|
||||
import { provideRouter } from '@angular/router';
|
||||
import { provideAnimationsAsync } from '@angular/platform-browser/animations/async';
|
||||
import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http';
|
||||
|
||||
import { routes } from './app.routes';
|
||||
|
||||
export const appConfig: ApplicationConfig = {
|
||||
providers: [
|
||||
provideBrowserGlobalErrorListeners(),
|
||||
provideRouter(routes),
|
||||
provideAnimationsAsync(),
|
||||
provideHttpClient(withInterceptorsFromDi())
|
||||
]
|
||||
};
|
||||
5
frontend/src/app/app.html
Normal file
5
frontend/src/app/app.html
Normal file
@@ -0,0 +1,5 @@
|
||||
<mat-toolbar color="primary">
|
||||
<span>Dashboard</span>
|
||||
</mat-toolbar>
|
||||
|
||||
<router-outlet></router-outlet>
|
||||
10
frontend/src/app/app.routes.ts
Normal file
10
frontend/src/app/app.routes.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { Routes } from '@angular/router';
|
||||
import { ItemListComponent } from './components/item-list/item-list';
|
||||
import { ItemFormComponent } from './components/item-form/item-form';
|
||||
|
||||
export const routes: Routes = [
|
||||
{ path: '', component: ItemListComponent },
|
||||
{ path: 'create', component: ItemFormComponent },
|
||||
{ path: 'edit/:id', component: ItemFormComponent },
|
||||
{ path: '**', redirectTo: '' }
|
||||
];
|
||||
0
frontend/src/app/app.scss
Normal file
0
frontend/src/app/app.scss
Normal file
23
frontend/src/app/app.spec.ts
Normal file
23
frontend/src/app/app.spec.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { App } from './app';
|
||||
|
||||
describe('App', () => {
|
||||
beforeEach(async () => {
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [App],
|
||||
}).compileComponents();
|
||||
});
|
||||
|
||||
it('should create the app', () => {
|
||||
const fixture = TestBed.createComponent(App);
|
||||
const app = fixture.componentInstance;
|
||||
expect(app).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should render title', async () => {
|
||||
const fixture = TestBed.createComponent(App);
|
||||
await fixture.whenStable();
|
||||
const compiled = fixture.nativeElement as HTMLElement;
|
||||
expect(compiled.querySelector('h1')?.textContent).toContain('Hello, frontend');
|
||||
});
|
||||
});
|
||||
14
frontend/src/app/app.ts
Normal file
14
frontend/src/app/app.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { Component } from '@angular/core';
|
||||
import { RouterOutlet } from '@angular/router';
|
||||
import { MaterialModule } from './material.module';
|
||||
|
||||
@Component({
|
||||
selector: 'app-root',
|
||||
standalone: true,
|
||||
imports: [RouterOutlet, MaterialModule],
|
||||
templateUrl: './app.html',
|
||||
styleUrls: ['./app.scss']
|
||||
})
|
||||
export class AppComponent {
|
||||
title = 'frontend';
|
||||
}
|
||||
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']);
|
||||
}
|
||||
}
|
||||
21
frontend/src/app/material.module.ts
Normal file
21
frontend/src/app/material.module.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { NgModule } from '@angular/core';
|
||||
import { MatButtonModule } from '@angular/material/button';
|
||||
import { MatCardModule } from '@angular/material/card';
|
||||
import { MatFormFieldModule } from '@angular/material/form-field';
|
||||
import { MatInputModule } from '@angular/material/input';
|
||||
import { MatTableModule } from '@angular/material/table';
|
||||
import { MatToolbarModule } from '@angular/material/toolbar';
|
||||
import { MatIconModule } from '@angular/material/icon';
|
||||
|
||||
@NgModule({
|
||||
exports: [
|
||||
MatButtonModule,
|
||||
MatCardModule,
|
||||
MatFormFieldModule,
|
||||
MatInputModule,
|
||||
MatTableModule,
|
||||
MatToolbarModule,
|
||||
MatIconModule,
|
||||
]
|
||||
})
|
||||
export class MaterialModule { }
|
||||
7
frontend/src/app/models/item.ts
Normal file
7
frontend/src/app/models/item.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
export interface Item {
|
||||
id: number;
|
||||
name: string;
|
||||
displayName: string;
|
||||
target: string;
|
||||
iconUrl: string;
|
||||
}
|
||||
16
frontend/src/app/services/api.spec.ts
Normal file
16
frontend/src/app/services/api.spec.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { Api } from './api';
|
||||
|
||||
describe('Api', () => {
|
||||
let service: Api;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
service = TestBed.inject(Api);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
});
|
||||
33
frontend/src/app/services/api.ts
Normal file
33
frontend/src/app/services/api.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { Observable } from 'rxjs';
|
||||
import { Item } from '../models/item';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class ApiService {
|
||||
private apiUrl = 'http://localhost:8080/api/items';
|
||||
|
||||
constructor(private http: HttpClient) { }
|
||||
|
||||
getItems(): Observable<Item[]> {
|
||||
return this.http.get<Item[]>(this.apiUrl);
|
||||
}
|
||||
|
||||
getItem(id: number): Observable<Item> {
|
||||
return this.http.get<Item>(`${this.apiUrl}/${id}`);
|
||||
}
|
||||
|
||||
createItem(item: Item): Observable<Item> {
|
||||
return this.http.post<Item>(this.apiUrl, item);
|
||||
}
|
||||
|
||||
updateItem(id: number, item: Item): Observable<Item> {
|
||||
return this.http.put<Item>(`${this.apiUrl}/${id}`, item);
|
||||
}
|
||||
|
||||
deleteItem(id: number): Observable<any> {
|
||||
return this.http.delete(`${this.apiUrl}/${id}`);
|
||||
}
|
||||
}
|
||||
14
frontend/src/index.html
Normal file
14
frontend/src/index.html
Normal file
@@ -0,0 +1,14 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Frontend</title>
|
||||
<base href="/">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<link rel="icon" type="image/x-icon" href="favicon.ico">
|
||||
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
|
||||
</head>
|
||||
<body>
|
||||
<app-root></app-root>
|
||||
</body>
|
||||
</html>
|
||||
6
frontend/src/main.ts
Normal file
6
frontend/src/main.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { bootstrapApplication } from '@angular/platform-browser';
|
||||
import { appConfig } from './app/app.config';
|
||||
import { AppComponent } from './app/app';
|
||||
|
||||
bootstrapApplication(AppComponent, appConfig)
|
||||
.catch((err) => console.error(err));
|
||||
1
frontend/src/styles.scss
Normal file
1
frontend/src/styles.scss
Normal file
@@ -0,0 +1 @@
|
||||
/* You can add global styles to this file, and also import other style files */
|
||||
Reference in New Issue
Block a user