Added state management

This commit is contained in:
Nicolas Constant 2018-08-06 21:07:28 -04:00
parent f8ce6d0479
commit e466a6ad3a
No known key found for this signature in database
GPG Key ID: 1E9F677FB01A5688
5 changed files with 95 additions and 10 deletions

16
package-lock.json generated
View File

@ -659,6 +659,22 @@
"webpack-sources": "^1.1.0"
}
},
"@ngxs/storage-plugin": {
"version": "3.2.0",
"resolved": "https://registry.npmjs.org/@ngxs/storage-plugin/-/storage-plugin-3.2.0.tgz",
"integrity": "sha1-KXMKBohJKQAYuu7Qe0S/O0KEol8=",
"requires": {
"tslib": "^1.9.0"
}
},
"@ngxs/store": {
"version": "3.2.0",
"resolved": "https://registry.npmjs.org/@ngxs/store/-/store-3.2.0.tgz",
"integrity": "sha1-Uzk5rLQalIo0iGlDMU0qDYZgRck=",
"requires": {
"tslib": "^1.9.0"
}
},
"@schematics/angular": {
"version": "0.7.1",
"resolved": "https://registry.npmjs.org/@schematics/angular/-/angular-0.7.1.tgz",

View File

@ -25,6 +25,8 @@
"@angular/platform-browser": "^6.1.0",
"@angular/platform-browser-dynamic": "^6.1.0",
"@angular/router": "^6.1.0",
"@ngxs/storage-plugin": "^3.2.0",
"@ngxs/store": "^3.2.0",
"bootstrap": "^4.1.3",
"core-js": "^2.5.4",
"ngx-electron": "^1.0.4",

View File

@ -6,6 +6,9 @@ import { RouterModule, Routes } from "@angular/router";
import { NgxElectronModule } from "ngx-electron";
import { NgxsModule } from '@ngxs/store';
import { NgxsStoragePluginModule } from '@ngxs/storage-plugin';
import { AppComponent } from "./app.component";
import { LeftSideBarComponent } from "./components/left-side-bar/left-side-bar.component";
import { StreamsMainDisplayComponent } from "./pages/streams-main-display/streams-main-display.component";
@ -17,6 +20,7 @@ import { AuthService } from "./services/auth.service";
import { AccountsService } from "./services/accounts.service";
import { StreamsService } from "./services/streams.service";
import { StreamingService } from "./services/streaming.service";
import { RegisteredAppsState } from "./stores/registered-apps.state";
const routes: Routes = [
{ path: "", redirectTo: "home", pathMatch: "full" },
@ -40,7 +44,12 @@ const routes: Routes = [
HttpModule,
FormsModule,
NgxElectronModule,
RouterModule.forRoot(routes)
RouterModule.forRoot(routes),
NgxsModule.forRoot([
RegisteredAppsState
]),
NgxsStoragePluginModule.forRoot()
],
providers: [AuthService, AccountsService, StreamsService, StreamingService, { provide: APP_INITIALIZER, useFactory: settingsServiceFactory, deps: [AccountsService], multi: true }],
bootstrap: [AppComponent]

View File

@ -1,7 +1,11 @@
import { Component, OnInit, Input } from "@angular/core";
import { Store, Select } from '@ngxs/store';
import { AuthService } from "../../services/auth.service";
import { TokenData } from "../../services/models/mastodon.interfaces";
import { AccountsService } from "../../services/accounts.service";
import { AddRegisteredApp, RegisteredAppsState, RegisteredAppsStateModel } from "../../stores/registered-apps.state";
import { Observable } from "rxjs";
@Component({
selector: "app-register-new-account",
@ -13,35 +17,55 @@ export class RegisterNewAccountComponent implements OnInit {
// @Input() email: string;
// @Input() password: string;
result: string;
//@Select() registeredApps$: Observable<RegisteredAppsStateModel>;
registeredApps$: Observable<RegisteredAppsStateModel>;
constructor(
private readonly authService: AuthService,
private readonly accountsService: AccountsService) { }
private readonly accountsService: AccountsService,
private readonly store: Store) {
this.registeredApps$ = this.store.select(state => state.registeredapps.registeredApps);
}
ngOnInit() {
this.registeredApps$.subscribe(x => {
console.error('registeredApps$')
console.warn(x);
});
}
onSubmit(): boolean {
this.store
.dispatch(new AddRegisteredApp({ name: 'test', id: 15, client_id: 'dsqdqs', client_secret: 'dsqdqs', redirect_uri: 'dsqdqs' }))
.subscribe(res => {
console.error('dispatch');
console.warn(res);
});
let fullHandle = this.mastodonFullHandle.split('@').filter(x => x != null && x !== '');
console.log(fullHandle[0]);
console.log(fullHandle[1]);
this.result = fullHandle[0] + '*' + fullHandle[1];
// let fullHandle = this.mastodonFullHandle.split('@').filter(x => x != null && x !== '');
// console.log(fullHandle[0]);
// console.log(fullHandle[1]);
//register app
// this.result = fullHandle[0] + '*' + fullHandle[1];
//redirect to oauth
// window.location.href = "https://google.com";
window.location.href = "https://google.com";
//register app
//ask for getting token

View File

@ -0,0 +1,34 @@
import { State, Action, StateContext } from '@ngxs/store';
export class AddRegisteredApp {
static readonly type = '[RegisteredApps] Add app';
constructor(public app: AppInfo) { }
}
export interface RegisteredAppsStateModel {
registeredApps: AppInfo[];
}
@State<RegisteredAppsStateModel>({
name: 'registeredapps',
defaults: {
registeredApps: []
}
})
export class RegisteredAppsState {
@Action(AddRegisteredApp)
AddRegisteredApp(ctx: StateContext<RegisteredAppsStateModel>, action: AddRegisteredApp) {
const state = ctx.getState();
ctx.setState({
registeredApps: [...state.registeredApps, action.app]
});
}
}
export class AppInfo {
id: number;
name: string;
redirect_uri: string;
client_id: string;
client_secret: string;
}