Skip to content

Commit ad62519

Browse files
Muhammad Faraz  MaqsoodFaraz32123
authored andcommitted
fix: publish btn doesn't show after component edit
When we edit & save the component, publish button doesn't show up until we refresh the page manualy or open this unit by opening previous unit and coming back to this unit again. In this commit, we are dispatching a storage event whenever we edit the component, it'll refresh the page & show the publish button as expected.
1 parent 76b5dd5 commit ad62519

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

src/course-unit/hooks.jsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,24 @@ export const useCourseUnit = ({ courseId, blockId }) => {
213213
}
214214
}, [isMoveModalOpen]);
215215

216+
useEffect(() => {
217+
const handlePageRefreshUsingStorage = (event) => {
218+
// ignoring tests for if block, because it triggers when someone
219+
// edits the component using editor which has a separate store
220+
/* istanbul ignore next */
221+
if (event.key === 'courseRefreshTriggerOnComponentEditSave') {
222+
dispatch(fetchCourseSectionVerticalData(blockId, sequenceId));
223+
dispatch(fetchCourseVerticalChildrenData(blockId, isSplitTestType));
224+
localStorage.removeItem(event.key);
225+
}
226+
};
227+
228+
window.addEventListener('storage', handlePageRefreshUsingStorage);
229+
return () => {
230+
window.removeEventListener('storage', handlePageRefreshUsingStorage);
231+
};
232+
}, [blockId, sequenceId, isSplitTestType]);
233+
216234
return {
217235
sequenceId,
218236
courseUnit,

src/editors/data/redux/thunkActions/app.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,16 @@ export const saveBlock = (content, returnToUnit) => (dispatch) => {
125125
content,
126126
onSuccess: (response) => {
127127
dispatch(actions.app.setSaveResponse(response));
128+
const parsedData = JSON.parse(response.config.data);
129+
if (parsedData?.has_changes) {
130+
const storageKey = 'courseRefreshTriggerOnComponentEditSave';
131+
localStorage.setItem(storageKey, Date.now());
132+
133+
window.dispatchEvent(new StorageEvent('storage', {
134+
key: storageKey,
135+
newValue: Date.now().toString(),
136+
}));
137+
}
128138
returnToUnit(response.data);
129139
},
130140
}));

src/editors/data/redux/thunkActions/app.test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,11 @@ describe('app thunkActions', () => {
352352
});
353353
it('dispatches actions.app.setSaveResponse with response and then calls returnToUnit', () => {
354354
dispatch.mockClear();
355-
const response = 'testRESPONSE';
355+
const mockParsedData = { has_changes: true };
356+
const response = {
357+
config: { data: JSON.stringify(mockParsedData) },
358+
data: {},
359+
};
356360
calls[1][0].saveBlock.onSuccess(response);
357361
expect(dispatch).toHaveBeenCalledWith(actions.app.setSaveResponse(response));
358362
expect(returnToUnit).toHaveBeenCalled();

0 commit comments

Comments
 (0)