|
| 1 | +const mapCopilot = obj => { |
| 2 | + obj._data = _.clone(obj) |
| 3 | + |
| 4 | + obj.theTime = obj.time * 60 - (Date.now() / 1000 - obj.timestamp) |
| 5 | + obj.time = obj.time + 'mins' |
| 6 | + |
| 7 | + if (obj.time_elapsed) { |
| 8 | + obj.date = 'Time elapsed' |
| 9 | + } else { |
| 10 | + obj.date = Quasar.date.formatDate( |
| 11 | + new Date((obj.theTime - 3600) * 1000), |
| 12 | + 'HH:mm:ss' |
| 13 | + ) |
| 14 | + } |
| 15 | + obj.displayComposeUrl = ['/copilot/cp/', obj.id].join('') |
| 16 | + obj.displayPanelUrl = ['/copilot/', obj.id].join('') |
| 17 | + return obj |
| 18 | +} |
| 19 | + |
| 20 | +window.app = Vue.createApp({ |
| 21 | + el: '#vue', |
| 22 | + mixins: [windowMixin], |
| 23 | + data() { |
| 24 | + return { |
| 25 | + filter: '', |
| 26 | + copilotLinks: [], |
| 27 | + copilotLinksObj: [], |
| 28 | + copilotsTable: { |
| 29 | + columns: [ |
| 30 | + { |
| 31 | + name: 'theId', |
| 32 | + align: 'left', |
| 33 | + label: 'id', |
| 34 | + field: 'id' |
| 35 | + }, |
| 36 | + { |
| 37 | + name: 'lnurl_toggle', |
| 38 | + align: 'left', |
| 39 | + label: 'Show lnurl pay link', |
| 40 | + field: 'lnurl_toggle' |
| 41 | + }, |
| 42 | + { |
| 43 | + name: 'title', |
| 44 | + align: 'left', |
| 45 | + label: 'title', |
| 46 | + field: 'title' |
| 47 | + }, |
| 48 | + { |
| 49 | + name: 'amount_made', |
| 50 | + align: 'left', |
| 51 | + label: 'amount made', |
| 52 | + field: 'amount_made' |
| 53 | + } |
| 54 | + ], |
| 55 | + pagination: { |
| 56 | + rowsPerPage: 10 |
| 57 | + } |
| 58 | + }, |
| 59 | + passedCopilot: {}, |
| 60 | + formDialog: { |
| 61 | + show: false, |
| 62 | + data: {} |
| 63 | + }, |
| 64 | + formDialogCopilot: { |
| 65 | + show: false, |
| 66 | + data: { |
| 67 | + lnurl_toggle: false, |
| 68 | + show_message: false, |
| 69 | + show_ack: false, |
| 70 | + show_price: 'None', |
| 71 | + title: '' |
| 72 | + } |
| 73 | + }, |
| 74 | + qrCodeDialog: { |
| 75 | + show: false, |
| 76 | + data: null |
| 77 | + }, |
| 78 | + options: ['bitcoin', 'confetti', 'rocket', 'face', 'martijn', 'rick'], |
| 79 | + currencyOptions: ['None', 'btcusd', 'btceur', 'btcgbp'] |
| 80 | + } |
| 81 | + }, |
| 82 | + methods: { |
| 83 | + cancelCopilot(data) { |
| 84 | + this.formDialogCopilot.show = false |
| 85 | + this.clearFormDialogCopilot() |
| 86 | + }, |
| 87 | + closeFormDialog() { |
| 88 | + this.clearFormDialogCopilot() |
| 89 | + this.formDialog.data = { |
| 90 | + lnurl_toggle: false, |
| 91 | + show_message: false, |
| 92 | + show_ack: false, |
| 93 | + show_price: 'None', |
| 94 | + title: '' |
| 95 | + } |
| 96 | + }, |
| 97 | + sendFormDataCopilot() { |
| 98 | + if (this.formDialogCopilot.data.id) { |
| 99 | + this.updateCopilot( |
| 100 | + this.g.user.wallets[0].adminkey, |
| 101 | + this.formDialogCopilot.data |
| 102 | + ) |
| 103 | + } else { |
| 104 | + this.createCopilot( |
| 105 | + this.g.user.wallets[0].adminkey, |
| 106 | + this.formDialogCopilot.data |
| 107 | + ) |
| 108 | + } |
| 109 | + }, |
| 110 | + |
| 111 | + createCopilot(wallet, data) { |
| 112 | + const updatedData = {} |
| 113 | + for (const property in data) { |
| 114 | + if (data[property]) { |
| 115 | + updatedData[property] = data[property] |
| 116 | + } |
| 117 | + if (property == 'animation1threshold' && data[property]) { |
| 118 | + updatedData[property] = parseInt(data[property]) |
| 119 | + } |
| 120 | + if (property == 'animation2threshold' && data[property]) { |
| 121 | + updatedData[property] = parseInt(data[property]) |
| 122 | + } |
| 123 | + if (property == 'animation3threshold' && data[property]) { |
| 124 | + updatedData[property] = parseInt(data[property]) |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + LNbits.api |
| 129 | + .request('POST', '/copilot/api/v1/copilot', wallet, updatedData) |
| 130 | + .then(response => { |
| 131 | + this.copilotLinks.push(mapCopilot(response.data)) |
| 132 | + this.formDialogCopilot.show = false |
| 133 | + this.clearFormDialogCopilot() |
| 134 | + }) |
| 135 | + .catch(LNbits.utils.notifyApiError) |
| 136 | + }, |
| 137 | + getCopilots() { |
| 138 | + LNbits.api |
| 139 | + .request('GET', '/copilot/api/v1/copilot', this.g.user.wallets[0].inkey) |
| 140 | + .then(response => { |
| 141 | + if (response.data) { |
| 142 | + this.copilotLinks = response.data.map(mapCopilot) |
| 143 | + } |
| 144 | + }) |
| 145 | + .catch(LNbits.utils.notifyApiError) |
| 146 | + }, |
| 147 | + getCopilot(copilot_id) { |
| 148 | + LNbits.api |
| 149 | + .request( |
| 150 | + 'GET', |
| 151 | + '/copilot/api/v1/copilot/' + copilot_id, |
| 152 | + this.g.user.wallets[0].inkey |
| 153 | + ) |
| 154 | + .then(response => { |
| 155 | + localStorage.setItem('copilot', JSON.stringify(response.data)) |
| 156 | + localStorage.setItem('inkey', this.g.user.wallets[0].inkey) |
| 157 | + }) |
| 158 | + .catch(LNbits.utils.notifyApiError) |
| 159 | + }, |
| 160 | + openCopilotCompose(copilot_id) { |
| 161 | + this.getCopilot(copilot_id) |
| 162 | + let params = |
| 163 | + 'scrollbars=no, resizable=no,status=no,location=no,toolbar=no,menubar=no,width=1200,height=644,left=410,top=100' |
| 164 | + open('../copilot/cp/', '_blank', params) |
| 165 | + }, |
| 166 | + openCopilotPanel(copilot_id) { |
| 167 | + this.getCopilot(copilot_id) |
| 168 | + let params = |
| 169 | + 'scrollbars=no, resizable=no,status=no,location=no,toolbar=no,menubar=no,width=300,height=450,left=10,top=400' |
| 170 | + open('../copilot/pn/', '_blank', params) |
| 171 | + }, |
| 172 | + deleteCopilotLink(copilotId) { |
| 173 | + const link = _.findWhere(this.copilotLinks, {id: copilotId}) |
| 174 | + LNbits.utils |
| 175 | + .confirmDialog('Are you sure you want to delete this pay link?') |
| 176 | + .onOk(() => { |
| 177 | + LNbits.api |
| 178 | + .request( |
| 179 | + 'DELETE', |
| 180 | + '/copilot/api/v1/copilot/' + copilotId, |
| 181 | + this.g.user.wallets[0].adminkey |
| 182 | + ) |
| 183 | + .then(response => { |
| 184 | + this.copilotLinks = _.reject(this.copilotLinks, function (obj) { |
| 185 | + return obj.id === copilotId |
| 186 | + }) |
| 187 | + }) |
| 188 | + .catch(LNbits.utils.notifyApiError) |
| 189 | + }) |
| 190 | + }, |
| 191 | + openUpdateCopilotLink(copilotId) { |
| 192 | + const copilot = _.findWhere(this.copilotLinks, {id: copilotId}) |
| 193 | + this.formDialogCopilot.data = {...copilot._data} |
| 194 | + this.formDialogCopilot.data.lnurl_toggle = Boolean( |
| 195 | + this.formDialogCopilot.data.lnurl_toggle |
| 196 | + ) |
| 197 | + this.formDialogCopilot.data.show_message = Boolean( |
| 198 | + this.formDialogCopilot.data.show_message |
| 199 | + ) |
| 200 | + this.formDialogCopilot.data.show_ack = Boolean( |
| 201 | + this.formDialogCopilot.data.show_ack |
| 202 | + ) |
| 203 | + this.formDialogCopilot.show = true |
| 204 | + }, |
| 205 | + updateCopilot(wallet, data) { |
| 206 | + const updatedData = {} |
| 207 | + |
| 208 | + const updateThreshold = property => { |
| 209 | + if (data[property] && data[property] !== 0) { |
| 210 | + updatedData[property] = parseInt(data[property]) |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + for (const property in data) { |
| 215 | + if (data[property]) { |
| 216 | + updatedData[property] = data[property] |
| 217 | + } |
| 218 | + switch (property) { |
| 219 | + case 'animation1threshold': |
| 220 | + case 'animation2threshold': |
| 221 | + case 'animation3threshold': |
| 222 | + updateThreshold(property) |
| 223 | + break |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + LNbits.api |
| 228 | + .request( |
| 229 | + 'PUT', |
| 230 | + '/copilot/api/v1/copilot/' + updatedData.id, |
| 231 | + wallet, |
| 232 | + updatedData |
| 233 | + ) |
| 234 | + .then(response => { |
| 235 | + this.copilotLinks = _.reject(this.copilotLinks, function (obj) { |
| 236 | + return obj.id === updatedData.id |
| 237 | + }) |
| 238 | + this.copilotLinks.push(mapCopilot(response.data)) |
| 239 | + this.formDialogCopilot.show = false |
| 240 | + this.clearFormDialogCopilot() |
| 241 | + }) |
| 242 | + .catch(LNbits.utils.notifyApiError) |
| 243 | + }, |
| 244 | + clearFormDialogCopilot() { |
| 245 | + this.formDialogCopilot.data = { |
| 246 | + lnurl_toggle: false, |
| 247 | + show_message: false, |
| 248 | + show_ack: false, |
| 249 | + show_price: 'None', |
| 250 | + title: '' |
| 251 | + } |
| 252 | + }, |
| 253 | + exportcopilotCSV() { |
| 254 | + LNbits.utils.exportCSV(this.copilotsTable.columns, this.copilotLinks) |
| 255 | + } |
| 256 | + }, |
| 257 | + created() { |
| 258 | + this.getCopilots() |
| 259 | + } |
| 260 | +}) |
0 commit comments