forked from angular-architects/ngrx-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add storage sync * doc: add storage sync docs
- Loading branch information
Showing
11 changed files
with
588 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { patchState, signalStore, withMethods } from '@ngrx/signals'; | ||
import { | ||
withEntities, | ||
setEntity, | ||
removeEntity, | ||
updateEntity, | ||
} from '@ngrx/signals/entities'; | ||
import { AddTodo, Todo } from '../todo-store'; | ||
import { withStorageSync } from 'ngrx-toolkit'; | ||
|
||
export const SyncedTodoStore = signalStore( | ||
{ providedIn: 'root' }, | ||
withEntities<Todo>(), | ||
withStorageSync({ | ||
key: 'todos', | ||
}), | ||
withMethods((store) => { | ||
let currentId = 0; | ||
return { | ||
add(todo: AddTodo) { | ||
patchState(store, setEntity({ id: ++currentId, ...todo })); | ||
}, | ||
|
||
remove(id: number) { | ||
patchState(store, removeEntity(id)); | ||
}, | ||
|
||
toggleFinished(id: number): void { | ||
const todo = store.entityMap()[id]; | ||
patchState( | ||
store, | ||
updateEntity({ id, changes: { finished: !todo.finished } }) | ||
); | ||
}, | ||
}; | ||
}) | ||
); |
43 changes: 43 additions & 0 deletions
43
apps/demo/src/app/todo-storage-sync/todo-storage-sync.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<mat-table [dataSource]="dataSource" class="mat-elevation-z8"> | ||
<!-- Checkbox Column --> | ||
<ng-container matColumnDef="finished"> | ||
<mat-header-cell *matHeaderCellDef></mat-header-cell> | ||
<mat-cell *matCellDef="let row" class="actions"> | ||
<mat-checkbox | ||
(click)="$event.stopPropagation()" | ||
(change)="checkboxLabel(row)" | ||
[checked]="row.finished" | ||
> | ||
</mat-checkbox> | ||
<mat-icon (click)="removeTodo(row)">delete</mat-icon> | ||
</mat-cell> | ||
</ng-container> | ||
|
||
<!-- Name Column --> | ||
<ng-container matColumnDef="name"> | ||
<mat-header-cell *matHeaderCellDef>Name</mat-header-cell> | ||
<mat-cell *matCellDef="let element">{{ element.name }}</mat-cell> | ||
</ng-container> | ||
|
||
<!-- Description Column --> | ||
<ng-container matColumnDef="description"> | ||
<mat-header-cell *matHeaderCellDef>Description</mat-header-cell> | ||
<mat-cell *matCellDef="let element">{{ element.description }}</mat-cell> | ||
</ng-container> | ||
|
||
<!-- Deadline Column --> | ||
<ng-container matColumnDef="deadline"> | ||
<mat-header-cell mat-header-cell *matHeaderCellDef | ||
>Deadline</mat-header-cell | ||
> | ||
<mat-cell mat-cell *matCellDef="let element">{{ | ||
element.deadline | ||
}}</mat-cell> | ||
</ng-container> | ||
|
||
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row> | ||
<mat-row | ||
*matRowDef="let row; columns: displayedColumns" | ||
(click)="selection.toggle(row)" | ||
></mat-row> | ||
</mat-table> |
Empty file.
38 changes: 38 additions & 0 deletions
38
apps/demo/src/app/todo-storage-sync/todo-storage-sync.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Component, effect, inject } from '@angular/core'; | ||
import { MatCheckboxModule } from '@angular/material/checkbox'; | ||
import { MatIconModule } from '@angular/material/icon'; | ||
import { MatTableDataSource, MatTableModule } from '@angular/material/table'; | ||
import { SyncedTodoStore } from './synced-todo-store'; | ||
import { SelectionModel } from '@angular/cdk/collections'; | ||
import { CategoryStore } from '../category.store'; | ||
import { Todo } from '../todo-store'; | ||
|
||
@Component({ | ||
selector: 'demo-todo-storage-sync', | ||
standalone: true, | ||
imports: [MatCheckboxModule, MatIconModule, MatTableModule], | ||
templateUrl: './todo-storage-sync.component.html', | ||
styleUrl: './todo-storage-sync.component.scss', | ||
}) | ||
export class TodoStorageSyncComponent { | ||
todoStore = inject(SyncedTodoStore); | ||
categoryStore = inject(CategoryStore); | ||
|
||
displayedColumns: string[] = ['finished', 'name', 'description', 'deadline']; | ||
dataSource = new MatTableDataSource<Todo>([]); | ||
selection = new SelectionModel<Todo>(true, []); | ||
|
||
constructor() { | ||
effect(() => { | ||
this.dataSource.data = this.todoStore.entities(); | ||
}); | ||
} | ||
|
||
checkboxLabel(todo: Todo) { | ||
this.todoStore.toggleFinished(todo.id); | ||
} | ||
|
||
removeTodo(todo: Todo) { | ||
this.todoStore.remove(todo.id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.