Initial Commit
Co-authored-by: Subin <subinthattaparambil@gmail.com> Co-authored-by: Manoj <manojmj92@gmail.com> Co-authored-by: Nithin <webofnithin@gmail.com>
This commit is contained in:
45
app/javascript/src/helper/APIHelper.js
Normal file
45
app/javascript/src/helper/APIHelper.js
Normal file
@@ -0,0 +1,45 @@
|
||||
/* eslint no-console: 0 */
|
||||
import constants from '../constants';
|
||||
import Auth from '../api/auth';
|
||||
import router from '../routes';
|
||||
|
||||
const parseErrorCode = (error) => {
|
||||
if (error.response) {
|
||||
if (error.response.status === 401) {
|
||||
// If auth failed
|
||||
|
||||
} else if (error.response.status === 500) {
|
||||
// If server failed
|
||||
|
||||
} else if (error.response.status === 422) {
|
||||
// If request params are errored
|
||||
|
||||
} else if (error.response.status === 901 || error.response.status === 902) {
|
||||
let name = 'billing_deactivated';
|
||||
if (Auth.isAdmin()) {
|
||||
name = 'billing';
|
||||
}
|
||||
// If Trial ended
|
||||
router.push({ name });
|
||||
} else {
|
||||
// Anything else
|
||||
}
|
||||
} else {
|
||||
// Something happened in setting up the request that triggered an Error
|
||||
|
||||
}
|
||||
// Do something with request error
|
||||
return Promise.reject(error);
|
||||
};
|
||||
|
||||
export default (axios) => {
|
||||
const wootApi = axios.create();
|
||||
wootApi.defaults.baseURL = constants.apiURL;
|
||||
// Add Auth Headers to requests if logged in
|
||||
if (Auth.isLoggedIn()) {
|
||||
Object.assign(wootApi.defaults.headers.common, Auth.getAuthData());
|
||||
}
|
||||
// Response parsing interceptor
|
||||
wootApi.interceptors.response.use(response => response, error => parseErrorCode(error));
|
||||
return wootApi;
|
||||
};
|
||||
11
app/javascript/src/helper/commons.js
Normal file
11
app/javascript/src/helper/commons.js
Normal file
@@ -0,0 +1,11 @@
|
||||
/* eslint no-console: 0 */
|
||||
/* eslint no-param-reassign: 0 */
|
||||
export default () => {
|
||||
if (!Array.prototype.last) {
|
||||
Object.assign(Array.prototype, {
|
||||
last() {
|
||||
return this[this.length - 1];
|
||||
},
|
||||
});
|
||||
}
|
||||
};
|
||||
85
app/javascript/src/helper/pusher.js
Normal file
85
app/javascript/src/helper/pusher.js
Normal file
@@ -0,0 +1,85 @@
|
||||
/* eslint-env browser */
|
||||
/* eslint no-console: 0 */
|
||||
/* global location */
|
||||
import Pusher from 'pusher-js';
|
||||
import AuthAPI from '../api/auth';
|
||||
import CONSTANTS from '../constants';
|
||||
|
||||
const ding = require('../assets/audio/ding.mp3');
|
||||
|
||||
class VuePusher {
|
||||
|
||||
constructor(apiKey, options) {
|
||||
this.app = options.app;
|
||||
this.pusher = new Pusher(apiKey, options);
|
||||
this.channels = [];
|
||||
}
|
||||
|
||||
subscribe(channelName) {
|
||||
const channel = this.pusher.subscribe(channelName);
|
||||
if (!this.channels.includes(channel)) {
|
||||
this.channels.push(channelName);
|
||||
}
|
||||
this.bindEvent(channel);
|
||||
}
|
||||
|
||||
unsubscribe(channelName) {
|
||||
this.pusher.unsubscribe(channelName);
|
||||
}
|
||||
|
||||
bindEvent(channel) {
|
||||
channel.bind('message.created', (data) => {
|
||||
// Play sound if incoming
|
||||
if (!data.message_type) {
|
||||
new Audio(ding).play();
|
||||
}
|
||||
this.app.$store.dispatch('addMessage', data);
|
||||
});
|
||||
|
||||
channel.bind('conversation.created', (data) => {
|
||||
this.app.$store.dispatch('addConversation', data);
|
||||
});
|
||||
|
||||
channel.bind('status_change:conversation', (data) => {
|
||||
this.app.$store.dispatch('addConversation', data);
|
||||
});
|
||||
|
||||
channel.bind('assignee.changed', (payload) => {
|
||||
if (!payload.meta) return;
|
||||
const { assignee } = payload.meta;
|
||||
const { id } = payload;
|
||||
if (id) {
|
||||
this.app.$store.dispatch('updateAssignee', {
|
||||
id,
|
||||
assignee,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
channel.bind('user:logout', () => {
|
||||
AuthAPI.logout();
|
||||
});
|
||||
|
||||
channel.bind('page:reload', () => {
|
||||
location.reload();
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* eslint no-param-reassign: ["error", { "props": false }]*/
|
||||
export default {
|
||||
init() {
|
||||
// Log only if env is testing or development.
|
||||
Pusher.logToConsole = CONSTANTS.PUSHER.logToConsole;
|
||||
// Init Pusher
|
||||
const options = { encrypted: true, app: window.WOOT, cluster: CONSTANTS.PUSHER.cluster };
|
||||
const pusher = new VuePusher(CONSTANTS.PUSHER.token, options);
|
||||
// Add to global Obj
|
||||
if (AuthAPI.isLoggedIn()) {
|
||||
pusher.subscribe(AuthAPI.getChannel());
|
||||
return pusher.pusher;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
};
|
||||
30
app/javascript/src/helper/states.js
Normal file
30
app/javascript/src/helper/states.js
Normal file
@@ -0,0 +1,30 @@
|
||||
/* eslint no-console: 0 */
|
||||
/* eslint no-param-reassign: 0 */
|
||||
export default (Vuex) => {
|
||||
const wootState = new Vuex.Store({
|
||||
state: {
|
||||
authenticated: false,
|
||||
currentChat: null,
|
||||
},
|
||||
mutations: {
|
||||
// Authentication mutations
|
||||
authenticate(state) {
|
||||
state.authenticated = true;
|
||||
},
|
||||
logout(state) {
|
||||
state.authenticated = false;
|
||||
},
|
||||
|
||||
// CurrentChat Mutations
|
||||
setCurrentChat(state, chat) {
|
||||
state.currentChat = chat;
|
||||
},
|
||||
},
|
||||
getters: {
|
||||
currentChat(state) {
|
||||
return state.currentChat;
|
||||
},
|
||||
},
|
||||
});
|
||||
return wootState;
|
||||
};
|
||||
Reference in New Issue
Block a user