deleteCompletedTasks Functionality (#4)

This commit is contained in:
imelilabourne 2020-09-21 15:55:28 +08:00
parent 7206da41eb
commit 95934a733d
4 changed files with 36 additions and 46 deletions

14
db.json
View File

@ -1,16 +1,16 @@
{ {
"tasks": [ "tasks": [
{ {
"title": "coffee", "title": "123",
"completed": false,
"editing": false,
"id": 1
},
{
"title": "2dasd",
"completed": false, "completed": false,
"editing": false, "editing": false,
"id": 2 "id": 2
},
{
"title": "sleep",
"completed": false,
"editing": false,
"id": 3
} }
] ]
} }

View File

@ -43,7 +43,7 @@
<div>{{ remaining()}} uncompleted tasks</div> <div>{{ remaining()}} uncompleted tasks</div>
</div> </div>
<div *ngIf="atleastOneCompleted()"> <div *ngIf="atleastOneCompleted()">
<button class="btn btn-warning btn-block" (click) ="deleteAllTask()">Clear Completed</button> <button class="btn btn-warning btn-block" (click) ="deleteCompletedTask()">Clear Completed</button>
<div class="alert alert-warning">You've selected an item</div> <div class="alert alert-warning">You've selected an item</div>
</div> </div>
</div> </div>

View File

@ -1,5 +1,6 @@
import { animate, style, transition, trigger } from '@angular/animations'; import { animate, style, transition, trigger } from '@angular/animations';
import { Component } from '@angular/core'; import { Component } from '@angular/core';
import { takeLast } from 'rxjs/operators';
import { Tasks } from 'src/app/Todo-List-App/models/todo-interface'; import { Tasks } from 'src/app/Todo-List-App/models/todo-interface';
import { TodoService } from '../../services/todo.service'; import { TodoService } from '../../services/todo.service';
@ -10,14 +11,14 @@ import { TodoService } from '../../services/todo.service';
styleUrls: ['todo-list.component.css'], styleUrls: ['todo-list.component.css'],
animations: [ animations: [
trigger('fade', [ trigger('fade', [
transition(':enter', [ // transition(':enter', [
style({opacity: 0, transform: 'translateX(-40px)'}), // style({opacity: 0, transform: 'translateX(-30px)'}),
animate(500, style({opacity:1, transform: 'translateY(0px)'})) // animate(500, style({opacity:0, transform: 'translateY(0px)'}))
]), // ]),
transition(':leave', [ // transition(':leave', [
style({opacity: 0, transform: 'translateX(0px)'}), // style({opacity: 0, transform: 'translateX(0px)'}),
animate(1000, style({opacity:1, transform: 'translateX(-50px)'})) // animate(500, style({opacity:0, transform: 'translateX(-30px)'}))
]) // ])
]) ])
] ]
}) })
@ -29,7 +30,6 @@ export class TodoList{
tasks:Tasks[] = []; tasks:Tasks[] = [];
beforeEditing: string; beforeEditing: string;
today: number = Date.now(); today: number = Date.now();
completed: Tasks[] = [];
constructor(private todoService: TodoService){ constructor(private todoService: TodoService){
@ -46,9 +46,7 @@ export class TodoList{
} }
addTask(){ addTask(){
if(this.taskTitle.trim().length === 0){ if(this.taskTitle.trim().length === 0)return;
return;
}
this.todoService.addTask({id:this.taskId, this.todoService.addTask({id:this.taskId,
title: this.taskTitle, title: this.taskTitle,
@ -56,9 +54,7 @@ export class TodoList{
editing: false}) editing: false})
.subscribe((data: Tasks) => { .subscribe((data: Tasks) => {
console.log(data); console.log(data);
}) });
//display
this.tasks.push({ this.tasks.push({
id:this.taskId, id:this.taskId,
title: this.taskTitle, title: this.taskTitle,
@ -66,6 +62,7 @@ export class TodoList{
editing: false editing: false
}) })
this.taskTitle =''; this.taskTitle ='';
this.ngOnInit();
} }
toggleEdit(event: Tasks){ toggleEdit(event: Tasks){
@ -77,7 +74,6 @@ export class TodoList{
event.editing = !event.editing; event.editing = !event.editing;
this.todoService.editTodo(event) this.todoService.editTodo(event)
.subscribe(data => this.tasks = this.tasks.map((task: Tasks )=>{ .subscribe(data => this.tasks = this.tasks.map((task: Tasks )=>{
if (task.title === event.title){ if (task.title === event.title){
task = Object.assign({}, task, event); task = Object.assign({}, task, event);
} }
@ -114,15 +110,13 @@ export class TodoList{
.subscribe(data => this.tasks.filter(task => { .subscribe(data => this.tasks.filter(task => {
return task !== data; return task !== data;
})) }))
} }
deleteAllTask(){ deleteCompletedTask(){
const selectedProducts = this.tasks.filter(product => product.completed).map(p => p.id); const selectedItems = this.tasks.filter(item => item.completed).map(i => i.id);
console.log (selectedProducts); console.log (selectedItems);
selectedProducts.forEach(value => { selectedItems.forEach(value => {
this.todoService.deleteTask(value) this.todoService.deleteTask(value)
.subscribe(res => { .subscribe(res => {
this.tasks = this.tasks.filter(task => !task.completed); this.tasks = this.tasks.filter(task => !task.completed);

View File

@ -1,4 +1,4 @@
import { HttpClient, HttpHeaders} from "@angular/common/http"; import { HttpClient, HttpHeaders, HttpResponse} from "@angular/common/http";
import { Injectable } from "@angular/core"; import { Injectable } from "@angular/core";
import { Observable } from "rxjs"; import { Observable } from "rxjs";
import { Tasks } from "../models/todo-interface"; import { Tasks } from "../models/todo-interface";
@ -23,20 +23,16 @@ export class TodoService{
editTodo(task: Tasks):Observable<Tasks[]>{ editTodo(task: Tasks):Observable<Tasks[]>{
return this.http return this.http
.put(URL + '/' + task.id, task) .put(URL + '/' + task.id, task)
.pipe(map((response: any) => response.json())); .pipe(map((response: any) => response));
} }
deleteTask(id:number){ deleteTask(id:number){
return this.http.delete(URL + '/' + id); return this.http.delete(URL + '/' + id);
} }
deleteAllTask(id: number[]){ deleteCompletedTask(id: number[]){
return this.http.delete<Tasks[]>(URL + '/' + id); return this.http.delete<number[]>(URL + '/' + id)
.pipe(map((res:any) => res.json()));
// .pipe(map((res:any) => {
// res.json();
// }))
} }