-
Notifications
You must be signed in to change notification settings - Fork 103
/
token-router.ts
92 lines (80 loc) · 2.81 KB
/
token-router.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { get, writable } from 'svelte/store'
import { Subrouter } from '@core/router'
import {
NewTransactionType,
updateNewTransactionDetails,
NotVerifiedStatus,
unverifyAsset,
VerifiedStatus,
verifyAsset,
getAssetById,
} from '@core/wallet'
import { selectedActivity, selectedAsset, TokenAction } from '@/contexts/dashboard'
import { activityRouter, dashboardRouter, sendRouter } from '../'
import { TokenRoute, DashboardRoute } from '../../enums'
import { ITokenRouterEvent } from '../../interfaces'
import { resetRouterWithDrawerDelay } from '../../utils'
export const tokenRoute = writable<TokenRoute>(null)
export const tokenRouter = writable<TokenRouter>(null)
export class TokenRouter extends Subrouter<TokenRoute> {
constructor() {
super(TokenRoute.Info, tokenRoute, get(dashboardRouter))
}
public next(event: ITokenRouterEvent = {}): void {
const { asset, action } = event
if (get(tokenRoute) === TokenRoute.Info) {
if (asset) {
selectedAsset.set(asset)
}
if (action) {
this.handleTokenAction(action)
}
}
}
public closeDrawer(): void {
selectedAsset.set(null)
// previous() opens the activity information instead of closing the drawer
// when the token information are displayed because an activity was clicked.
if (!get(selectedActivity)) {
get(dashboardRouter).previous()
}
resetRouterWithDrawerDelay(get(tokenRouter))
}
private activitySelected(): boolean {
const activity = get(selectedActivity)
if (activity) {
selectedAsset.set(null)
get(dashboardRouter).reset()
get(tokenRouter).reset()
get(activityRouter)?.next({ activity })
return true
}
return false
}
private handleTokenAction(action): void {
if (!get(selectedAsset)) {
return
}
const { id } = get(selectedAsset)
switch (action) {
case TokenAction.Skip:
unverifyAsset(id, NotVerifiedStatus.Skipped)
if (!this.activitySelected()) {
selectedAsset.set(getAssetById(id))
}
return
case TokenAction.Verify: {
verifyAsset(id, VerifiedStatus.SelfVerified)
if (!this.activitySelected()) {
selectedAsset.set(getAssetById(id))
}
return
}
case TokenAction.Send:
updateNewTransactionDetails({ type: NewTransactionType.TokenTransfer, assetId: id })
get(sendRouter).next()
this.closeDrawer()
get(dashboardRouter).goTo(DashboardRoute.Send)
}
}
}