Connect wallet and fill in address during signup

In case there's a wallet available, ask the user to connect it and fill
in their account address in the form field.

Also, check the status code when submitting it to the back-end and show
an error message instead of the success/completion page in case the
contributor could not be created.
This commit is contained in:
Râu Cao
2023-01-21 12:39:14 +08:00
parent 1e7d8491f9
commit 0bdb4d2021
2 changed files with 55 additions and 45 deletions
+25 -21
View File
@@ -1,25 +1,27 @@
import Controller from '@ember/controller'; import Controller from '@ember/controller';
import { computed } from '@ember/object';
import { alias, not } from '@ember/object/computed';
import { isAddress } from 'web3-utils';
import { inject as service } from '@ember/service'; import { inject as service } from '@ember/service';
import { action } from '@ember/object';
import { alias } from '@ember/object/computed';
import { tracked } from '@glimmer/tracking';
import config from 'kredits-web/config/environment'; import config from 'kredits-web/config/environment';
import { isAddress } from 'web3-utils';
export default Controller.extend({ export default class BudgetController extends Controller {
@service kredits;
kredits: service(), @tracked accountAddress = null;
accountAddress: null, @alias('kredits.githubAccessToken') githubAccessToken;
githubAccessToken: alias('kredits.githubAccessToken'),
isValidEthAccount: computed('accountAddress', function() { get isValidEthAccount () {
return isAddress(this.accountAddress); return isAddress(this.accountAddress);
}), }
signupButtonDisabled: not('isValidEthAccount'), get signupButtonDisabled () {
return !this.isValidEthAccount;
actions: { }
@action
completeSignup () { completeSignup () {
const payload = { const payload = {
accessToken: this.githubAccessToken, accessToken: this.githubAccessToken,
@@ -33,17 +35,19 @@ export default Controller.extend({
}) })
.then(res => res.json()) .then(res => res.json())
.then(data => { .then(data => {
console.log('Created contributor:', data); if (data.error) {
alert('Creating profile failed. We have been notified about this error and will take a look soon. Sorry!');
console.warn('Creating contributor profile failed:',
JSON.parse(data.error.body).error.message)
return false;
} else {
console.log('[signup/account] Created contributor:', data);
this.setProperties({ this.githubAccessToken = null;
githubAccessToken: null, this.accountAddress = null;
accountAddress: null
});
this.transitionToRoute('signup.complete'); this.transitionToRoute('signup.complete');
});
} }
})
}
} }
});
+12 -6
View File
@@ -2,16 +2,22 @@ import Route from '@ember/routing/route';
import { inject as service } from '@ember/service'; import { inject as service } from '@ember/service';
import { isEmpty } from '@ember/utils'; import { isEmpty } from '@ember/utils';
export default Route.extend({ export default class SignupAccountRoute extends Route {
@service kredits;
kredits: service(), async setupController (controller) {
if (!window.ethereum) return;
if (this.kredits.hasAccounts) {
controller.accountAddress = this.kredits.currentUserAccounts.firstObject;
} else {
return this.kredits.connectWallet();
}
}
redirect () { redirect () {
this._super(...arguments);
if (isEmpty(this.kredits.githubAccessToken)) { if (isEmpty(this.kredits.githubAccessToken)) {
this.transitionTo('signup.index'); this.transitionTo('signup.index');
} }
} }
}
});