From e0d291c49e31adcd915c680f9a0fbab9bd65e618 Mon Sep 17 00:00:00 2001 From: Pranav Raj S Date: Tue, 22 Oct 2019 13:28:45 +0530 Subject: [PATCH] Create util for apiLoadingStatus (#170) * Create util for apiLoadingStatus * Revert "Fix #52 rubocop metrics abc size in passwords controller (#119)" This reverts commit 9c22da0ac6cac771f279019daeef320981345cd2. --- app/controllers/passwords_controller.rb | 11 +++++------ .../dashboard/store/modules/AccountState.js | 9 +++------ .../dashboard/store/modules/cannedResponse.js | 9 +++------ app/javascript/dashboard/store/utils/api.js | 6 ++++++ .../dashboard/store/utils/specs/api.spec.js | 15 +++++++++++++++ 5 files changed, 32 insertions(+), 18 deletions(-) create mode 100644 app/javascript/dashboard/store/utils/api.js create mode 100644 app/javascript/dashboard/store/utils/specs/api.spec.js diff --git a/app/controllers/passwords_controller.rb b/app/controllers/passwords_controller.rb index d5636ebf6..84b2cbef4 100644 --- a/app/controllers/passwords_controller.rb +++ b/app/controllers/passwords_controller.rb @@ -31,12 +31,11 @@ class PasswordsController < Devise::PasswordsController def set_headers(user) data = user.create_new_auth_token - headers_names = response.headers[DeviseTokenAuth.headers_names] - headers_names[:'access-token'] = data['access-token'] - headers_names[:'token-type'] = 'Bearer' - headers_names[:'client'] = data['client'] - headers_names[:'expiry'] = data['expiry'] - headers_names[:'uid'] = data['uid'] + response.headers[DeviseTokenAuth.headers_names[:"access-token"]] = data['access-token'] + response.headers[DeviseTokenAuth.headers_names[:"token-type"]] = 'Bearer' + response.headers[DeviseTokenAuth.headers_names[:client]] = data['client'] + response.headers[DeviseTokenAuth.headers_names[:expiry]] = data['expiry'] + response.headers[DeviseTokenAuth.headers_names[:uid]] = data['uid'] end def reset_password_and_confirmation(recoverable) diff --git a/app/javascript/dashboard/store/modules/AccountState.js b/app/javascript/dashboard/store/modules/AccountState.js index 12c8a2693..2adb6a63f 100644 --- a/app/javascript/dashboard/store/modules/AccountState.js +++ b/app/javascript/dashboard/store/modules/AccountState.js @@ -3,6 +3,7 @@ /* eslint no-shadow: 0 */ import * as types from '../mutation-types'; import Account from '../../api/account'; +import { setLoadingStatus, getLoadingStatus } from '../utils/api'; const state = { agents: [], @@ -16,9 +17,7 @@ const getters = { getVerifiedAgents(_state) { return _state.agents.filter(element => element.confirmed); }, - getAgentFetchStatus(_state) { - return _state.fetchAPIloadingStatus; - }, + getAgentFetchStatus: getLoadingStatus, }; const actions = { @@ -73,9 +72,7 @@ const actions = { const mutations = { // List - [types.default.SET_AGENT_FETCHING_STATUS](_state, flag) { - _state.fetchAPIloadingStatus = flag; - }, + [types.default.SET_AGENT_FETCHING_STATUS]: setLoadingStatus, // List [types.default.SET_AGENTS](_state, response) { _state.agents = response.data; diff --git a/app/javascript/dashboard/store/modules/cannedResponse.js b/app/javascript/dashboard/store/modules/cannedResponse.js index 114347da4..14da75ef7 100644 --- a/app/javascript/dashboard/store/modules/cannedResponse.js +++ b/app/javascript/dashboard/store/modules/cannedResponse.js @@ -3,6 +3,7 @@ /* eslint no-shadow: 0 */ import * as types from '../mutation-types'; import CannedApi from '../../api/cannedResponse'; +import { setLoadingStatus, getLoadingStatus } from '../utils/api'; const state = { cannedResponse: [], @@ -13,9 +14,7 @@ const getters = { getCannedResponses(_state) { return _state.cannedResponse; }, - getCannedFetchStatus(_state) { - return _state.fetchAPIloadingStatus; - }, + getCannedFetchStatus: getLoadingStatus, }; const actions = { @@ -79,9 +78,7 @@ const actions = { const mutations = { // List - [types.default.SET_CANNED_FETCHING_STATUS](_state, flag) { - _state.fetchAPIloadingStatus = flag; - }, + [types.default.SET_CANNED_FETCHING_STATUS]: setLoadingStatus, // List [types.default.SET_CANNED](_state, response) { _state.cannedResponse = response.data; diff --git a/app/javascript/dashboard/store/utils/api.js b/app/javascript/dashboard/store/utils/api.js new file mode 100644 index 000000000..1b221994c --- /dev/null +++ b/app/javascript/dashboard/store/utils/api.js @@ -0,0 +1,6 @@ +/* eslint no-param-reassign: 0 */ + +export const getLoadingStatus = state => state.fetchAPIloadingStatus; +export const setLoadingStatus = (state, status) => { + state.fetchAPIloadingStatus = status; +}; diff --git a/app/javascript/dashboard/store/utils/specs/api.spec.js b/app/javascript/dashboard/store/utils/specs/api.spec.js new file mode 100644 index 000000000..50c82b7d9 --- /dev/null +++ b/app/javascript/dashboard/store/utils/specs/api.spec.js @@ -0,0 +1,15 @@ +import { getLoadingStatus, setLoadingStatus } from '../api'; + +describe('#getLoadingStatus', () => { + it('returns correct status', () => { + expect(getLoadingStatus({ fetchAPIloadingStatus: true })).toBe(true); + }); +}); + +describe('#setLoadingStatus', () => { + it('set correct status', () => { + const state = { fetchAPIloadingStatus: true }; + setLoadingStatus(state, false); + expect(state.fetchAPIloadingStatus).toBe(false); + }); +});