Skip to content

Commit 35d4673

Browse files
Muhammad Faraz  MaqsoodMuhammad Faraz  Maqsood
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 aeefcc6 commit 35d4673

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

src/course-unit/hooks.jsx

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

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