Skip to content

Commit

Permalink
await based on id
Browse files Browse the repository at this point in the history
  • Loading branch information
thepiwo committed Apr 2, 2021
1 parent 02afbc9 commit c8cb88a
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 33 deletions.
2 changes: 0 additions & 2 deletions src/components/TipInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
<script>
import { mapState, mapGetters } from 'vuex';
import IconTip from '../assets/iconTip.svg?icon-component';
import Backend from '../utils/backend';
import { EventBus } from '../utils/eventBus';
import { createDeepLinkUrl, shiftDecimalPlaces } from '../utils';
import AeInputAmount from './AeInputAmount.vue';
Expand Down Expand Up @@ -203,7 +202,6 @@ export default {
}
if (!this.userAddress) {
await Backend.awaitTips();
EventBus.$emit('reloadData');
}
this.hideModal();
Expand Down
41 changes: 19 additions & 22 deletions src/components/layout/sendTip/SendTip.vue
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ import { mapState, mapGetters } from 'vuex';
import AeInputAmount from '../../AeInputAmount.vue';
import { createDeepLinkUrl, shiftDecimalPlaces, validateTipUrl } from '../../../utils';
import { EventBus } from '../../../utils/eventBus';
import Backend from '../../../utils/backend';
import AeButton from '../../AeButton.vue';
import IconDiamond from '../../../assets/iconDiamond.svg?icon-component';
import IconClose from '../../../assets/iconClose.svg?icon-component';
Expand Down Expand Up @@ -125,28 +124,26 @@ export default {
title: this.sendTipForm.title,
amount,
tokenAddress: this.inputToken,
})
.then(async () => {
await Backend.awaitTips().catch(console.error);
this.clearTipForm();
this.$store.dispatch('modals/open', {
name: 'success',
title: this.$t('components.layout.SendTip.SuccessHeader'),
body: this.$t('components.layout.SendTip.SuccessText'),
});
setTimeout(() => EventBus.$emit('reloadData'), 5000);
}).catch((e) => {
this.sendingTip = false;
if (e.code && e.code === 4) {
return;
}
console.error(e);
this.$store.dispatch('modals/open', {
name: 'failure',
title: this.$t('components.layout.SendTip.ErrorHeader'),
body: this.$t('components.layout.SendTip.ErrorText'),
});
}).then(async () => {
this.clearTipForm();
this.$store.dispatch('modals/open', {
name: 'success',
title: this.$t('components.layout.SendTip.SuccessHeader'),
body: this.$t('components.layout.SendTip.SuccessText'),
});
EventBus.$emit('reloadData');
}).catch((e) => {
this.sendingTip = false;
if (e.code && e.code === 4) {
return;
}
console.error(e);
this.$store.dispatch('modals/open', {
name: 'failure',
title: this.$t('components.layout.SendTip.ErrorHeader'),
body: this.$t('components.layout.SendTip.ErrorText'),
});
});
},
clearTipForm() {
this.sendTipForm = { amount: 0, url: '', title: '' };
Expand Down
3 changes: 2 additions & 1 deletion src/store/getters.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { shiftDecimalPlaces } from '../utils';

export default {
roundedTokenAmount: ({ tokenInfo }) => (amount, token, round = 2) => {
const shiftedAmount = shiftDecimalPlaces(amount, -(tokenInfo[token] ? tokenInfo[token].decimals : 18));
const shiftedAmount = shiftDecimalPlaces(amount,
-(tokenInfo[token] ? tokenInfo[token].decimals : 18));
return new BigNumber(shiftedAmount).toFixed(round);
},
isLoggedIn: (state) => !!state.address,
Expand Down
28 changes: 21 additions & 7 deletions src/store/modules/aeternity.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,12 +330,19 @@ export default {

if (tokenAddress && tokenAddress !== 'native') {
await dispatch('createOrChangeAllowance', { contractAddress: tokenAddress, amount });
return contractV2.methods.tip_token(url, title, tokenAddress, amount);

const { decodedResult } = await contractV2.methods
.tip_token(url, title, tokenAddress, amount);
return dispatch('backend/awaitTip', `${decodedResult}_v2`, { root: true });
}

if (contractV2) {
const { decodedResult } = await contractV2.methods.tip(url, title, { amount });
return dispatch('backend/awaitTip', `${decodedResult}_v2`, { root: true });
}

return contractV2
? contractV2.methods.tip(url, title, { amount })
: contractV1.methods.tip(url, title, { amount });
await contractV1.methods.tip(url, title, { amount });
return dispatch('backend/awaitTip', null, { root: true });
},
async retip({
dispatch,
Expand All @@ -353,15 +360,22 @@ export default {

if (tokenAddress && tokenAddress !== 'native') {
await dispatch('createOrChangeAllowance', { contractAddress: tokenAddress, amount });
return contractV2.methods.retip_token(Number(id.split('_')[0]), tokenAddress, amount);

const [tipId, contractVersion] = id.split('_');
const { decodedResult } = await contractV2.methods
.retip_token(Number(tipId), tokenAddress, amount);
return dispatch('backend/awaitRetip', `${decodedResult}_${contractVersion}`, { root: true });
}

if (contractAddress === process.env.VUE_APP_CONTRACT_V1_ADDRESS) {
return contractV1.methods.retip(Number(id.split('_')[0]), { amount });
await contractV1.methods.retip(Number(id.split('_')[0]), { amount });
return dispatch('backend/awaitRetip', null, { root: true });
}

if (contractAddress === process.env.VUE_APP_CONTRACT_V2_ADDRESS) {
return contractV2.methods.retip(Number(id.split('_')[0]), { amount });
const [tipId, contractVersion] = id.split('_');
const { decodedResult } = await contractV2.methods.retip(Number(tipId), { amount });
return dispatch('backend/awaitRetip', `${decodedResult}_${contractVersion}`, { root: true });
}

return null;
Expand Down
6 changes: 6 additions & 0 deletions src/store/modules/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ export default {
async reloadComment({ commit }, id) {
commit('setComment', { id, value: await Backend.getCommentById(id) });
},
async awaitTip(_, id) {
await Backend.awaitTip(id);
},
async awaitRetip(_, id) {
await Backend.awaitRetip(id);
},
async reloadStats({ commit, rootState: { aeternity: { sdk } } }) {
const [stats1, stats2, height] = await Promise.all([
Backend.getTipStats(),
Expand Down
4 changes: 3 additions & 1 deletion src/utils/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ export default class Backend {

static getTokenBalances = async (address) => backendFetch(`tokenCache/balances?address=${address}`);

static awaitTips = async () => backendFetch('tips/await');
static awaitTip = async (id) => backendFetch(`tips/await/tip?id=${id}`);

static awaitRetip = async (id) => backendFetch(`tips/await/retip?id=${id}`);

static invalidateTokenCache = async (token) => backendFetch(`cache/invalidate/token/${token}`);

Expand Down

0 comments on commit c8cb88a

Please sign in to comment.