Skip to content

Commit

Permalink
Don't use event bus for sending comments
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Nov 4, 2020
1 parent 68531bb commit 9abcbdf
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 37 deletions.
23 changes: 5 additions & 18 deletions src/components/SendComment.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@
</template>

<script>
import { EventBus } from '../utils/eventBus';
import { createDeepLinkUrl, handleUnknownError } from '../utils';
import { client } from '../utils/aeternity';
import { handleUnknownError } from '../utils';
import MessageInput from './MessageInput.vue';
import backendAuthMixin from '../utils/backendAuthMixin';
export default {
components: { MessageInput },
mixins: [backendAuthMixin(true)],
props: {
tipId: { type: String, required: true },
parentId: { type: [Number, String], default: undefined },
Expand All @@ -40,21 +36,12 @@ export default {
methods: {
async sendTipComment() {
if (!this.allowSubmit) return;
if (!this.$store.state.useSdkWallet) {
window.location = createDeepLinkUrl({
type: 'comment', id: this.tipId, text: this.comment, parentId: this.parentId,
});
return;
}
this.setLoading = true;
try {
await this.backendAuth('sendTipComment', {
tipId: this.tipId,
text: this.comment,
author: client.rpcClient.getCurrentAccount(),
parentId: this.parentId,
});
EventBus.$emit('reloadData');
await this.$store.dispatch(
'backend/sendComment',
{ text: this.comment, tipId: this.tipId, parentId: this.parentId },
);
this.comment = '';
} catch (e) {
if (e.message !== 'Operation rejected by user') handleUnknownError(e);
Expand Down
39 changes: 39 additions & 0 deletions src/store/modules/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Vue from 'vue';
import { times } from 'lodash-es';
// eslint-disable-next-line import/no-cycle
import Backend from '../../utils/backend';
import { createDeepLinkUrl } from '../../utils';

export default {
namespaced: true,
Expand Down Expand Up @@ -111,5 +112,43 @@ export default {
async reloadPrices({ commit }) {
commit('setPrices', (await Backend.getPrice())?.aeternity);
},
async callWithAuth({ rootState: { useSdkWallet, sdk, address } }, { method, arg, to }) {
const { challenge } = await Backend[method](address, arg);
if (useSdkWallet) {
const signature = await sdk.signMessage(challenge);
await Backend[method](address, { challenge, signature });
return;
}

const url = new URL(to || window.location, window.location);
url.search = '';
window.location = createDeepLinkUrl({
type: 'sign-message',
message: challenge,
'x-success': `${url}?method=${method}&address=${address}&challenge=${challenge}&signature={signature}`,
});
},
async sendComment(
{ dispatch, rootState: { address, useSdkWallet } },
{ tipId, text, parentId },
) {
if (!useSdkWallet) {
window.location = createDeepLinkUrl({
type: 'comment', id: tipId, text, parentId,
});
return;
}
await dispatch('callWithAuth', {
method: 'sendTipComment',
arg: {
tipId, text, author: address, parentId,
},
});
await Promise.all([
dispatch('reloadTip', tipId),
...parentId ? [dispatch('reloadComment', parentId)] : [],
dispatch('reloadStats'),
]);
},
},
};
19 changes: 3 additions & 16 deletions src/utils/backendAuthMixin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { mapState } from 'vuex';
import Backend from './backend';
import { createDeepLinkUrl } from './index';
import { client } from './aeternity';

export default (ignoreCallInQuery) => ({
computed: mapState({
Expand Down Expand Up @@ -31,20 +29,9 @@ export default (ignoreCallInQuery) => ({
},
methods: {
async backendAuth(method, arg, to) {
const { challenge } = await Backend[method](this.address, arg);
if (this.useSdkWallet) {
const signature = await client.signMessage(challenge);
await Backend[method](this.address, { challenge, signature });
return;
}

const url = new URL(to ? this.$router.resolve(to).href : window.location, window.location);
url.search = '';
window.location = createDeepLinkUrl({
type: 'sign-message',
message: challenge,
'x-success': `${url}?method=${method}&address=${this.address}&challenge=${challenge}&signature={signature}`,
});
await this.$store.dispatch(
'backend/callWithAuth', { method, arg, to: to && this.$router.resolve(to).href },
);
},
},
});
3 changes: 0 additions & 3 deletions src/views/TipsAndComments.vue
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ import TipRecord from '../components/tipRecords/TipRecord.vue';
import TipCommentList from '../components/tipRecords/TipCommentList.vue';
import BackButtonRibbon from '../components/BackButtonRibbon.vue';
import Loading from '../components/Loading.vue';
import { EventBus } from '../utils/eventBus';
import backendAuthMixin from '../utils/backendAuthMixin';
import SendComment from '../components/SendComment.vue';
Expand Down Expand Up @@ -88,11 +87,9 @@ export default {
async mounted() {
const handler = () => this.reloadData();
this.$watch(({ id }) => id, handler, { immediate: true });
EventBus.$on('reloadData', handler);
const interval = setInterval(handler, 120 * 1000);
this.$once('hook:destroyed', () => {
EventBus.$off('reloadData', handler);
clearInterval(interval);
});
},
Expand Down

1 comment on commit 9abcbdf

@mradkov
Copy link
Contributor

@mradkov mradkov commented on 9abcbdf Nov 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.