-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsystem.js
167 lines (157 loc) · 4.49 KB
/
system.js
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import API from "@/helpers/api";
// Initial state
const state = () => ({
version: "",
backupStatus: {
status: "", //success, failed
timestamp: null
},
backupOverTor: true, // by default we backup over Tor
mostRecentBackupSuccess: false,
onboarding: false, // assume false to prevent modal flickering
loading: true,
unit: "sats", //sats or btc
api: {
operational: false,
version: ""
},
onionAddress: "",
seedExists: false,
localExplorerUrl: false,
});
// Functions to update the state directly
const mutations = {
setVersion(state, version) {
state.version = version;
},
setUnit(state, unit) {
state.unit = unit;
},
setApi(state, api) {
state.api = api;
},
setLoading(state, loading) {
state.loading = loading;
},
setOnionAddress(state, address) {
state.onionAddress = address;
},
setBackupStatus(state, status) {
state.backupStatus = status;
},
setBackupOverTor(state, backupOverTor) {
state.backupOverTor = backupOverTor;
},
setMostRecentBackupSuccess(state, mostRecentBackupSuccess) {
state.mostRecentBackupSuccess = mostRecentBackupSuccess;
},
setOnboarding(state, status) {
state.onboarding = status;
},
setSeedExists(state, seedExists) {
state.seedExists = seedExists;
},
setLocalExplorerUrl(state, localExplorerUrl) {
state.localExplorerUrl = localExplorerUrl;
}
};
// Functions to get data from the API
const actions = {
async getVersion({ commit }) {
const data = await API.get(
`${process.env.VUE_APP_API_BASE_URL}/v1/system/info`
);
if (data && data.version) {
let { version } = data;
if (data.build) {
version += `-build-${data.build}`;
}
commit("setVersion", version);
}
},
async getUnit({ commit }) {
if (window.localStorage && window.localStorage.getItem("unit")) {
commit("setUnit", window.localStorage.getItem("unit"));
}
},
changeUnit({ commit }, unit) {
if (unit === "sats" || unit === "btc") {
window.localStorage.setItem("unit", unit);
commit("setUnit", unit);
}
},
async getApi({ commit }) {
const api = await API.get(`${process.env.VUE_APP_API_BASE_URL}/ping`);
commit("setApi", {
operational: !!(api && api.version),
version: api && api.version ? api.version : ""
});
},
async getBackupStatus({ commit }) {
const status = await API.get(
`${process.env.VUE_APP_API_BASE_URL}/v1/system/backup-status`
);
if (status && status.timestamp) {
commit("setBackupStatus", status);
}
},
async getBackupOverTor({ commit }) {
const backupOverTor = await API.get(
`${process.env.VUE_APP_API_BASE_URL}/v1/system/backup-over-tor`
);
commit("setBackupOverTor", backupOverTor);
},
async toggleBackupOverTor({ commit, state }) {
const newValue = !state.backupOverTor;
const response = await API.post(`${process.env.VUE_APP_API_BASE_URL}/v1/system/backup-over-tor`, {
backupOverTor: newValue
});
if (response.data.success) {
commit('setBackupOverTor', newValue);
}
},
async getMostRecentBackupSuccess({ commit }) {
const mostRecentBackupSuccess = await API.get(
`${process.env.VUE_APP_API_BASE_URL}/v1/system/recent-backup-success`
);
commit("setMostRecentBackupSuccess", mostRecentBackupSuccess);
},
async getOnboardingStatus({ commit }) {
const onboarding = await API.get(
`${process.env.VUE_APP_API_BASE_URL}/v1/system/onboarding`
);
commit("setOnboarding", onboarding);
},
async onboardingComplete({ commit }) {
commit("setOnboarding", false); // update state to immediately hide modal
await API.post(
`${process.env.VUE_APP_API_BASE_URL}/v1/system/onboarding`
);
},
async getSeedExists({ commit }) {
const seedExists = await API.get(
`${process.env.VUE_APP_API_BASE_URL}/v1/system/seed-exists`
);
commit("setSeedExists", seedExists);
},
async getLocalExplorerUrl({ commit }) {
const {port, hiddenService} = await API.get(
`${process.env.VUE_APP_API_BASE_URL}/v1/system/explorer`
);
let localExplorerUrl = false;
if (window.location.origin.endsWith(".onion") && hiddenService) {
localExplorerUrl = `http://${hiddenService}`;
} else if (port) {
localExplorerUrl = `${window.location.protocol}//${window.location.hostname}:${port}`;
}
commit("setLocalExplorerUrl", localExplorerUrl);
},
};
const getters = {};
export default {
namespaced: true,
state,
getters,
actions,
mutations
};