Skip to content

Commit 06497bf

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 7e0b7f9 commit 06497bf

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
@@ -217,6 +217,24 @@ export const useCourseUnit = ({ courseId, blockId }) => {
217217
}
218218
}, [isMoveModalOpen]);
219219

220+
useEffect(() => {
221+
const handlePageRefreshUsingStorage = (event) => {
222+
// ignoring tests for if block, because it triggers when someone
223+
// edits the component using editor which has a separate store
224+
/* istanbul ignore next */
225+
if (event.key === 'courseRefreshTriggerOnComponentEditSave') {
226+
dispatch(fetchCourseSectionVerticalData(blockId, sequenceId));
227+
dispatch(fetchCourseVerticalChildrenData(blockId, isSplitTestType));
228+
localStorage.removeItem(event.key);
229+
}
230+
};
231+
232+
window.addEventListener('storage', handlePageRefreshUsingStorage);
233+
return () => {
234+
window.removeEventListener('storage', handlePageRefreshUsingStorage);
235+
};
236+
}, [blockId, sequenceId, isSplitTestType]);
237+
220238
return {
221239
sequenceId,
222240
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)