Skip to content

Commit ccbb4e0

Browse files
committed
mobx: Created addSessionView, added support for custom sessions
1 parent d162d6b commit ccbb4e0

File tree

3 files changed

+152
-0
lines changed

3 files changed

+152
-0
lines changed

app/src/store/store.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
} from './stores';
2121
import {
2222
AccountSectionView,
23+
AddSessionView,
2324
AppView,
2425
BatchesView,
2526
BuildSwapView,
@@ -66,6 +67,7 @@ export class Store {
6667
orderListView = new OrderListView(this);
6768
batchesView = new BatchesView(this);
6869
registerSidecarView = new RegisterSidecarView(this);
70+
addSessionView = new AddSessionView(this);
6971

7072
/** the backend api services to be used by child stores */
7173
api: {

app/src/store/views/addSessionView.ts

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
import { makeAutoObservable, observable } from 'mobx';
2+
import { Store } from 'store';
3+
import * as LIT from 'types/generated/lit-sessions_pb';
4+
import { MAX_DATE } from 'util/constants';
5+
6+
export default class AddSessionView {
7+
private _store: Store;
8+
9+
label = '';
10+
permissionType = 'admin';
11+
editing = false;
12+
permissions: { [key: string]: boolean } = {
13+
openChannel: false,
14+
closeChannel: false,
15+
setFees: false,
16+
loop: false,
17+
pool: false,
18+
viewActivity: false,
19+
send: false,
20+
receive: false,
21+
};
22+
expiration = 'never';
23+
expirationDate = '';
24+
showAdvanced = false;
25+
proxy = '';
26+
27+
constructor(store: Store) {
28+
makeAutoObservable(
29+
this,
30+
{
31+
permissions: observable.deep,
32+
},
33+
{ deep: false, autoBind: true },
34+
);
35+
36+
this._store = store;
37+
}
38+
39+
//
40+
// Private helper functions
41+
//
42+
43+
private setAllPermissions(value: boolean) {
44+
Object.keys(this.permissions).forEach(permissionName => {
45+
this.permissions[permissionName] = value;
46+
});
47+
}
48+
49+
//
50+
// Actions
51+
//
52+
53+
setLabel(label: string) {
54+
this.label = label;
55+
}
56+
57+
setExpiration(expiration: string) {
58+
this.expiration = expiration;
59+
}
60+
61+
setExpirationDate(expirationDate: string) {
62+
this.expirationDate = expirationDate;
63+
}
64+
65+
setProxy(proxy: string) {
66+
this.proxy = proxy;
67+
}
68+
69+
setPermissionType(permissionType: string) {
70+
this.permissionType = permissionType;
71+
72+
switch (permissionType) {
73+
case 'admin':
74+
this.setAllPermissions(true);
75+
break;
76+
77+
case 'read-only':
78+
this.setAllPermissions(false);
79+
this.permissions.viewActivity = true;
80+
break;
81+
82+
case 'liquidity':
83+
this.setAllPermissions(false);
84+
this.permissions.setFees = true;
85+
this.permissions.loop = true;
86+
this.permissions.pool = true;
87+
break;
88+
89+
case 'payments':
90+
this.setAllPermissions(false);
91+
this.permissions.send = true;
92+
this.permissions.receive = true;
93+
break;
94+
95+
case 'custom':
96+
// We don't need to change anything, let the user customize permissions how they want
97+
break;
98+
}
99+
}
100+
101+
togglePermission(permission: string) {
102+
this.setPermissionType('custom');
103+
this.permissions[permission] = !this.permissions[permission];
104+
}
105+
106+
toggleEditing() {
107+
this.editing = !this.editing;
108+
}
109+
110+
toggleAdvanced() {
111+
this.showAdvanced = !this.showAdvanced;
112+
}
113+
114+
cancel() {
115+
this.label = '';
116+
this.permissionType = 'admin';
117+
this.editing = false;
118+
this.setAllPermissions(false);
119+
this.expiration = 'never';
120+
this.showAdvanced = false;
121+
}
122+
123+
//
124+
// Async Actions
125+
//
126+
127+
async handleSubmit() {
128+
if (this.permissionType === 'custom') {
129+
this._store.settingsStore.sidebarVisible = false;
130+
this._store.router.push('/connect/custom');
131+
} else {
132+
const sessionType =
133+
this.permissionType === 'admin'
134+
? LIT.SessionType.TYPE_MACAROON_ADMIN
135+
: LIT.SessionType.TYPE_MACAROON_READONLY;
136+
137+
const session = await this._store.sessionStore.addSession(
138+
this.label,
139+
sessionType,
140+
MAX_DATE,
141+
true,
142+
);
143+
144+
if (session) {
145+
this.cancel();
146+
}
147+
}
148+
}
149+
}

app/src/store/views/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ export { default as OrderListView } from './orderListView';
1010
export { default as LeaseView } from './leaseView';
1111
export { default as BatchesView } from './batchesView';
1212
export { default as RegisterSidecarView } from './registerSidecarView';
13+
export { default as AddSessionView } from './addSessionView';

0 commit comments

Comments
 (0)