Move src to dashboard (#152)
This commit is contained in:
29
app/javascript/dashboard/helper/APIHelper.js
Normal file
29
app/javascript/dashboard/helper/APIHelper.js
Normal file
@@ -0,0 +1,29 @@
|
||||
/* eslint no-console: 0 */
|
||||
import constants from '../constants';
|
||||
import Auth from '../api/auth';
|
||||
import router from '../routes';
|
||||
|
||||
const parseErrorCode = error => {
|
||||
const errorStatus = error.response ? error.response.status : undefined;
|
||||
// 901, 902 are used to identify billing related issues
|
||||
if ([901, 902].includes(errorStatus)) {
|
||||
const name = Auth.isAdmin() ? 'billing' : 'billing_deactivated';
|
||||
router.push({ name });
|
||||
}
|
||||
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;
|
||||
};
|
||||
6
app/javascript/dashboard/helper/URLHelper.js
Normal file
6
app/javascript/dashboard/helper/URLHelper.js
Normal file
@@ -0,0 +1,6 @@
|
||||
import queryString from 'query-string';
|
||||
|
||||
export const frontendURL = (path, params) => {
|
||||
const stringifiedParams = params ? `?${queryString.stringify(params)}` : '';
|
||||
return `/app/${path}${stringifiedParams}`;
|
||||
};
|
||||
11
app/javascript/dashboard/helper/commons.js
Normal file
11
app/javascript/dashboard/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];
|
||||
},
|
||||
});
|
||||
}
|
||||
};
|
||||
71
app/javascript/dashboard/helper/pusher.js
Normal file
71
app/javascript/dashboard/helper/pusher.js
Normal file
@@ -0,0 +1,71 @@
|
||||
/* eslint-env browser */
|
||||
/* eslint no-console: 0 */
|
||||
import Pusher from 'pusher-js';
|
||||
import AuthAPI from '../api/auth';
|
||||
import CONSTANTS from '../constants';
|
||||
|
||||
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 => {
|
||||
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 => {
|
||||
const { meta = {}, id } = payload;
|
||||
const { assignee } = meta || {};
|
||||
if (id) {
|
||||
this.app.$store.dispatch('updateAssignee', { id, assignee });
|
||||
}
|
||||
});
|
||||
|
||||
channel.bind('user:logout', () => AuthAPI.logout());
|
||||
channel.bind('page:reload', () => window.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 || true;
|
||||
// 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/dashboard/helper/states.js
Normal file
30
app/javascript/dashboard/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