From 880c5e8bf44c06e887d3a23c91d5c2408cb4f79c Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Wed, 10 Jun 2026 12:46:10 +0530 Subject: [PATCH] refactor: configure cache api clients via constructor options --- .../dashboard/api/CacheEnabledApiClient.js | 19 +++++++++--------- app/javascript/dashboard/api/agents.js | 17 +--------------- app/javascript/dashboard/api/attributes.js | 20 ++++--------------- .../dashboard/api/cannedResponse.js | 20 ++++--------------- app/javascript/dashboard/api/inboxes.js | 11 +++++----- app/javascript/dashboard/api/labels.js | 11 +++++----- app/javascript/dashboard/api/teams.js | 18 +---------------- 7 files changed, 30 insertions(+), 86 deletions(-) diff --git a/app/javascript/dashboard/api/CacheEnabledApiClient.js b/app/javascript/dashboard/api/CacheEnabledApiClient.js index ed4e6a3be..e00af7f82 100644 --- a/app/javascript/dashboard/api/CacheEnabledApiClient.js +++ b/app/javascript/dashboard/api/CacheEnabledApiClient.js @@ -5,14 +5,15 @@ import ApiClient from './ApiClient'; class CacheEnabledApiClient extends ApiClient { constructor(resource, options = {}) { super(resource, options); + // `cacheModel` is the Rails Model.name.underscore value — simultaneously + // the server cache-key name and the IDB object-store name. + this.cacheModelName = options.cacheModel; + // inbox/label endpoints wrap collections in { payload }; the rest return + // the bare array. + this.payloadEnvelope = options.payloadEnvelope || false; this.dataManager = new DataManager(this.accountIdFromRoute); } - // eslint-disable-next-line class-methods-use-this - get cacheModelName() { - throw new Error('cacheModelName is not defined'); - } - get(cache = false) { if (cache) { return this.getFromCache(); @@ -25,14 +26,14 @@ class CacheEnabledApiClient extends ApiClient { return axios.get(this.url); } - // eslint-disable-next-line class-methods-use-this extractDataFromResponse(response) { - return response.data.payload; + return this.payloadEnvelope ? response.data.payload : response.data; } - // eslint-disable-next-line class-methods-use-this marshallData(dataToParse) { - return { data: { payload: dataToParse } }; + return this.payloadEnvelope + ? { data: { payload: dataToParse } } + : { data: dataToParse }; } async getFromCache() { diff --git a/app/javascript/dashboard/api/agents.js b/app/javascript/dashboard/api/agents.js index 5688364d8..2f3b98903 100644 --- a/app/javascript/dashboard/api/agents.js +++ b/app/javascript/dashboard/api/agents.js @@ -4,22 +4,7 @@ import CacheEnabledApiClient from './CacheEnabledApiClient'; class Agents extends CacheEnabledApiClient { constructor() { - super('agents', { accountScoped: true }); - } - - // eslint-disable-next-line class-methods-use-this - get cacheModelName() { - return 'account_user'; - } - - // eslint-disable-next-line class-methods-use-this - extractDataFromResponse(response) { - return response.data; - } - - // eslint-disable-next-line class-methods-use-this - marshallData(dataToParse) { - return { data: dataToParse }; + super('agents', { accountScoped: true, cacheModel: 'account_user' }); } bulkInvite({ emails }) { diff --git a/app/javascript/dashboard/api/attributes.js b/app/javascript/dashboard/api/attributes.js index 91d288991..2abf144d5 100644 --- a/app/javascript/dashboard/api/attributes.js +++ b/app/javascript/dashboard/api/attributes.js @@ -2,22 +2,10 @@ import CacheEnabledApiClient from './CacheEnabledApiClient'; class AttributeAPI extends CacheEnabledApiClient { constructor() { - super('custom_attribute_definitions', { accountScoped: true }); - } - - // eslint-disable-next-line class-methods-use-this - get cacheModelName() { - return 'custom_attribute_definition'; - } - - // eslint-disable-next-line class-methods-use-this - extractDataFromResponse(response) { - return response.data; - } - - // eslint-disable-next-line class-methods-use-this - marshallData(dataToParse) { - return { data: dataToParse }; + super('custom_attribute_definitions', { + accountScoped: true, + cacheModel: 'custom_attribute_definition', + }); } getAttributesByModel() { diff --git a/app/javascript/dashboard/api/cannedResponse.js b/app/javascript/dashboard/api/cannedResponse.js index 11ffb6a70..3a946dc9f 100644 --- a/app/javascript/dashboard/api/cannedResponse.js +++ b/app/javascript/dashboard/api/cannedResponse.js @@ -4,22 +4,10 @@ import CacheEnabledApiClient from './CacheEnabledApiClient'; class CannedResponse extends CacheEnabledApiClient { constructor() { - super('canned_responses', { accountScoped: true }); - } - - // eslint-disable-next-line class-methods-use-this - get cacheModelName() { - return 'canned_response'; - } - - // eslint-disable-next-line class-methods-use-this - extractDataFromResponse(response) { - return response.data; - } - - // eslint-disable-next-line class-methods-use-this - marshallData(dataToParse) { - return { data: dataToParse }; + super('canned_responses', { + accountScoped: true, + cacheModel: 'canned_response', + }); } get({ searchKey } = {}) { diff --git a/app/javascript/dashboard/api/inboxes.js b/app/javascript/dashboard/api/inboxes.js index 114dbb6f4..9af8fc765 100644 --- a/app/javascript/dashboard/api/inboxes.js +++ b/app/javascript/dashboard/api/inboxes.js @@ -3,12 +3,11 @@ import CacheEnabledApiClient from './CacheEnabledApiClient'; class Inboxes extends CacheEnabledApiClient { constructor() { - super('inboxes', { accountScoped: true }); - } - - // eslint-disable-next-line class-methods-use-this - get cacheModelName() { - return 'inbox'; + super('inboxes', { + accountScoped: true, + cacheModel: 'inbox', + payloadEnvelope: true, + }); } getCampaigns(inboxId) { diff --git a/app/javascript/dashboard/api/labels.js b/app/javascript/dashboard/api/labels.js index 2b521b058..a3278342a 100644 --- a/app/javascript/dashboard/api/labels.js +++ b/app/javascript/dashboard/api/labels.js @@ -2,12 +2,11 @@ import CacheEnabledApiClient from './CacheEnabledApiClient'; class LabelsAPI extends CacheEnabledApiClient { constructor() { - super('labels', { accountScoped: true }); - } - - // eslint-disable-next-line class-methods-use-this - get cacheModelName() { - return 'label'; + super('labels', { + accountScoped: true, + cacheModel: 'label', + payloadEnvelope: true, + }); } } diff --git a/app/javascript/dashboard/api/teams.js b/app/javascript/dashboard/api/teams.js index 5413af96b..c795355d3 100644 --- a/app/javascript/dashboard/api/teams.js +++ b/app/javascript/dashboard/api/teams.js @@ -1,25 +1,9 @@ /* global axios */ -// import ApiClient from './ApiClient'; import CacheEnabledApiClient from './CacheEnabledApiClient'; export class TeamsAPI extends CacheEnabledApiClient { constructor() { - super('teams', { accountScoped: true }); - } - - // eslint-disable-next-line class-methods-use-this - get cacheModelName() { - return 'team'; - } - - // eslint-disable-next-line class-methods-use-this - extractDataFromResponse(response) { - return response.data; - } - - // eslint-disable-next-line class-methods-use-this - marshallData(dataToParse) { - return { data: dataToParse }; + super('teams', { accountScoped: true, cacheModel: 'team' }); } getAgents({ teamId }) {