fix: No dual requests
This commit is contained in:
parent
bbe5e30837
commit
9738536cf8
4 changed files with 33 additions and 14 deletions
|
@ -2,13 +2,16 @@
|
||||||
<input class="input" type="text" formControlName="title" />
|
<input class="input" type="text" formControlName="title" />
|
||||||
<input class="input" type="date" formControlName="dueDate" />
|
<input class="input" type="date" formControlName="dueDate" />
|
||||||
|
|
||||||
<button class="btn-primary" type="submit">
|
<button class="btn-primary min-w-[80px]" type="submit">
|
||||||
{{ isEditing() ? "Update" : "Create" }}
|
{{ isEditing() ? "Update" : "Create" }}
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
@if (isEditing()) {
|
<button
|
||||||
<button class="btn-secondary" type="button" (click)="stopEdit()">
|
[class.invisible]="!isEditing()"
|
||||||
|
class="btn-secondary"
|
||||||
|
type="button"
|
||||||
|
(click)="stopEdit()"
|
||||||
|
>
|
||||||
Stop Editing
|
Stop Editing
|
||||||
</button>
|
</button>
|
||||||
}
|
|
||||||
</form>
|
</form>
|
||||||
|
|
|
@ -20,7 +20,7 @@ export class CreateTodo {
|
||||||
private datePipe = new DatePipe('en-US');
|
private datePipe = new DatePipe('en-US');
|
||||||
private taskService: TaskService = inject(TaskService);
|
private taskService: TaskService = inject(TaskService);
|
||||||
|
|
||||||
@Input({ required: true }) tasks!: ResourceRef<any>;
|
@Input({ required: true }) tasks!: ResourceRef<Task[]>;
|
||||||
@Input() editTodo: number = -1;
|
@Input() editTodo: number = -1;
|
||||||
|
|
||||||
ngOnInit() {
|
ngOnInit() {
|
||||||
|
@ -37,13 +37,24 @@ export class CreateTodo {
|
||||||
submit() {
|
submit() {
|
||||||
if (this.form.valid) {
|
if (this.form.valid) {
|
||||||
if (this.form.value.id == -1) {
|
if (this.form.value.id == -1) {
|
||||||
this.taskService
|
this.taskService.createTask(this.form.value).then((task) =>
|
||||||
.createTask(this.form.value)
|
this.tasks.update((tasks: Task[]) => {
|
||||||
.then(() => this.tasks.reload());
|
tasks.push(task);
|
||||||
|
return tasks;
|
||||||
|
}),
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
this.taskService
|
this.taskService.updateTask(this.form.value).then((task: Task) => {
|
||||||
.updateTask(this.form.value)
|
this.tasks.update((tasks: Task[]) => {
|
||||||
.then(() => this.tasks.reload());
|
return tasks.map((existingTask) => {
|
||||||
|
if (existingTask.id == task.id) {
|
||||||
|
return task;
|
||||||
|
} else {
|
||||||
|
return existingTask;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
this.isEditing.set(false);
|
this.isEditing.set(false);
|
||||||
}
|
}
|
||||||
this.form.controls['id'].setValue(-1);
|
this.form.controls['id'].setValue(-1);
|
||||||
|
|
|
@ -71,7 +71,7 @@ export class TaskService {
|
||||||
if (response.status != 200) {
|
if (response.status != 200) {
|
||||||
reject('Request did not fetch tasks succesfully');
|
reject('Request did not fetch tasks succesfully');
|
||||||
} else {
|
} else {
|
||||||
response.json().then((json) => resolve(json));
|
response.json().then((json) => resolve(task));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -20,6 +20,7 @@ export class TodoTable {
|
||||||
const response = await fetch('http://localhost:2000/api/tasks');
|
const response = await fetch('http://localhost:2000/api/tasks');
|
||||||
return (await response.json()) as Task[];
|
return (await response.json()) as Task[];
|
||||||
},
|
},
|
||||||
|
defaultValue: [],
|
||||||
});
|
});
|
||||||
private taskService: TaskService = inject(TaskService);
|
private taskService: TaskService = inject(TaskService);
|
||||||
|
|
||||||
|
@ -30,10 +31,14 @@ export class TodoTable {
|
||||||
}
|
}
|
||||||
|
|
||||||
deleteTask(task: Task) {
|
deleteTask(task: Task) {
|
||||||
this.taskService.deleteTask(task).then(() => this.tasks.reload());
|
this.taskService.deleteTask(task).then(() =>
|
||||||
|
this.tasks.update((tasks: Task[]) => {
|
||||||
|
return tasks.filter((existingTask) => existingTask.id != task.id);
|
||||||
|
}),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
toggleTaskDoneStatus(task: Task) {
|
toggleTaskDoneStatus(task: Task) {
|
||||||
this.taskService.toggleTaskDoneStatus(task).then(() => this.tasks.reload());
|
this.taskService.toggleTaskDoneStatus(task);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue