-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: (WIP) start migrating to ngrx style
ngrx seems to be the way to go with Angular. Therefore adapt its style, so that future migrating will become more easy refs: #2318
- Loading branch information
1 parent
60e89b2
commit 18d8e36
Showing
11 changed files
with
71 additions
and
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Injectable } from "@angular/core" | ||
import { Observable } from "rxjs" | ||
|
||
import { CCAction } from "../../codeCharta.model" | ||
import { CcState, Store as PlainStore } from "../store/store" | ||
|
||
type Selector<T> = (state: CcState) => T | ||
|
||
// todo: Add tests | ||
|
||
@Injectable({ | ||
providedIn: "root" | ||
}) | ||
export class Store { | ||
select<T>(selector: Selector<T>): Observable<T> { | ||
return new Observable<T>(subscriber => { | ||
let lastValue = selector(PlainStore.store.getState()) | ||
subscriber.next(lastValue) | ||
return PlainStore.store.subscribe(() => { | ||
const newValue = selector(PlainStore.store.getState()) | ||
if (lastValue !== newValue) { | ||
lastValue = newValue | ||
subscriber.next(newValue) | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
dispatch(action: CCAction) { | ||
PlainStore.store.dispatch(action) | ||
} | ||
} |
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
16 changes: 6 additions & 10 deletions
16
...codeCharta/state/store/appSettings/sortingOrderAscending/sortingOrderAscending.actions.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 |
---|---|---|
@@ -1,21 +1,17 @@ | ||
import { Action } from "redux" | ||
|
||
export enum SortingOrderAscendingActions { | ||
SET_SORTING_ORDER_ASCENDING = "SET_SORTING_ORDER_ASCENDING" | ||
TOGGLE_SORTING_ORDER_ASCENDING = "TOGGLE_SORTING_ORDER_ASCENDING" | ||
} | ||
|
||
export interface SetSortingOrderAscendingAction extends Action { | ||
type: SortingOrderAscendingActions.SET_SORTING_ORDER_ASCENDING | ||
payload: boolean | ||
export interface ToggleSortingOrderAscendingAction extends Action { | ||
type: SortingOrderAscendingActions.TOGGLE_SORTING_ORDER_ASCENDING | ||
} | ||
|
||
export type SortingOrderAscendingAction = SetSortingOrderAscendingAction | ||
export type SortingOrderAscendingAction = ToggleSortingOrderAscendingAction | ||
|
||
export function setSortingOrderAscending(sortingOrderAscending: boolean = defaultSortingOrderAscending): SetSortingOrderAscendingAction { | ||
export function toggleSortingOrderAscending(): ToggleSortingOrderAscendingAction { | ||
return { | ||
type: SortingOrderAscendingActions.SET_SORTING_ORDER_ASCENDING, | ||
payload: sortingOrderAscending | ||
type: SortingOrderAscendingActions.TOGGLE_SORTING_ORDER_ASCENDING | ||
} | ||
} | ||
|
||
export const defaultSortingOrderAscending = false |
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
10 changes: 6 additions & 4 deletions
10
...codeCharta/state/store/appSettings/sortingOrderAscending/sortingOrderAscending.reducer.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
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
5 changes: 0 additions & 5 deletions
5
...odeCharta/state/store/appSettings/sortingOrderAscending/sortingOrderAscending.splitter.ts
This file was deleted.
Oops, something went wrong.
6 changes: 3 additions & 3 deletions
6
visualization/app/codeCharta/ui/sortingButton/sortingButton.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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
<button | ||
class="box-rounded" | ||
(click)="onButtonClick()" | ||
title="Toggle sort order (currently {{ orderAscending.value ? 'ascending' : 'descending' }})" | ||
(click)="onClick()" | ||
title="Toggle sort order (currently {{ (orderAscending$ | async) ? 'ascending' : 'descending' }})" | ||
> | ||
<i [className]="orderAscending.value ? 'fa fa-sort-amount-asc' : 'fa fa-sort-amount-desc'"></i> | ||
<i [className]="(orderAscending$ | async) ? 'fa fa-sort-amount-asc' : 'fa fa-sort-amount-desc'"></i> | ||
</button> |
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
22 changes: 13 additions & 9 deletions
22
visualization/app/codeCharta/ui/sortingButton/sortingButton.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 |
---|---|---|
@@ -1,18 +1,22 @@ | ||
import { Component } from "@angular/core" | ||
import { Component, Inject } from "@angular/core" | ||
import { Observable } from "rxjs" | ||
|
||
import { connect } from "../../state/angular-redux/connect" | ||
import { setSortingOrderAscending } from "../../state/store/appSettings/sortingOrderAscending/sortingOrderAscending.actions" | ||
import { Store } from "../../state/angular-redux/store" | ||
import { toggleSortingOrderAscending } from "../../state/store/appSettings/sortingOrderAscending/sortingOrderAscending.actions" | ||
import { sortingOrderAscendingSelector } from "../../state/store/appSettings/sortingOrderAscending/sortingOrderAscending.selector" | ||
import { CcState } from "../../state/store/store" | ||
|
||
const ConnectedClass = connect((state: CcState) => ({ orderAscending: sortingOrderAscendingSelector(state) }), { setSortingOrderAscending }) | ||
|
||
@Component({ | ||
selector: "cc-sorting-button", | ||
template: require("./sortingButton.component.html") | ||
}) | ||
export class SortingButtonComponent extends ConnectedClass { | ||
onButtonClick() { | ||
this.setSortingOrderAscending(!this.orderAscending.value) | ||
export class SortingButtonComponent { | ||
orderAscending$: Observable<boolean> | ||
|
||
constructor(@Inject(Store) private store: Store) { | ||
this.orderAscending$ = this.store.select(sortingOrderAscendingSelector) | ||
} | ||
|
||
onClick() { | ||
this.store.dispatch(toggleSortingOrderAscending()) | ||
} | ||
} |