# Pull Request Template ## Description ### Description This PR fixes an issue where the editor would reset content and move the cursor while typing. The issue was caused by a dual debounce setup (400ms + 2500ms) that saved content and then overwrote local state with stale API responses while the user was still typing. ### What changed * Editor now uses local state (`localTitle`, `localContent`) as the source of truth while editing * Vuex store is only used on initial load or navigation * Replaced dual debounce with a single 500ms debounce (fewer API calls) * `UPDATE_ARTICLE` now merges updates instead of replacing the article * Prevents status changes from wiping unsaved content * Removed `updateAsync` for a simpler update flow ### How it works User types → local ref updates immediately (editor reads from this) → 500ms debounce triggers → dispatches `articles/update` → API persists the change → on success: store merges the response (used by other components) → editor remains unaffected (continues using local state) Fixes https://linear.app/chatwoot/issue/CW-6727/better-syncing-of-content-the-editor-randomly-updates-the-content ## Type of change - [x] Bug fix (non-breaking change which fixes an issue) ## How Has This Been Tested? 1. Open any Help Center article for editing 2. Type continuously for a few seconds — content should not reset or jump 3. Change article status (publish/archive/draft) while editing — content should remain intact 4. Test on a slow network (use DevTools throttling) — typing should remain smooth ## Checklist: - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my code - [x] I have commented on my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules --------- Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
170 lines
4.9 KiB
JavaScript
170 lines
4.9 KiB
JavaScript
import articlesAPI from 'dashboard/api/helpCenter/articles';
|
|
import { uploadExternalImage, uploadFile } from 'dashboard/helper/uploadHelper';
|
|
import { throwErrorMessage } from 'dashboard/store/utils/api';
|
|
import camelcaseKeys from 'camelcase-keys';
|
|
|
|
import types from '../../mutation-types';
|
|
export const actions = {
|
|
index: async (
|
|
{ commit },
|
|
{ pageNumber, portalSlug, locale, status, authorId, categorySlug }
|
|
) => {
|
|
try {
|
|
commit(types.SET_UI_FLAG, { isFetching: true });
|
|
const { data } = await articlesAPI.getArticles({
|
|
pageNumber,
|
|
portalSlug,
|
|
locale,
|
|
status,
|
|
authorId,
|
|
categorySlug,
|
|
});
|
|
const payload = camelcaseKeys(data.payload);
|
|
const meta = camelcaseKeys(data.meta);
|
|
const articleIds = payload.map(article => article.id);
|
|
commit(types.CLEAR_ARTICLES);
|
|
commit(types.ADD_MANY_ARTICLES, payload);
|
|
commit(types.SET_ARTICLES_META, meta);
|
|
commit(types.ADD_MANY_ARTICLES_ID, articleIds);
|
|
return articleIds;
|
|
} catch (error) {
|
|
return throwErrorMessage(error);
|
|
} finally {
|
|
commit(types.SET_UI_FLAG, { isFetching: false });
|
|
}
|
|
},
|
|
|
|
create: async ({ commit, dispatch }, { portalSlug, ...articleObj }) => {
|
|
commit(types.SET_UI_FLAG, { isCreating: true });
|
|
try {
|
|
const { data } = await articlesAPI.createArticle({
|
|
portalSlug,
|
|
articleObj,
|
|
});
|
|
const payload = camelcaseKeys(data.payload);
|
|
const { id: articleId } = payload;
|
|
commit(types.ADD_ARTICLE, payload);
|
|
commit(types.ADD_ARTICLE_ID, articleId);
|
|
commit(types.ADD_ARTICLE_FLAG, articleId);
|
|
dispatch('portals/updatePortal', portalSlug, { root: true });
|
|
return articleId;
|
|
} catch (error) {
|
|
return throwErrorMessage(error);
|
|
} finally {
|
|
commit(types.SET_UI_FLAG, { isCreating: false });
|
|
}
|
|
},
|
|
|
|
show: async ({ commit }, { id, portalSlug }) => {
|
|
commit(types.SET_UI_FLAG, { isFetching: true });
|
|
try {
|
|
const { data } = await articlesAPI.getArticle({ id, portalSlug });
|
|
const payload = camelcaseKeys(data.payload);
|
|
const { id: articleId } = payload;
|
|
commit(types.ADD_ARTICLE, payload);
|
|
commit(types.ADD_ARTICLE_ID, articleId);
|
|
commit(types.SET_UI_FLAG, { isFetching: false });
|
|
} catch (error) {
|
|
commit(types.SET_UI_FLAG, { isFetching: false });
|
|
}
|
|
},
|
|
|
|
update: async ({ commit }, { portalSlug, articleId, ...articleObj }) => {
|
|
commit(types.UPDATE_ARTICLE_FLAG, {
|
|
uiFlags: { isUpdating: true },
|
|
articleId,
|
|
});
|
|
|
|
try {
|
|
const { data } = await articlesAPI.updateArticle({
|
|
portalSlug,
|
|
articleId,
|
|
articleObj,
|
|
});
|
|
const payload = camelcaseKeys(data.payload);
|
|
commit(types.UPDATE_ARTICLE, payload);
|
|
|
|
return articleId;
|
|
} catch (error) {
|
|
return throwErrorMessage(error);
|
|
} finally {
|
|
commit(types.UPDATE_ARTICLE_FLAG, {
|
|
uiFlags: { isUpdating: false },
|
|
articleId,
|
|
});
|
|
}
|
|
},
|
|
|
|
updateArticleMeta: async ({ commit }, { portalSlug, locale }) => {
|
|
try {
|
|
const { data } = await articlesAPI.getArticles({
|
|
pageNumber: 1,
|
|
portalSlug,
|
|
locale,
|
|
});
|
|
const meta = camelcaseKeys(data.meta);
|
|
const { currentPage, ...metaWithoutCurrentPage } = meta;
|
|
commit(types.SET_ARTICLES_META, metaWithoutCurrentPage);
|
|
} catch (error) {
|
|
throwErrorMessage(error);
|
|
}
|
|
},
|
|
|
|
delete: async ({ commit }, { portalSlug, articleId }) => {
|
|
commit(types.UPDATE_ARTICLE_FLAG, {
|
|
uiFlags: {
|
|
isDeleting: true,
|
|
},
|
|
articleId,
|
|
});
|
|
try {
|
|
await articlesAPI.deleteArticle({ portalSlug, articleId });
|
|
commit(types.REMOVE_ARTICLE, articleId);
|
|
commit(types.REMOVE_ARTICLE_ID, articleId);
|
|
return articleId;
|
|
} catch (error) {
|
|
return throwErrorMessage(error);
|
|
} finally {
|
|
commit(types.UPDATE_ARTICLE_FLAG, {
|
|
uiFlags: {
|
|
isDeleting: false,
|
|
},
|
|
articleId,
|
|
});
|
|
}
|
|
},
|
|
|
|
attachImage: async (_, { file }) => {
|
|
const { fileUrl } = await uploadFile(file);
|
|
return fileUrl;
|
|
},
|
|
|
|
uploadExternalImage: async (_, { url }) => {
|
|
const { fileUrl } = await uploadExternalImage(url);
|
|
return fileUrl;
|
|
},
|
|
|
|
reorder: async (
|
|
{ commit, state },
|
|
{ portalSlug, categorySlug, reorderedGroup }
|
|
) => {
|
|
// Save old positions so we can rollback on failure
|
|
const oldPositions = Object.keys(reorderedGroup).reduce((map, id) => {
|
|
map[id] = state.articles.byId[id]?.position;
|
|
return map;
|
|
}, {});
|
|
// Update positions in the store immediately so subsequent mutations preserve correct positions
|
|
commit(types.SET_ARTICLE_POSITIONS, reorderedGroup);
|
|
try {
|
|
await articlesAPI.reorderArticles({
|
|
portalSlug,
|
|
reorderedGroup,
|
|
categorySlug,
|
|
});
|
|
} catch (error) {
|
|
commit(types.SET_ARTICLE_POSITIONS, oldPositions);
|
|
throw error;
|
|
}
|
|
},
|
|
};
|