Skip to content

Commit

Permalink
feat(user/config): implement save button
Browse files Browse the repository at this point in the history
  • Loading branch information
ElonH committed May 17, 2020
1 parent 7d6d257 commit 9a810b4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
14 changes: 7 additions & 7 deletions src/app/@dataflow/extra/users-flow.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Observable, of } from 'rxjs';
import { BareFlow, FlowInNode, CombErr } from '../core';
import { Observable, of, Subject } from 'rxjs';
import { BareFlow, FlowInNode, CombErr } from '../core';
import { startWith } from 'rxjs/operators';

export interface IRcloneServer {
url: string;
Expand All @@ -19,35 +20,34 @@ export abstract class UsersFlow extends BareFlow<FlowInNode, UsersFlowNode> {
public static readonly defaultUser: IUser[] = [
{ name: 'localhost', url: 'http://localhost:5572' },
];
private static trigger$ = new Subject<number>();
protected request(pre: CombErr<FlowInNode>): Observable<CombErr<UsersFlowNode>> {
const dataRaw = localStorage.getItem('users');
if (dataRaw) return of([{ users: JSON.parse(dataRaw) }, []]);
localStorage.setItem('users', JSON.stringify(UsersFlow.defaultUser));
return of([{ users: UsersFlow.defaultUser }, []]);
}
public static setAll(data: IUser[]) {
// TODO: trigger dataflow
localStorage.setItem('users', JSON.stringify(data));
}
public static set(user: IUser) {
// TODO: trigger dataflow
const dataRaw = localStorage.getItem('users');
if (dataRaw) {
const data = JSON.parse(dataRaw) as IUser[];
for (let i = 0; i < data.length; i++) {
if (data[i].name === user.name) {
data[i] = user;
localStorage.setItem('users', JSON.stringify(data));
this.setAll(data);
return;
}
}
data.push(user);
localStorage.setItem('users', JSON.stringify(data));
this.setAll(data);
return;
}
const data = [];
data.push(user);
localStorage.setItem('users', JSON.stringify(data));
this.setAll(data);
}
public static purge() {
localStorage.removeItem('users');
Expand Down
19 changes: 16 additions & 3 deletions src/app/pages/user/config/config.component.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Component, OnInit, ChangeDetectionStrategy, Input } from '@angular/core';
import { Component, OnInit, ChangeDetectionStrategy, Input, Output, EventEmitter } from '@angular/core';
import { Observable, Subject } from 'rxjs';
import {
UsersFlowNode,
NameValidation,
NameValidationPreNode,
IRcloneServer,
UsersFlow,
} from 'src/app/@dataflow/extra';
import { FormControl } from '@angular/forms';
import { withLatestFrom, map, startWith, filter } from 'rxjs/operators';
Expand Down Expand Up @@ -98,7 +99,7 @@ import { NoopAuthFlow } from 'src/app/@dataflow/rclone';
</button>
</nb-action>
<nb-action>
<button nbButton outline status="primary" [disabled]="disableSave$ | async">
<button nbButton outline status="primary" [disabled]="disableSave$ | async" (click)="save()">
save
</button>
</nb-action>
Expand Down Expand Up @@ -129,7 +130,9 @@ export class ConfigComponent implements OnInit {
connectTrigger$ = new Subject<number>();

@Input()
users$: Observable<UsersFlowNode>;
users$: Observable<UsersFlowNode>;
@Output()
onSave: EventEmitter<any> = new EventEmitter();

nameValidation$: NameValidation;
authPass$: Observable<boolean | null>;
Expand All @@ -139,6 +142,16 @@ export class ConfigComponent implements OnInit {

constructor() {}

save() {
UsersFlow.set({
name: this.name.value,
url: this.url.value,
user: this.user.value,
password: this.password.value,
});
this.onSave.emit(true);
}

ngOnInit(): void {
const outer = this;
this.nameValidation$ = new (class extends NameValidation {
Expand Down
8 changes: 6 additions & 2 deletions src/app/pages/user/user.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { CombErr, FlowInNode } from 'src/app/@dataflow/core';
<p>
user works!
</p>
<user-config [users$]="usersFlow$.getOutput()"> </user-config>
<user-config [users$]="usersFlow$.getOutput()" (onSave)="onSave()"> </user-config>
`,
styles: [],
})
Expand All @@ -27,5 +27,9 @@ export class UserComponent implements OnInit {
this.usersFlow$.deploy();

this.userSubject.next(1);
}
}

onSave(){
this.userSubject.next(1);
}
}

0 comments on commit 9a810b4

Please sign in to comment.