Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(store): Centralize store #49

Merged
merged 1 commit into from
Aug 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@angular/router": "6.1.0",
"@angular/service-worker": "6.1.0",
"@ngrx/effects": "^6.0.1",
"@ngrx/entity": "^6.1.0",
"@ngrx/router-store": "^6.0.1",
"@ngrx/store": "^6.0.1",
"@ngrx/store-devtools": "^6.0.1",
Expand Down
22 changes: 2 additions & 20 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@ import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { ServiceWorkerModule } from '@angular/service-worker';
import { EffectsModule } from '@ngrx/effects';
import {
RouterStateSerializer,
StoreRouterConnectingModule
} from '@ngrx/router-store';
import { StoreModule } from '@ngrx/store';
import { StoreDevtoolsModule } from '@ngrx/store-devtools';
import { CookieModule } from 'ngx-cookie';
import { MarkdownModule } from 'ngx-markdown';
import { environment } from './../environments/environment';
Expand All @@ -18,7 +11,7 @@ import { AppComponent } from './core/containers/app/app.component';
import { CoreModule } from './core/core.module';
import { SteemconnectConfig } from './steemconnect/config';
import { SteemconnectModule } from './steemconnect/steemconnect.module';
import * as fromStore from './store';
import { RootStoreModule } from './store/root-store.module';

@NgModule({
imports: [
Expand All @@ -33,21 +26,10 @@ import * as fromStore from './store';
environment.steemConnectConfig as SteemconnectConfig
),
AppRoutingModule,
StoreModule.forRoot(fromStore.reducers),
StoreDevtoolsModule.instrument({
maxAge: 10,
logOnly: environment.production
}),
StoreRouterConnectingModule.forRoot({
stateKey: 'router'
}),
EffectsModule.forRoot(fromStore.effects),
RootStoreModule,
CookieModule.forRoot(),
MarkdownModule.forRoot()
],
providers: [
{ provide: RouterStateSerializer, useClass: fromStore.CustomSerializer }
],
bootstrap: [AppComponent]
})
export class AppModule {}
2 changes: 1 addition & 1 deletion src/app/core/containers/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</span>

<span fxFlex class="toolbar__button">
<app-log-in-out [isAuthenticated]="isAuthenticated$ | async" [isLoggingOut]="isLoggingOut$ | async" (login)="login()" (logout)="logout()"></app-log-in-out>
<app-log-in-out [isAuthenticated]="!!(currentUser$ | async)" [isLoggingOut]="isLoggingOut$ | async" (login)="login()" (logout)="logout()"></app-log-in-out>
</span>
</mat-toolbar-row>

Expand Down
17 changes: 9 additions & 8 deletions src/app/core/containers/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
import { Store } from '@ngrx/store';
import { Observable } from 'rxjs';
import * as fromStore from '../../../store';
import { selectCurrentUser, selectLoggingOut } from '../../../store/auth-store';
import { State } from '../../../store/root-state';
import { authActionCreators } from './../../../store/auth-store/actions';

@Component({
selector: 'app-root',
Expand All @@ -10,22 +12,21 @@ import * as fromStore from '../../../store';
changeDetection: ChangeDetectionStrategy.OnPush
})
export class AppComponent implements OnInit {
isAuthenticated$: Observable<boolean>;
currentUser$: Observable<string | null>;
isLoggingOut$: Observable<boolean>;

constructor(private store: Store<fromStore.State>) {}
constructor(private store: Store<State>) {}

ngOnInit() {
this.store.dispatch(fromStore.authenticate());
this.isAuthenticated$ = this.store.select(fromStore.selectAuthenticated);
this.isLoggingOut$ = this.store.select(fromStore.selectLoggingOut);
this.currentUser$ = this.store.select(selectCurrentUser);
this.isLoggingOut$ = this.store.select(selectLoggingOut);
}

login() {
this.store.dispatch(fromStore.login());
this.store.dispatch(authActionCreators.login());
}

logout() {
this.store.dispatch(fromStore.logout());
this.store.dispatch(authActionCreators.logout());
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Empty file.

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion src/app/drafts/containers/drafts/drafts.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@

</div>

<button mat-fab class="drafts__fab" ngClass.lt-md="drafts__fab--mobile" (click)="openNewDraftDialog()">
<button mat-fab class="drafts__fab" ngClass.lt-md="drafts__fab--mobile" (click)="createDraft()">
<mat-icon>add</mat-icon>
</button>
51 changes: 18 additions & 33 deletions src/app/drafts/containers/drafts/drafts.component.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { Store } from '@ngrx/store';
import { Observable } from 'rxjs';
import * as fromRootStore from '../../../store';
import { NewDraftDialogComponent } from '../../components/new-draft-dialog/new-draft-dialog.component';
import * as fromTemplates from '../../templates';
import { Draft } from './../../models/draft.model';
import * as fromFeatureStore from './../../store';
import {
Draft,
draftsActionCreators,
selectAllDrafts,
selectLoaded,
selectLoading
} from '../../../store/drafts-store';
import { State } from '../../../store/root-state';
import { routerActionCreators } from './../../../store/router-store';

@Component({
selector: 'app-drafts',
Expand All @@ -19,42 +22,24 @@ export class DraftsComponent implements OnInit {
areLoading$: Observable<boolean>;
areLoaded$: Observable<boolean>;

constructor(
public dialog: MatDialog,
private store: Store<fromFeatureStore.State>
) {}
constructor(private store: Store<State>) {}

ngOnInit() {
this.drafts$ = this.store.select(fromFeatureStore.selectAllDrafts);
this.areLoading$ = this.store.select(fromFeatureStore.selectDraftsLoading);
this.areLoaded$ = this.store.select(fromFeatureStore.selectDraftsLoaded);
this.store.dispatch(fromFeatureStore.loadDrafts());
this.drafts$ = this.store.select(selectAllDrafts);
this.areLoading$ = this.store.select(selectLoading);
this.areLoaded$ = this.store.select(selectLoaded);
this.store.dispatch(draftsActionCreators.loadDrafts());
}

editDraft(id: number) {
this.store.dispatch(new fromRootStore.Go({ path: ['/drafts', id] }));
this.store.dispatch(routerActionCreators.go({ path: ['/drafts', id] }));
}

removeDraft(id: number) {
this.store.dispatch(fromFeatureStore.removeDraft(id));
this.store.dispatch(draftsActionCreators.removeDraft(id));
}

openNewDraftDialog() {
this.dialog
.open(NewDraftDialogComponent, {
data: {
all: [...fromTemplates.all],
entities: { ...fromTemplates.entities }
},
width: '400px'
})
.afterClosed()
.subscribe(result => {
// clicking out of the dialog window returns undefined
// we want to dispatch an action only if user clicked 'Create':
if (result) {
this.store.dispatch(fromFeatureStore.createDraft(result));
}
});
createDraft() {
this.store.dispatch(draftsActionCreators.createDraft());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
<app-editor (whenSubmit)="broadcast($event)"></app-editor>
</ng-template>

<mat-spinner *ngIf="(broadcastState$ | async)?.broadcasting"></mat-spinner>
<mat-spinner *ngIf="isBroadcasting$ | async"></mat-spinner>
Loading