|
| 1 | +import Component from '@glimmer/component'; |
| 2 | +import { action } from '@ember/object'; |
| 3 | +import { tracked } from '@glimmer/tracking'; |
| 4 | +import { inject as service } from '@ember/service'; |
| 5 | +import { toastNotificationTimeoutOptions } from '../../constants/toast-notification'; |
| 6 | +import { APPS } from '../../constants/urls'; |
| 7 | +const BASE_URL = APPS.API_BACKEND; |
| 8 | + |
| 9 | +export default class Step1Component extends Component { |
| 10 | + @tracked isChaincodeGenerated = false; |
| 11 | + @tracked isGeneratingChaincode = false; |
| 12 | + @tracked chaincode = 'asxjdDZVNTfuDMQJiunJ'; |
| 13 | + @tracked isChaincodeVisible = false; |
| 14 | + @service toast; |
| 15 | + |
| 16 | + @action handleNext() { |
| 17 | + if ( |
| 18 | + confirm( |
| 19 | + 'Make sure you copied the chaincode as you are not able to see it again. If not, click `Cancel` and copy it.', |
| 20 | + ) |
| 21 | + ) { |
| 22 | + this.args.setState('step2'); |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + @action handleEyeClick() { |
| 27 | + this.isChaincodeVisible = !this.isChaincodeVisible; |
| 28 | + } |
| 29 | + |
| 30 | + @action handleCopy() { |
| 31 | + navigator.clipboard.writeText(this.chaincode); |
| 32 | + this.toast.info('Chaincode Copied!!', '', toastNotificationTimeoutOptions); |
| 33 | + } |
| 34 | + |
| 35 | + @action async handleGenerateChaincode(e) { |
| 36 | + e.preventDefault(); |
| 37 | + if (this.isGeneratingChaincode === false) { |
| 38 | + this.isGeneratingChaincode = true; |
| 39 | + try { |
| 40 | + const response = await fetch(`${BASE_URL}/users/chaincode`, { |
| 41 | + method: 'GET', |
| 42 | + headers: { |
| 43 | + 'Content-Type': 'application/json', |
| 44 | + }, |
| 45 | + credentials: 'include', |
| 46 | + }); |
| 47 | + |
| 48 | + const { chaincode } = await response.json(); |
| 49 | + |
| 50 | + if (response.ok) { |
| 51 | + this.chaincode = chaincode; |
| 52 | + this.isChaincodeGenerated = true; |
| 53 | + this.toast.info( |
| 54 | + 'Generated New Chaincode!!', |
| 55 | + '', |
| 56 | + toastNotificationTimeoutOptions, |
| 57 | + ); |
| 58 | + } else { |
| 59 | + console.log(response); |
| 60 | + this.toast.error( |
| 61 | + 'Something went wrong. Please check console errors.', |
| 62 | + '', |
| 63 | + toastNotificationTimeoutOptions, |
| 64 | + ); |
| 65 | + } |
| 66 | + } catch (error) { |
| 67 | + console.log('error', error); |
| 68 | + this.toast.error( |
| 69 | + 'Something went wrong. Please check console errors.', |
| 70 | + '', |
| 71 | + toastNotificationTimeoutOptions, |
| 72 | + ); |
| 73 | + } finally { |
| 74 | + this.isGeneratingChaincode = false; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments