Change store storage system.
This commit is contained in:
@@ -20,7 +20,6 @@
|
|||||||
"@angular/platform-browser": "~12.2.0",
|
"@angular/platform-browser": "~12.2.0",
|
||||||
"@angular/platform-browser-dynamic": "~12.2.0",
|
"@angular/platform-browser-dynamic": "~12.2.0",
|
||||||
"@angular/router": "~12.2.0",
|
"@angular/router": "~12.2.0",
|
||||||
"ngx-cookie-service": "^12.0.3",
|
|
||||||
"rxjs": "~6.6.0",
|
"rxjs": "~6.6.0",
|
||||||
"tslib": "^2.3.0",
|
"tslib": "^2.3.0",
|
||||||
"uuid": "^8.3.2",
|
"uuid": "^8.3.2",
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
<div class="container">
|
<div class="container">
|
||||||
<app-task-display class="task" *ngFor="let i of [0,1,2,3,4,5,6,7,8,9]" class="task"></app-task-display>
|
<app-task-display *ngFor="let task of activeTaskList?.tasks"
|
||||||
|
[task]="task" class="task"></app-task-display>
|
||||||
<div class="task new">
|
<div class="task new">
|
||||||
<mat-icon>add</mat-icon>
|
<mat-icon>add</mat-icon>
|
||||||
<input />
|
<input placeholder="Nouvelle tâche..."
|
||||||
|
(keydown)="onNewTaskKeyDown($event)"
|
||||||
|
[formControl]="newTaskControl"/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
import { Component, OnDestroy, OnInit } from '@angular/core';
|
import { Component, OnDestroy, OnInit } from '@angular/core';
|
||||||
|
import { FormControl, Validators } from '@angular/forms';
|
||||||
import { MatSnackBar } from '@angular/material/snack-bar';
|
import { MatSnackBar } from '@angular/material/snack-bar';
|
||||||
import { Router } from '@angular/router';
|
import { Router } from '@angular/router';
|
||||||
import { Subscription } from 'rxjs';
|
import { Subscription } from 'rxjs';
|
||||||
|
import { Task } from '../core/entity/task';
|
||||||
import { TaskList } from '../core/entity/task-list';
|
import { TaskList } from '../core/entity/task-list';
|
||||||
import { TaskListService } from '../core/service/task-list.service';
|
import { TaskListService } from '../core/service/task-list.service';
|
||||||
|
|
||||||
@@ -11,8 +13,9 @@ import { TaskListService } from '../core/service/task-list.service';
|
|||||||
styleUrls: ['./active-list-tasks.component.scss']
|
styleUrls: ['./active-list-tasks.component.scss']
|
||||||
})
|
})
|
||||||
export class ActiveListTasksComponent implements OnInit, OnDestroy {
|
export class ActiveListTasksComponent implements OnInit, OnDestroy {
|
||||||
private _activeTaskList?: TaskList;
|
|
||||||
private _storeSubscription?: Subscription;
|
private _storeSubscription?: Subscription;
|
||||||
|
activeTaskList?: TaskList;
|
||||||
|
newTaskControl: FormControl = new FormControl(undefined, Validators.required);
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private _router: Router,
|
private _router: Router,
|
||||||
@@ -23,8 +26,8 @@ export class ActiveListTasksComponent implements OnInit, OnDestroy {
|
|||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
this._storeSubscription = this._taskListService.store$.subscribe(store => {
|
this._storeSubscription = this._taskListService.store$.subscribe(store => {
|
||||||
if (store.activeTaskListId) {
|
if (store.activeTaskListId) {
|
||||||
this._activeTaskList = store.taskLists.find(taskList => taskList.id === store.activeTaskListId);
|
this.activeTaskList = store.taskLists.find(taskList => taskList.id === store.activeTaskListId);
|
||||||
if (!this._activeTaskList) {
|
if (!this.activeTaskList) {
|
||||||
this._backToTaskListPane();
|
this._backToTaskListPane();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -36,7 +39,21 @@ export class ActiveListTasksComponent implements OnInit, OnDestroy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private _backToTaskListPane(): void {
|
private _backToTaskListPane(): void {
|
||||||
this._snackBar.open('La task-list active n\'existe pas dans le store.', 'Fermer', {duration: 5000});
|
console.error('La task-list active n\'existe pas dans le store.', 'Fermer', {duration: 5000});
|
||||||
this._router.navigate(['/']);
|
this._router.navigate(['/']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onNewTaskKeyDown(event: KeyboardEvent): void {
|
||||||
|
if (event.key === 'Enter') {
|
||||||
|
if (this.newTaskControl.valid) {
|
||||||
|
const newTask = {
|
||||||
|
title: this.newTaskControl.value as string,
|
||||||
|
creationDate: new Date(),
|
||||||
|
description: undefined as unknown as string
|
||||||
|
} as Task;
|
||||||
|
|
||||||
|
this._taskListService.addTask(newTask);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
<mat-icon class="drag-n-drop">drag_handle</mat-icon>
|
<mat-icon class="drag-n-drop">drag_handle</mat-icon>
|
||||||
<mat-checkbox class="example-margin"></mat-checkbox>
|
<mat-checkbox class="example-margin"></mat-checkbox>
|
||||||
<div class="input-container">
|
<div class="input-container">
|
||||||
<input type="text" [formControl]="taskNameControl" />
|
<input type="text" [formControl]="titleControl" />
|
||||||
</div>
|
</div>
|
||||||
<button mat-button type="button" class="expand">
|
<button mat-button type="button" class="expand">
|
||||||
<mat-icon>expand_more</mat-icon>
|
<mat-icon>expand_more</mat-icon>
|
||||||
|
|||||||
@@ -35,13 +35,11 @@
|
|||||||
padding: 0 .8rem;
|
padding: 0 .8rem;
|
||||||
margin: 0 .8rem;
|
margin: 0 .8rem;
|
||||||
background-color: #444;
|
background-color: #444;
|
||||||
|
|
||||||
border-style: solid;
|
border-style: solid;
|
||||||
border-width: .1rem;
|
border-width: .1rem;
|
||||||
border-color: rgba(0,0,0, 0);
|
border-color: rgba(0,0,0, 0);
|
||||||
border-radius: .2rem;
|
border-radius: .2rem;
|
||||||
|
|
||||||
|
|
||||||
width: 100%;
|
width: 100%;
|
||||||
|
|
||||||
transition: background-color .2s ease-out,
|
transition: background-color .2s ease-out,
|
||||||
|
|||||||
@@ -1,16 +1,23 @@
|
|||||||
import { Component, OnInit, ViewChild } from '@angular/core';
|
import { AfterViewInit, Component, Input, OnInit } from '@angular/core';
|
||||||
import { FormControl, Validators } from '@angular/forms';
|
import { FormControl } from '@angular/forms';
|
||||||
|
import { Task } from 'src/app/core/entity/task';
|
||||||
|
import { TaskListService } from 'src/app/core/service/task-list.service';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-task-display',
|
selector: 'app-task-display',
|
||||||
templateUrl: './task-display.component.html',
|
templateUrl: './task-display.component.html',
|
||||||
styleUrls: ['./task-display.component.scss']
|
styleUrls: ['./task-display.component.scss']
|
||||||
})
|
})
|
||||||
export class TaskDisplayComponent implements OnInit {
|
export class TaskDisplayComponent implements AfterViewInit {
|
||||||
taskNameControl = new FormControl(undefined);
|
@Input() task?: Task;
|
||||||
|
titleControl = new FormControl(this.task?.title);
|
||||||
|
|
||||||
constructor() { }
|
constructor(
|
||||||
|
private _taskListService: TaskListService
|
||||||
|
) {}
|
||||||
|
|
||||||
ngOnInit(): void {
|
|
||||||
|
ngAfterViewInit(): void {
|
||||||
|
this.titleControl.setValue(this?.task?.title);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { NgModule } from '@angular/core';
|
import { APP_INITIALIZER, NgModule } from '@angular/core';
|
||||||
import { BrowserModule } from '@angular/platform-browser';
|
import { BrowserModule } from '@angular/platform-browser';
|
||||||
import { CookieService } from 'ngx-cookie-service';
|
import { CookieService } from 'ngx-cookie-service';
|
||||||
|
|
||||||
@@ -20,6 +20,8 @@ import { HeaderComponent } from './core/components/header/header.component';
|
|||||||
import { ActiveListTasksComponent } from './active-list-tasks/active-list-tasks.component';
|
import { ActiveListTasksComponent } from './active-list-tasks/active-list-tasks.component';
|
||||||
import { TaskDisplayComponent } from './active-list-tasks/task-display/task-display.component';
|
import { TaskDisplayComponent } from './active-list-tasks/task-display/task-display.component';
|
||||||
import {MatCheckboxModule} from '@angular/material/checkbox';
|
import {MatCheckboxModule} from '@angular/material/checkbox';
|
||||||
|
import { TaskListService } from './core/service/task-list.service';
|
||||||
|
import { StorePersistenceService } from './core/service/store-persistence.service';
|
||||||
|
|
||||||
@NgModule({
|
@NgModule({
|
||||||
declarations: [
|
declarations: [
|
||||||
@@ -47,6 +49,12 @@ import {MatCheckboxModule} from '@angular/material/checkbox';
|
|||||||
],
|
],
|
||||||
providers: [
|
providers: [
|
||||||
CookieService,
|
CookieService,
|
||||||
|
{
|
||||||
|
provide: APP_INITIALIZER,
|
||||||
|
useFactory: (taskListService: TaskListService) => () => taskListService.removeActiveTaskList(),
|
||||||
|
deps: [TaskListService, StorePersistenceService],
|
||||||
|
multi: true
|
||||||
|
}
|
||||||
],
|
],
|
||||||
bootstrap: [AppComponent]
|
bootstrap: [AppComponent]
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,30 +1,25 @@
|
|||||||
import { Injectable } from "@angular/core";
|
import { Injectable } from "@angular/core";
|
||||||
import { CookieService } from "ngx-cookie-service";
|
|
||||||
import { Store } from "../entity/store";
|
import { Store } from "../entity/store";
|
||||||
|
|
||||||
const COOKIE_NAME = 'todo-store';
|
const STORE_NAME = 'todo-store';
|
||||||
|
|
||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: 'root'
|
providedIn: 'root'
|
||||||
})
|
})
|
||||||
export class StorePersistenceService {
|
export class StorePersistenceService {
|
||||||
constructor(
|
|
||||||
private _cookieService: CookieService
|
|
||||||
) {}
|
|
||||||
|
|
||||||
save(store: Store): void {
|
save(store: Store): void {
|
||||||
const serializedStore = JSON.stringify(store);
|
const serializedStore = JSON.stringify(store);
|
||||||
this._cookieService.set(COOKIE_NAME, serializedStore);
|
localStorage.setItem(STORE_NAME, serializedStore);
|
||||||
}
|
}
|
||||||
|
|
||||||
load(): Store {
|
load(): Store {
|
||||||
const serializedStore = this._cookieService.get(COOKIE_NAME);
|
const serializedStore = localStorage.getItem(STORE_NAME);
|
||||||
|
|
||||||
if (serializedStore?.length) {
|
if (serializedStore?.length && serializedStore !== 'undefined') {
|
||||||
try {
|
try {
|
||||||
return JSON.parse(serializedStore);
|
return JSON.parse(serializedStore);
|
||||||
} catch (jsonParseError) {
|
} catch (jsonParseError) {
|
||||||
throw new Error(`JsonSerializationException: Invalid format for store in cookie "${COOKIE_NAME}".`);
|
throw new Error(`JsonSerializationException: Invalid format for store in cookie "${STORE_NAME}".`);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return {
|
return {
|
||||||
|
|||||||
Reference in New Issue
Block a user