Fix tasks persistence and display.
This commit is contained in:
@@ -1,3 +1,6 @@
|
|||||||
<div>
|
<div>
|
||||||
<mat-icon>add</mat-icon>
|
<input placeholder="Nom de la tâche"/>
|
||||||
</div>
|
<textarea placeholder="Description">
|
||||||
|
|
||||||
|
</textarea>
|
||||||
|
</div>
|
||||||
|
|||||||
@@ -7,19 +7,23 @@ import { AppComponent } from './app.component';
|
|||||||
import { MainPageComponent } from './main-page/main-page.component';
|
import { MainPageComponent } from './main-page/main-page.component';
|
||||||
import { AddTaskComponent } from './add-task/add-task.component';
|
import { AddTaskComponent } from './add-task/add-task.component';
|
||||||
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
|
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
|
||||||
import {MatIconModule} from '@angular/material/icon';
|
import {MatIconModule} from '@angular/material/icon';
|
||||||
|
import {MatInputModule} from '@angular/material/input';
|
||||||
|
import { DisplayTaskComponent } from './display-task/display-task.component';
|
||||||
|
|
||||||
@NgModule({
|
@NgModule({
|
||||||
declarations: [
|
declarations: [
|
||||||
AppComponent,
|
AppComponent,
|
||||||
MainPageComponent,
|
MainPageComponent,
|
||||||
AddTaskComponent
|
AddTaskComponent,
|
||||||
|
DisplayTaskComponent
|
||||||
],
|
],
|
||||||
imports: [
|
imports: [
|
||||||
BrowserModule,
|
BrowserModule,
|
||||||
AppRoutingModule,
|
AppRoutingModule,
|
||||||
BrowserAnimationsModule,
|
BrowserAnimationsModule,
|
||||||
MatIconModule
|
MatIconModule,
|
||||||
|
MatInputModule
|
||||||
],
|
],
|
||||||
providers: [
|
providers: [
|
||||||
CookieService,
|
CookieService,
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import { TaskPersistenceService } from './task-persistence.service';
|
|||||||
providedIn: 'root'
|
providedIn: 'root'
|
||||||
})
|
})
|
||||||
export class TaskService {
|
export class TaskService {
|
||||||
_tasks: BehaviorSubject<Task[]> = new BehaviorSubject<Task[]>([]);
|
private _tasks: BehaviorSubject<Task[]> = new BehaviorSubject<Task[]>([]);
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private _taskPersistenceService: TaskPersistenceService
|
private _taskPersistenceService: TaskPersistenceService
|
||||||
@@ -24,9 +24,15 @@ export class TaskService {
|
|||||||
const tasks = this._tasks.value;
|
const tasks = this._tasks.value;
|
||||||
tasks.push(task);
|
tasks.push(task);
|
||||||
this._tasks.next(tasks);
|
this._tasks.next(tasks);
|
||||||
|
this._saveTasks();
|
||||||
}
|
}
|
||||||
|
|
||||||
_saveTasks(): void {
|
private _saveTasks(): void {
|
||||||
this._taskPersistenceService.save(this._tasks.value);
|
this._taskPersistenceService.save(this._tasks.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
refresh() {
|
||||||
|
const tasks = this._taskPersistenceService.getAll();
|
||||||
|
this._tasks.next(tasks);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
9
src/app/display-task/display-task.component.html
Normal file
9
src/app/display-task/display-task.component.html
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<div class="component" [title]="getTitle()">
|
||||||
|
<h1 *ngIf="task.title?.length; else titlePlaceholder">{{ task.title }}</h1>
|
||||||
|
<ng-template #titlePlaceholder>
|
||||||
|
<div class="placeholder">
|
||||||
|
Saisissez le titre de la tâche.
|
||||||
|
</div>
|
||||||
|
</ng-template>
|
||||||
|
<p>{{ task.description }}</p>
|
||||||
|
</div>
|
||||||
11
src/app/display-task/display-task.component.scss
Normal file
11
src/app/display-task/display-task.component.scss
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
.component {
|
||||||
|
min-height: 50px;
|
||||||
|
width: 90%;
|
||||||
|
margin: auto;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
.placeholder {
|
||||||
|
font-style: italic;
|
||||||
|
color: #AAAAAA;
|
||||||
|
}
|
||||||
25
src/app/display-task/display-task.component.spec.ts
Normal file
25
src/app/display-task/display-task.component.spec.ts
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||||
|
|
||||||
|
import { DisplayTaskComponent } from './display-task.component';
|
||||||
|
|
||||||
|
describe('DisplayTaskComponent', () => {
|
||||||
|
let component: DisplayTaskComponent;
|
||||||
|
let fixture: ComponentFixture<DisplayTaskComponent>;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
await TestBed.configureTestingModule({
|
||||||
|
declarations: [ DisplayTaskComponent ]
|
||||||
|
})
|
||||||
|
.compileComponents();
|
||||||
|
});
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
fixture = TestBed.createComponent(DisplayTaskComponent);
|
||||||
|
component = fixture.componentInstance;
|
||||||
|
fixture.detectChanges();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should create', () => {
|
||||||
|
expect(component).toBeTruthy();
|
||||||
|
});
|
||||||
|
});
|
||||||
15
src/app/display-task/display-task.component.ts
Normal file
15
src/app/display-task/display-task.component.ts
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import { Component, Input, OnInit } from '@angular/core';
|
||||||
|
import { Task } from '../core/entity/task';
|
||||||
|
|
||||||
|
@Component({
|
||||||
|
selector: 'app-display-task',
|
||||||
|
templateUrl: './display-task.component.html',
|
||||||
|
styleUrls: ['./display-task.component.scss']
|
||||||
|
})
|
||||||
|
export class DisplayTaskComponent {
|
||||||
|
@Input() task: Task = {} as Task;
|
||||||
|
|
||||||
|
getTitle(): string {
|
||||||
|
return `Créé le ${this.task.creationDate?.toLocaleString()}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,12 +1,8 @@
|
|||||||
<div class="component">
|
<div class="component">
|
||||||
<h1>Todo List</h1>
|
<h1>Todo List</h1>
|
||||||
<div *ngFor="let task of tasks" [title]="task.creationDate">
|
<app-display-task *ngFor="let task of tasks" [task]="task"></app-display-task>
|
||||||
<h1>{{ task.title }}</h1>
|
<div id="add-task-btn" *ngIf="!isAddingATask" (click)="createTask()">
|
||||||
<p>{{ task.description }}</p>
|
|
||||||
</div>
|
|
||||||
<div id="add-task-btn" *ngIf="!isAddingATask" (click)="startTaskAddition()">
|
|
||||||
<mat-icon>add</mat-icon>
|
<mat-icon>add</mat-icon>
|
||||||
<span>Ajouter une nouvelle tâche...</span>
|
<span>Ajouter une nouvelle tâche...</span>
|
||||||
</div>
|
</div>
|
||||||
<app-add-task *ngIf="isAddingATask"></app-add-task>
|
</div>
|
||||||
</div>
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { Component, OnInit } from '@angular/core';
|
import { AfterViewInit, Component, OnInit } from '@angular/core';
|
||||||
import { Task } from '../core/entity/task';
|
import { Task } from '../core/entity/task';
|
||||||
import { TaskService } from '../core/service/task.service';
|
import { TaskService } from '../core/service/task.service';
|
||||||
|
|
||||||
@@ -7,7 +7,7 @@ import { TaskService } from '../core/service/task.service';
|
|||||||
templateUrl: './main-page.component.html',
|
templateUrl: './main-page.component.html',
|
||||||
styleUrls: ['./main-page.component.scss']
|
styleUrls: ['./main-page.component.scss']
|
||||||
})
|
})
|
||||||
export class MainPageComponent implements OnInit {
|
export class MainPageComponent implements OnInit, AfterViewInit {
|
||||||
tasks: Task[] = [];
|
tasks: Task[] = [];
|
||||||
isAddingATask = false;
|
isAddingATask = false;
|
||||||
|
|
||||||
@@ -19,7 +19,15 @@ export class MainPageComponent implements OnInit {
|
|||||||
this._taskService.tasks.subscribe(tasks => this.tasks = tasks);
|
this._taskService.tasks.subscribe(tasks => this.tasks = tasks);
|
||||||
}
|
}
|
||||||
|
|
||||||
startTaskAddition(): void {
|
ngAfterViewInit(): void {
|
||||||
this.isAddingATask = true;
|
this._taskService.refresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
createTask(): void {
|
||||||
|
const newTask = {
|
||||||
|
creationDate: new Date()
|
||||||
|
} as Task;
|
||||||
|
|
||||||
|
this._taskService.add(newTask);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user