Skip to content

Commit bfa7426

Browse files
committed
feat: refresh iframe on message
1 parent b23c37a commit bfa7426

4 files changed

Lines changed: 13 additions & 0 deletions

File tree

src/course-unit/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export const getXBlockSupportMessages = (intl) => ({
4141

4242
export const messageTypes = {
4343
refreshXBlock: 'refreshXBlock',
44+
refreshIframe: 'refreshIframe',
4445
showMoveXBlockModal: 'showMoveXBlockModal',
4546
completeXBlockMoving: 'completeXBlockMoving',
4647
rollbackMovedXBlock: 'rollbackMovedXBlock',

src/course-unit/xblock-container-iframe/hooks/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export type UseMessageHandlersTypes = {
1515
handleOpenManageTagsModal: (id: string) => void;
1616
handleShowProcessingNotification: (variant: string) => void;
1717
handleHideProcessingNotification: () => void;
18+
handleRefreshIframe: () => void;
1819
};
1920

2021
export type MessageHandlersTypes = Record<string, (payload: any) => void>;

src/course-unit/xblock-container-iframe/hooks/useMessageHandlers.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const useMessageHandlers = ({
3131
handleShowProcessingNotification,
3232
handleHideProcessingNotification,
3333
handleEditXBlock,
34+
handleRefreshIframe,
3435
}: UseMessageHandlersTypes): MessageHandlersTypes => {
3536
const { copyToClipboard } = useClipboard();
3637

@@ -50,6 +51,7 @@ export const useMessageHandlers = ({
5051
[messageTypes.saveEditedXBlockData]: handleSaveEditedXBlockData,
5152
[messageTypes.studioAjaxError]: ({ error }) => handleResponseErrors(error, dispatch, updateSavingStatus),
5253
[messageTypes.refreshPositions]: handleFinishXBlockDragging,
54+
[messageTypes.refreshIframe]: handleRefreshIframe,
5355
[messageTypes.openManageTags]: (payload) => handleOpenManageTagsModal(payload.contentId),
5456
[messageTypes.addNewComponent]: () => handleShowProcessingNotification(NOTIFICATION_MESSAGES.adding),
5557
[messageTypes.pasteNewComponent]: () => handleShowProcessingNotification(NOTIFICATION_MESSAGES.pasting),

src/course-unit/xblock-container-iframe/index.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ const XBlockContainerIframe: FC<XBlockContainerIframeProps> = ({
4646
const intl = useIntl();
4747
const dispatch = useDispatch();
4848

49+
// Useful to reload iframe
50+
const [iframeKey, setIframeKey] = useState(0);
4951
const [isDeleteModalOpen, openDeleteModal, closeDeleteModal] = useToggle(false);
5052
const [isUnlinkModalOpen, openUnlinkModal, closeUnlinkModal] = useToggle(false);
5153
const [isConfigureModalOpen, openConfigureModal, closeConfigureModal] = useToggle(false);
@@ -182,6 +184,11 @@ const XBlockContainerIframe: FC<XBlockContainerIframeProps> = ({
182184
dispatch(hideProcessingNotification());
183185
};
184186

187+
const handleRefreshIframe = () => {
188+
// Updating iframeKey forces the iframe to re-render.
189+
setIframeKey((prev) => prev + 1);
190+
};
191+
185192
const messageHandlers = useMessageHandlers({
186193
courseId,
187194
dispatch,
@@ -199,6 +206,7 @@ const XBlockContainerIframe: FC<XBlockContainerIframeProps> = ({
199206
handleShowProcessingNotification,
200207
handleHideProcessingNotification,
201208
handleEditXBlock,
209+
handleRefreshIframe,
202210
});
203211

204212
useIframeMessages(messageHandlers);
@@ -268,6 +276,7 @@ const XBlockContainerIframe: FC<XBlockContainerIframeProps> = ({
268276
/>
269277
) : null}
270278
<iframe
279+
key={iframeKey}
271280
ref={iframeRef}
272281
title={intl.formatMessage(messages.xblockIframeTitle)}
273282
name="xblock-iframe"

0 commit comments

Comments
 (0)