Files
chatwoot/app/javascript/widget/store/modules/articles.js
Muhsin Keloth 41d6f9a200 chore: Add cache to improve widget performance (#11163)
- Add dynamic importing for routes.
- Added caching for `campaign`, `articles` and `inbox_members` API end
points.

---------

Co-authored-by: Pranav <pranavrajs@gmail.com>
2025-03-24 16:04:49 -07:00

66 lines
1.4 KiB
JavaScript

import { getMostReadArticles } from 'widget/api/article';
import { getFromCache, setCache } from 'shared/helpers/cache';
const CACHE_KEY_PREFIX = 'chatwoot_most_read_articles_';
const state = {
records: [],
uiFlags: {
isError: false,
hasFetched: false,
isFetching: false,
},
};
export const getters = {
uiFlags: $state => $state.uiFlags,
popularArticles: $state => $state.records,
};
export const actions = {
fetch: async ({ commit }, { slug, locale }) => {
commit('setIsFetching', true);
commit('setError', false);
try {
const cachedData = getFromCache(`${CACHE_KEY_PREFIX}${slug}_${locale}`);
if (cachedData) {
commit('setArticles', cachedData);
return;
}
const { data } = await getMostReadArticles(slug, locale);
const { payload = [] } = data;
setCache(`${CACHE_KEY_PREFIX}${slug}_${locale}`, payload);
if (payload.length) {
commit('setArticles', payload);
}
} catch (error) {
commit('setError', true);
} finally {
commit('setIsFetching', false);
}
},
};
export const mutations = {
setArticles($state, data) {
$state.records = data;
},
setError($state, value) {
$state.uiFlags.isError = value;
},
setIsFetching($state, value) {
$state.uiFlags.isFetching = value;
},
};
export default {
namespaced: true,
state,
getters,
actions,
mutations,
};