feat: add edit functionality for todo items
This commit is contained in:
@ -1,125 +1,125 @@
|
||||
{
|
||||
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
|
||||
"version": 1,
|
||||
"newProjectRoot": "projects",
|
||||
"projects": {
|
||||
"todo": {
|
||||
"projectType": "application",
|
||||
"schematics": {},
|
||||
"root": "",
|
||||
"sourceRoot": "src",
|
||||
"prefix": "app",
|
||||
"architect": {
|
||||
"build": {
|
||||
"builder": "@angular/build:application",
|
||||
"options": {
|
||||
"outputPath": "dist/todo",
|
||||
"index": "src/index.html",
|
||||
"browser": "src/main.ts",
|
||||
"polyfills": [
|
||||
"zone.js"
|
||||
],
|
||||
"tsConfig": "tsconfig.app.json",
|
||||
"assets": [
|
||||
{
|
||||
"glob": "**/*",
|
||||
"input": "public"
|
||||
}
|
||||
],
|
||||
"styles": [
|
||||
"src/styles.css"
|
||||
],
|
||||
"scripts": []
|
||||
},
|
||||
"configurations": {
|
||||
"production": {
|
||||
"budgets": [
|
||||
{
|
||||
"type": "initial",
|
||||
"maximumWarning": "500kB",
|
||||
"maximumError": "1MB"
|
||||
},
|
||||
{
|
||||
"type": "anyComponentStyle",
|
||||
"maximumWarning": "2kB",
|
||||
"maximumError": "4kB"
|
||||
}
|
||||
],
|
||||
"outputHashing": "all"
|
||||
},
|
||||
"development": {
|
||||
"optimization": false,
|
||||
"extractLicenses": false,
|
||||
"sourceMap": true
|
||||
}
|
||||
},
|
||||
"defaultConfiguration": "production"
|
||||
},
|
||||
"serve": {
|
||||
"builder": "@angular/build:dev-server",
|
||||
"configurations": {
|
||||
"production": {
|
||||
"buildTarget": "todo:build:production"
|
||||
},
|
||||
"development": {
|
||||
"buildTarget": "todo:build:development"
|
||||
}
|
||||
},
|
||||
"defaultConfiguration": "development"
|
||||
},
|
||||
"extract-i18n": {
|
||||
"builder": "@angular/build:extract-i18n"
|
||||
},
|
||||
"test": {
|
||||
"builder": "@angular/build:karma",
|
||||
"options": {
|
||||
"polyfills": [
|
||||
"zone.js",
|
||||
"zone.js/testing"
|
||||
],
|
||||
"tsConfig": "tsconfig.spec.json",
|
||||
"assets": [
|
||||
{
|
||||
"glob": "**/*",
|
||||
"input": "public"
|
||||
}
|
||||
],
|
||||
"styles": [
|
||||
"src/styles.css"
|
||||
],
|
||||
"scripts": []
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"cli": {
|
||||
"analytics": "4b15b216-5855-4376-b6a8-53304c9e623a"
|
||||
},
|
||||
"schematics": {
|
||||
"@schematics/angular:component": {
|
||||
"type": "component"
|
||||
},
|
||||
"@schematics/angular:directive": {
|
||||
"type": "directive"
|
||||
},
|
||||
"@schematics/angular:service": {
|
||||
"type": "service"
|
||||
},
|
||||
"@schematics/angular:guard": {
|
||||
"typeSeparator": "."
|
||||
},
|
||||
"@schematics/angular:interceptor": {
|
||||
"typeSeparator": "."
|
||||
},
|
||||
"@schematics/angular:module": {
|
||||
"typeSeparator": "."
|
||||
},
|
||||
"@schematics/angular:pipe": {
|
||||
"typeSeparator": "."
|
||||
},
|
||||
"@schematics/angular:resolver": {
|
||||
"typeSeparator": "."
|
||||
}
|
||||
}
|
||||
}
|
||||
{
|
||||
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
|
||||
"version": 1,
|
||||
"newProjectRoot": "projects",
|
||||
"projects": {
|
||||
"todo": {
|
||||
"projectType": "application",
|
||||
"schematics": {},
|
||||
"root": "",
|
||||
"sourceRoot": "src",
|
||||
"prefix": "app",
|
||||
"architect": {
|
||||
"build": {
|
||||
"builder": "@angular/build:application",
|
||||
"options": {
|
||||
"outputPath": "dist/todo",
|
||||
"index": "src/index.html",
|
||||
"browser": "src/main.ts",
|
||||
"polyfills": [
|
||||
"zone.js"
|
||||
],
|
||||
"tsConfig": "tsconfig.app.json",
|
||||
"assets": [
|
||||
{
|
||||
"glob": "**/*",
|
||||
"input": "public"
|
||||
}
|
||||
],
|
||||
"styles": [
|
||||
"src/styles.css"
|
||||
],
|
||||
"scripts": []
|
||||
},
|
||||
"configurations": {
|
||||
"production": {
|
||||
"budgets": [
|
||||
{
|
||||
"type": "initial",
|
||||
"maximumWarning": "500kB",
|
||||
"maximumError": "1MB"
|
||||
},
|
||||
{
|
||||
"type": "anyComponentStyle",
|
||||
"maximumWarning": "2kB",
|
||||
"maximumError": "4kB"
|
||||
}
|
||||
],
|
||||
"outputHashing": "all"
|
||||
},
|
||||
"development": {
|
||||
"optimization": false,
|
||||
"extractLicenses": false,
|
||||
"sourceMap": true
|
||||
}
|
||||
},
|
||||
"defaultConfiguration": "production"
|
||||
},
|
||||
"serve": {
|
||||
"builder": "@angular/build:dev-server",
|
||||
"configurations": {
|
||||
"production": {
|
||||
"buildTarget": "todo:build:production"
|
||||
},
|
||||
"development": {
|
||||
"buildTarget": "todo:build:development"
|
||||
}
|
||||
},
|
||||
"defaultConfiguration": "development"
|
||||
},
|
||||
"extract-i18n": {
|
||||
"builder": "@angular/build:extract-i18n"
|
||||
},
|
||||
"test": {
|
||||
"builder": "@angular/build:karma",
|
||||
"options": {
|
||||
"polyfills": [
|
||||
"zone.js",
|
||||
"zone.js/testing"
|
||||
],
|
||||
"tsConfig": "tsconfig.spec.json",
|
||||
"assets": [
|
||||
{
|
||||
"glob": "**/*",
|
||||
"input": "public"
|
||||
}
|
||||
],
|
||||
"styles": [
|
||||
"src/styles.css"
|
||||
],
|
||||
"scripts": []
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"cli": {
|
||||
"analytics": "4b15b216-5855-4376-b6a8-53304c9e623a"
|
||||
},
|
||||
"schematics": {
|
||||
"@schematics/angular:component": {
|
||||
"type": "component"
|
||||
},
|
||||
"@schematics/angular:directive": {
|
||||
"type": "directive"
|
||||
},
|
||||
"@schematics/angular:service": {
|
||||
"type": "service"
|
||||
},
|
||||
"@schematics/angular:guard": {
|
||||
"typeSeparator": "."
|
||||
},
|
||||
"@schematics/angular:interceptor": {
|
||||
"typeSeparator": "."
|
||||
},
|
||||
"@schematics/angular:module": {
|
||||
"typeSeparator": "."
|
||||
},
|
||||
"@schematics/angular:pipe": {
|
||||
"typeSeparator": "."
|
||||
},
|
||||
"@schematics/angular:resolver": {
|
||||
"typeSeparator": "."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
1486
frontend/bun.lock
1486
frontend/bun.lock
File diff suppressed because it is too large
Load Diff
@ -1,15 +1,15 @@
|
||||
import {Component} from '@angular/core';
|
||||
import {RouterOutlet} from '@angular/router';
|
||||
import {TableComponent} from "./table/table.component";
|
||||
import {CreateComponent} from "./create/create.component";
|
||||
|
||||
@Component({
|
||||
selector: 'app-root',
|
||||
imports: [RouterOutlet, TableComponent, CreateComponent],
|
||||
templateUrl: './app.component.html',
|
||||
styleUrl: './app.component.css',
|
||||
standalone: true,
|
||||
})
|
||||
export class AppComponent {
|
||||
title = 'todo';
|
||||
}
|
||||
import {Component} from '@angular/core';
|
||||
import {RouterOutlet} from '@angular/router';
|
||||
import {TableComponent} from "./table/table.component";
|
||||
import {CreateComponent} from "./create/create.component";
|
||||
|
||||
@Component({
|
||||
selector: 'app-root',
|
||||
imports: [RouterOutlet, TableComponent, CreateComponent],
|
||||
templateUrl: './app.component.html',
|
||||
styleUrl: './app.component.css',
|
||||
standalone: true,
|
||||
})
|
||||
export class AppComponent {
|
||||
title = 'todo';
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
import { Routes } from '@angular/router';
|
||||
import {Routes} from '@angular/router';
|
||||
|
||||
export const routes: Routes = [];
|
||||
export const routes: Routes = [
|
||||
];
|
||||
|
@ -1,30 +1,32 @@
|
||||
import {Component, inject, OnInit} from '@angular/core';
|
||||
import {TodoService} from "../service/todo.service";
|
||||
import {FormBuilder, FormGroup, ReactiveFormsModule, Validators} from "@angular/forms";
|
||||
|
||||
@Component({
|
||||
selector: 'app-create',
|
||||
standalone: true,
|
||||
imports: [
|
||||
ReactiveFormsModule
|
||||
],
|
||||
templateUrl: './create.component.html',
|
||||
styleUrl: './create.component.css'
|
||||
})
|
||||
export class CreateComponent implements OnInit {
|
||||
todoService: TodoService = inject(TodoService);
|
||||
formBuilder: FormBuilder = inject(FormBuilder);
|
||||
form!: FormGroup;
|
||||
|
||||
ngOnInit(): void {
|
||||
this.form = this.formBuilder.group({
|
||||
title: ['', Validators.required],
|
||||
dueDate: ['', Validators.required],
|
||||
})
|
||||
}
|
||||
|
||||
public create(): void {
|
||||
console.log(this.form.value);
|
||||
this.todoService.create(this.form.value).subscribe(() => {this.todoService.todos.reload()})
|
||||
}
|
||||
}
|
||||
import {Component, inject, OnInit} from '@angular/core';
|
||||
import {TodoService} from "../service/todo.service";
|
||||
import {FormBuilder, FormGroup, ReactiveFormsModule, Validators} from "@angular/forms";
|
||||
|
||||
@Component({
|
||||
selector: 'app-create',
|
||||
standalone: true,
|
||||
imports: [
|
||||
ReactiveFormsModule
|
||||
],
|
||||
templateUrl: './create.component.html',
|
||||
styleUrl: './create.component.css'
|
||||
})
|
||||
export class CreateComponent implements OnInit {
|
||||
todoService: TodoService = inject(TodoService);
|
||||
formBuilder: FormBuilder = inject(FormBuilder);
|
||||
form!: FormGroup;
|
||||
|
||||
ngOnInit(): void {
|
||||
this.form = this.formBuilder.group({
|
||||
title: ['', Validators.required],
|
||||
dueDate: ['', Validators.required],
|
||||
})
|
||||
}
|
||||
|
||||
public create(): void {
|
||||
this.todoService.create(this.form.value).subscribe(() => {
|
||||
this.todoService.todos.reload();
|
||||
this.form.reset();
|
||||
})
|
||||
}
|
||||
}
|
||||
|
0
frontend/src/app/edit/edit.component.css
Normal file
0
frontend/src/app/edit/edit.component.css
Normal file
6
frontend/src/app/edit/edit.component.html
Normal file
6
frontend/src/app/edit/edit.component.html
Normal file
@ -0,0 +1,6 @@
|
||||
<form [formGroup]="form">
|
||||
<input type="text" formControlName="title">
|
||||
<input type="date" formControlName="dueDate">
|
||||
|
||||
<button type="submit" (click)="edit()">Save</button>
|
||||
</form>
|
44
frontend/src/app/edit/edit.component.ts
Normal file
44
frontend/src/app/edit/edit.component.ts
Normal file
@ -0,0 +1,44 @@
|
||||
import {Component, inject, OnInit} from '@angular/core';
|
||||
import {ActivatedRoute, Router} from "@angular/router";
|
||||
import {TodoService} from "../service/todo.service";
|
||||
import {FormBuilder, FormGroup, ReactiveFormsModule, Validators} from "@angular/forms";
|
||||
|
||||
@Component({
|
||||
selector: 'app-edit',
|
||||
imports: [
|
||||
ReactiveFormsModule
|
||||
],
|
||||
templateUrl: './edit.component.html',
|
||||
styleUrl: './edit.component.css'
|
||||
})
|
||||
export default class EditComponent implements OnInit{
|
||||
private id!: number;
|
||||
private todoService: TodoService = inject(TodoService);
|
||||
private route: ActivatedRoute = inject(ActivatedRoute);
|
||||
private fb: FormBuilder = inject(FormBuilder);
|
||||
public form!: FormGroup;
|
||||
|
||||
|
||||
ngOnInit() {
|
||||
this.id = parseInt(this.route.snapshot.paramMap.get('id') ?? '', 10);
|
||||
|
||||
const old = this.todoService.getOne(this.id);
|
||||
this.form = this.fb.group({
|
||||
title: ['', Validators.required],
|
||||
dueDate: ['', Validators.required],
|
||||
});
|
||||
|
||||
old.subscribe((old => {
|
||||
this.form.patchValue({
|
||||
title: old.title,
|
||||
dueDate: old.dueDate,
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
edit() {
|
||||
const edited = this.form.value as { title: string, dueDate: Date };
|
||||
|
||||
this.todoService.edit(edited, this.id).subscribe(() => this.todoService.todos.reload());
|
||||
}
|
||||
}
|
@ -1,43 +1,51 @@
|
||||
import {inject, Injectable} from "@angular/core";
|
||||
import {HttpClient} from "@angular/common/http";
|
||||
import {Observable} from "rxjs";
|
||||
import {GetTask, PutTask} from "../dto/models";
|
||||
import {rxResource} from "@angular/core/rxjs-interop";
|
||||
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class TodoService {
|
||||
http: HttpClient = inject(HttpClient);
|
||||
|
||||
_todos = rxResource({
|
||||
stream: () => this.getTodos(),
|
||||
})
|
||||
|
||||
public get todos() {
|
||||
return this._todos;
|
||||
}
|
||||
|
||||
public getTodos(): Observable<GetTask[]> {
|
||||
return this.http.get<GetTask[]>('http://localhost:2000/api/tasks');
|
||||
}
|
||||
|
||||
public create(todo: {title: string, dueDate: Date}) {
|
||||
return this.http.post('http://localhost:2000/api/tasks', todo);
|
||||
}
|
||||
|
||||
public markAsDone(todo: GetTask) {
|
||||
const putTask: PutTask = {
|
||||
title: todo.title,
|
||||
dueDate: todo.dueDate,
|
||||
done: true,
|
||||
}
|
||||
|
||||
return this.http.put(`http://localhost:2000/api/tasks/${todo.id}`, putTask);
|
||||
}
|
||||
|
||||
public delete(id: number) {
|
||||
return this.http.delete(`http://localhost:2000/api/tasks/${id}`);
|
||||
}
|
||||
}
|
||||
import {inject, Injectable} from "@angular/core";
|
||||
import {HttpClient} from "@angular/common/http";
|
||||
import {Observable} from "rxjs";
|
||||
import {GetTask, PutTask} from "../dto/models";
|
||||
import {rxResource} from "@angular/core/rxjs-interop";
|
||||
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class TodoService {
|
||||
http: HttpClient = inject(HttpClient);
|
||||
|
||||
_todos = rxResource({
|
||||
stream: () => this.getTodos(),
|
||||
})
|
||||
|
||||
public get todos() {
|
||||
return this._todos;
|
||||
}
|
||||
|
||||
public getTodos(): Observable<GetTask[]> {
|
||||
return this.http.get<GetTask[]>('http://localhost:2000/api/tasks');
|
||||
}
|
||||
|
||||
public create(todo: {title: string, dueDate: Date}) {
|
||||
return this.http.post('http://localhost:2000/api/tasks', todo);
|
||||
}
|
||||
|
||||
public markAsDone(todo: GetTask) {
|
||||
const putTask: PutTask = {
|
||||
title: todo.title,
|
||||
dueDate: todo.dueDate,
|
||||
done: true,
|
||||
}
|
||||
|
||||
return this.http.put(`http://localhost:2000/api/tasks/${todo.id}`, putTask);
|
||||
}
|
||||
|
||||
public delete(id: number) {
|
||||
return this.http.delete(`http://localhost:2000/api/tasks/${id}`);
|
||||
}
|
||||
|
||||
edit(edited: { title: string; dueDate: Date }, id: number) {
|
||||
return this.http.put(`http://localhost:2000/api/tasks/${id}`, edited);
|
||||
}
|
||||
|
||||
getOne(id: number) {
|
||||
return this.http.get<GetTask>(`http://localhost:2000/api/tasks/${id}`);
|
||||
}
|
||||
}
|
||||
|
@ -1,27 +1,27 @@
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Title</th>
|
||||
<th>Due Date</th>
|
||||
<th>Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@for (todo of todos.value(); track todo.id) {
|
||||
<tr>
|
||||
<td>
|
||||
{{todo.title}}
|
||||
@if (todo.done) {
|
||||
✓
|
||||
}
|
||||
</td>
|
||||
<td>{{todo.dueDate | date}}</td>
|
||||
<td>
|
||||
<button (click)="markAsDone(todo)">Done</button>
|
||||
<button>Edit</button>
|
||||
<button (click)="delete(todo.id)">Delete</button>
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Title</th>
|
||||
<th>Due Date</th>
|
||||
<th>Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@for (todo of todos.value(); track todo.id) {
|
||||
<tr>
|
||||
<td>
|
||||
{{todo.title}}
|
||||
@if (todo.done) {
|
||||
✓
|
||||
}
|
||||
</td>
|
||||
<td>{{todo.dueDate | date}}</td>
|
||||
<td>
|
||||
<button (click)="markAsDone(todo)">Done</button>
|
||||
<button (click)="edit(todo)">Edit</button>
|
||||
<button (click)="delete(todo.id)">Delete</button>
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
|
@ -1,27 +1,31 @@
|
||||
import {Component, inject, Signal} from '@angular/core';
|
||||
import {TodoService} from "../service/todo.service";
|
||||
import {DatePipe} from "@angular/common";
|
||||
import {GetTask} from "../dto/models";
|
||||
import {toSignal} from "@angular/core/rxjs-interop";
|
||||
|
||||
@Component({
|
||||
selector: 'app-table',
|
||||
standalone: true,
|
||||
imports: [
|
||||
DatePipe
|
||||
],
|
||||
templateUrl: './table.component.html',
|
||||
styleUrl: './table.component.css'
|
||||
})
|
||||
export class TableComponent {
|
||||
todoService: TodoService = inject(TodoService);
|
||||
todos = this.todoService.todos;
|
||||
|
||||
markAsDone(todo: GetTask) {
|
||||
this.todoService.markAsDone(todo).subscribe(() => {this.todos.reload()});
|
||||
}
|
||||
|
||||
delete(id: number) {
|
||||
this.todoService.delete(id).subscribe(() => {this.todos.reload()});
|
||||
}
|
||||
}
|
||||
import {Component, inject} from '@angular/core';
|
||||
import {TodoService} from "../service/todo.service";
|
||||
import {DatePipe} from "@angular/common";
|
||||
import {GetTask} from "../dto/models";
|
||||
|
||||
@Component({
|
||||
selector: 'app-table',
|
||||
standalone: true,
|
||||
imports: [
|
||||
DatePipe,
|
||||
],
|
||||
templateUrl: './table.component.html',
|
||||
styleUrl: './table.component.css'
|
||||
})
|
||||
export class TableComponent {
|
||||
todoService: TodoService = inject(TodoService);
|
||||
todos = this.todoService.todos;
|
||||
|
||||
|
||||
markAsDone(todo: GetTask) {
|
||||
this.todoService.markAsDone(todo).subscribe(() => this.todos.reload());
|
||||
}
|
||||
|
||||
delete(id: number) {
|
||||
this.todoService.delete(id).subscribe(() => this.todos.reload());
|
||||
}
|
||||
|
||||
edit(todo: GetTask) {
|
||||
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user