Compare commits

..

2 Commits

Author SHA1 Message Date
1037d74620 Minor visual changes 2022-03-05 12:54:19 +01:00
4cfed23613 Add description saving and silent save in service. 2022-03-05 12:36:59 +01:00
5 changed files with 130 additions and 52 deletions

View File

@@ -16,15 +16,13 @@
<label for="description"> <label for="description">
Description Description
</label> </label>
<textarea id="description"></textarea> <textarea id="description" [formControl]="descriptionControl"></textarea>
</div> </div>
<div class="actions"> <div class="actions">
<div class="row"> <div class="row">
<button class="icon raised" matRipple matTooltip="Définir une alerte dans X minutes"> <button class="raised" matRipple matTooltip="Définir une alerte dans X minutes">
<mat-icon>update</mat-icon> <mat-icon>update</mat-icon>
</button> Rappel
<button class="icon raised alert" matRipple>
<mat-icon>delete</mat-icon>
</button> </button>
</div> </div>
<div class="row"> <div class="row">

View File

@@ -104,9 +104,18 @@
flex-wrap: wrap; flex-wrap: wrap;
justify-content: space-around; justify-content: space-around;
align-items: flex-start; align-items: flex-start;
padding: 1rem; padding: .5rem 1rem;
button {
display: flex;
flex-grow: 1;
}
} }
} }
} }
} }
} }
input, textarea, mat-icon, check-box {
color: #fff;
}

View File

@@ -1,5 +1,7 @@
import { AfterViewInit, Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core'; import { AfterViewInit, Component, ElementRef, EventEmitter, Input, OnDestroy, OnInit, Output, ViewChild } from '@angular/core';
import { FormControl } from '@angular/forms'; import { FormControl } from '@angular/forms';
import { Subscription } from 'rxjs';
import { debounceTime, distinctUntilChanged } from 'rxjs/operators';
import { Task } from 'src/app/core/entity/task'; import { Task } from 'src/app/core/entity/task';
import { TaskListService } from 'src/app/core/service/task-list.service'; import { TaskListService } from 'src/app/core/service/task-list.service';
@@ -9,11 +11,14 @@ import { TaskListService } from 'src/app/core/service/task-list.service';
templateUrl: './task-display.component.html', templateUrl: './task-display.component.html',
styleUrls: ['./task-display.component.scss'] styleUrls: ['./task-display.component.scss']
}) })
export class TaskDisplayComponent implements AfterViewInit { export class TaskDisplayComponent implements AfterViewInit, OnDestroy {
@Input() task?: Task; @Input() task?: Task;
titleControl = new FormControl(this.task?.title);
isExpanded = false;
@ViewChild('titleInput', {static: true}) titleInput?: ElementRef<HTMLInputElement>; @ViewChild('titleInput', {static: true}) titleInput?: ElementRef<HTMLInputElement>;
@ViewChild('descriptionInput', {static: true}) descriptionInput?: ElementRef<HTMLTextAreaElement>;
titleControl = new FormControl();
descriptionControl = new FormControl();
isExpanded = false;
private _subscriptions: Subscription[] = [];
constructor( constructor(
private _taskListService: TaskListService private _taskListService: TaskListService
@@ -21,6 +26,37 @@ export class TaskDisplayComponent implements AfterViewInit {
ngAfterViewInit(): void { ngAfterViewInit(): void {
this.titleControl.setValue(this?.task?.title); this.titleControl.setValue(this?.task?.title);
this.descriptionControl.setValue(this?.task?.description);
const titleControlSubscription = this.titleControl.valueChanges
.pipe(
distinctUntilChanged(),
debounceTime(500)
)
.subscribe(newTitle => {
if (this.task) {
this.task.title = newTitle;
this._taskListService.updateTask(this.task);
}
});
this._subscriptions.push(titleControlSubscription);
const descriptionControlSubscription = this.descriptionControl.valueChanges
.pipe(
distinctUntilChanged(),
debounceTime(500)
)
.subscribe(description => {
if (this.task) {
this.task.description = description;
this._taskListService.updateTask(this.task);
}
});
this._subscriptions.push(descriptionControlSubscription);
}
ngOnDestroy(): void {
this._subscriptions.forEach(subscription => subscription.unsubscribe());
} }
expand(): void { expand(): void {

View File

@@ -19,7 +19,7 @@ export class TaskListService {
private _storePersistenceService: StorePersistenceService private _storePersistenceService: StorePersistenceService
) { ) {
this.store$.subscribe(store => { this.store$.subscribe(store => {
this._storePersistenceService.save(store); this.saveStore(store, true);
}); });
} }
@@ -32,6 +32,15 @@ export class TaskListService {
return this._storePersistenceService.load(); return this._storePersistenceService.load();
} }
private saveStore(store: Store, silent: boolean = false): void {
if (silent) {
this._storePersistenceService.save(store);
} else {
// We send the store into the subject because there is an observable on in, that saves the store at every single value.
this._store.next(store);
}
}
addTask(taskTitle: string): void { addTask(taskTitle: string): void {
const store = this.store; const store = this.store;
const activeTaskList = store.taskLists.find(taskList => taskList.id === store.activeTaskListId); const activeTaskList = store.taskLists.find(taskList => taskList.id === store.activeTaskListId);
@@ -52,7 +61,27 @@ export class TaskListService {
activeTaskList?.tasks.push(newTask); activeTaskList?.tasks.push(newTask);
this._store.next(store); this.saveStore(store);
}
updateTask(taskToUpdate: Task) {
const store = this.store;
const activeTaskList = store.taskLists.find(taskList => taskList.id === store.activeTaskListId);
if (!activeTaskList) {
throw new Error("No active tasklist");
}
const task = activeTaskList?.tasks.find(task => task.id === taskToUpdate.id);
if (!task) {
throw new Error('Unknown task to update');
}
task.title = taskToUpdate.title;
task.description = taskToUpdate.description;
// If the store is saved loudly, all views will be refreshed and the user will lose the focus of its input,
// so we save the store silently here.
this.saveStore(store, true);
} }
delete(taskToDelete: Task) { delete(taskToDelete: Task) {
@@ -68,7 +97,7 @@ export class TaskListService {
} }
activeTaskList?.tasks.splice(taskIndex, 1); activeTaskList?.tasks.splice(taskIndex, 1);
this._store.next(store); this.saveStore(store);
} }
createTaskList(taskListName: string): void { createTaskList(taskListName: string): void {
@@ -80,7 +109,7 @@ export class TaskListService {
const store = this.store; const store = this.store;
store.taskLists.push(newTaskList); store.taskLists.push(newTaskList);
this._store.next(store); this.saveStore(store);
} }
getAll(): TaskList[] { getAll(): TaskList[] {
@@ -90,14 +119,14 @@ export class TaskListService {
setActive(taskList: TaskList): void { setActive(taskList: TaskList): void {
const store = this.store; const store = this.store;
store.activeTaskListId = taskList.id; store.activeTaskListId = taskList.id;
this._store.next(store); this.saveStore(store);
this._router.navigate(['/task-lists/active']); this._router.navigate(['/task-lists/active']);
} }
removeActiveTaskList(): void { removeActiveTaskList(): void {
const store = this.store; const store = this.store;
delete store.activeTaskListId; delete store.activeTaskListId;
this._store.next(store); this.saveStore(store);
this._router.navigate(['/']); this._router.navigate(['/']);
} }
@@ -118,10 +147,10 @@ export class TaskListService {
store.selectionMode = false; store.selectionMode = false;
} }
this._store.next(store); this.saveStore(store);
} else { } else {
store.selectedTaskLists.push(taskList); store.selectedTaskLists.push(taskList);
this._store.next(store); this.saveStore(store);
} }
} }
@@ -137,13 +166,13 @@ export class TaskListService {
enableSelectionMode(): void { enableSelectionMode(): void {
const store = this.store; const store = this.store;
store.selectionMode = true; store.selectionMode = true;
this._store.next(store); this.saveStore(store);
} }
disableSelectionMode(): void { disableSelectionMode(): void {
const store = this.store; const store = this.store;
store.selectionMode = false; store.selectionMode = false;
store.selectedTaskLists = []; store.selectedTaskLists = [];
this._store.next(store); this.saveStore(store);
} }
} }

View File

@@ -36,6 +36,7 @@ button {
justify-content: center; justify-content: center;
align-items: center; align-items: center;
padding: .5rem; padding: .5rem;
color: #fff;
transition: background-color .2s ease-out, transition: background-color .2s ease-out,
border-color .2s ease-out; border-color .2s ease-out;
@@ -61,5 +62,10 @@ button {
&.alert { &.alert {
background: #F93154; background: #F93154;
color: #fff; color: #fff;
:hover {
background-color: #f83e5d;
border-color: #b91b35;
}
} }
} }