Skip to content

Commit

Permalink
refactor: some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
PKulkoRaccoonGang committed Oct 25, 2024
1 parent cfcb208 commit 89ba553
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 136 deletions.
11 changes: 6 additions & 5 deletions src/course-unit/context/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { useContext } from 'react';

import { IframeContext, IframeContextType } from './iFrameContext';

// eslint-disable-next-line import/prefer-default-export
export const useIframe = (): IframeContextType => {
const context = useContext(IframeContext);
if (!context) {
throw new Error('useIframe must be used within an IframeProvider');
}
return context;
const context = useContext(IframeContext);
if (!context) {
throw new Error('useIframe must be used within an IframeProvider');
}
return context;
};
3 changes: 3 additions & 0 deletions src/course-unit/context/iFrameContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ export const IframeProvider: React.FC = ({ children }) => {
try {
iframeWindow.postMessage({ type: messageType, payload }, '*');
} catch (error) {
// eslint-disable-next-line no-console
console.error('Failed to send message to iframe:', error);
}
} else {
// eslint-disable-next-line no-console
console.warn('Iframe is not accessible or loaded yet.');
}
};

return (
// eslint-disable-next-line react/jsx-no-constructed-context-values
<IframeContext.Provider value={{ setIframeRef, sendMessageToIframe }}>
{children}
</IframeContext.Provider>
Expand Down
2 changes: 1 addition & 1 deletion src/course-unit/move-modal/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const CATEGORIES_KEYS = {
component: 'component',
split_test: 'split_test',
group: 'group',
}
};

export const CATEGORY_RELATION_MAP = {
course: 'section',
Expand Down
21 changes: 1 addition & 20 deletions src/course-unit/move-modal/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
useCallback, useEffect, useState, useMemo,
} from 'react';
import { useDispatch, useSelector } from 'react-redux';
// eslint-disable-next-line import/no-extraneous-dependencies
import { IntlShape } from 'react-intl';
import { useParams } from 'react-router-dom';
import { useMediaQuery } from 'react-responsive';
Expand Down Expand Up @@ -216,26 +217,6 @@ export const useMoveModal = ({
setDisplayedXBlocksCategories, enableMoveOperation,
]);

console.log(
state.sourceXBlockInfo,
{
isLoading: loadingStatus === RequestStatus.IN_PROGRESS,
isValidMove: state.isValidMove,
isExtraSmall,
parentInfo: state.parentInfo,
childrenInfo: state.childrenInfo,
displayName: state.sourceXBlockInfo.current.displayName,
sourceXBlockId: state.sourceXBlockInfo.current.id,
categoryText: getCategoryText(),
breadcrumbs,
currentXBlockParentIds,
handleXBlockClick,
handleBreadcrumbsClick,
handleCLoseModal,
handleMoveXBlock,
}
);

return {
isLoading: loadingStatus === RequestStatus.IN_PROGRESS,
isValidMove: state.isValidMove,
Expand Down
2 changes: 1 addition & 1 deletion src/course-unit/move-modal/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
border-radius: 0;
width: 100%;
gap: map-get($spacers, 2);
padding: 0.5625rem $spacer 0.5625rem map-get($spacers, 4);
padding: .5625rem $spacer .5625rem map-get($spacers, 4);
}

.btn {
Expand Down
134 changes: 68 additions & 66 deletions src/course-unit/move-modal/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,95 +1,97 @@
// eslint-disable-next-line import/export
export interface IXBlockInfo {
id: string;
displayName: string;
child_info?: {
children?: IXBlockInfo[];
};
category?: string;
has_children?: boolean;
id: string;
displayName: string;
child_info?: {
children?: IXBlockInfo[];
};
category?: string;
has_children?: boolean;
}

export interface IUseMoveModalParams {
isOpenModal: boolean;
closeModal: () => void;
openModal: () => void;
courseId: string;
isOpenModal: boolean;
closeModal: () => void;
openModal: () => void;
courseId: string;
}

export interface IUseMoveModalReturn {
isLoading: boolean;
isValidMove: boolean;
isExtraSmall: boolean;
parentInfo: {
parent: IXBlockInfo;
category: string;
};
childrenInfo: {
children: IXBlockInfo[];
category: string;
};
displayName: string;
sourceXBlockId: string;
categoryText: string;
breadcrumbs: string[];
currentXBlockParentIds: string[];
handleXBlockClick: (newParentIndex: string|number) => void;
handleBreadcrumbsClick: (newParentIndex: string|number) => void;
handleCLoseModal: () => void;
handleMoveXBlock: () => void;
isLoading: boolean;
isValidMove: boolean;
isExtraSmall: boolean;
parentInfo: {
parent: IXBlockInfo;
category: string;
};
childrenInfo: {
children: IXBlockInfo[];
category: string;
};
displayName: string;
sourceXBlockId: string;
categoryText: string;
breadcrumbs: string[];
currentXBlockParentIds: string[];
handleXBlockClick: (newParentIndex: string | number) => void;
handleBreadcrumbsClick: (newParentIndex: string | number) => void;
handleCLoseModal: () => void;
handleMoveXBlock: () => void;
}

export interface IState {
sourceXBlockInfo: {
current: IXBlockInfo;
parent: IXBlockInfo;
};
childrenInfo: {
children: IXBlockInfo[];
category: string;
};
parentInfo: {
parent: IXBlockInfo;
category: string;
};
visitedAncestors: IXBlockInfo[];
isValidMove: boolean;
sourceXBlockInfo: {
current: IXBlockInfo;
parent: IXBlockInfo;
};
childrenInfo: {
children: IXBlockInfo[];
category: string;
};
parentInfo: {
parent: IXBlockInfo;
category: string;
};
visitedAncestors: IXBlockInfo[];
isValidMove: boolean;
}

export interface ITreeNode {
id: string;
child_info?: {
children?: ITreeNode[];
};
id: string;
child_info?: {
children?: ITreeNode[];
};
}

// eslint-disable-next-line import/export
export interface IXBlockInfo {
category?: string;
hasChildren?: boolean;
has_children?: boolean;
category?: string;
hasChildren?: boolean;
has_children?: boolean;
}

export interface IAncestor {
category?: string;
display_name?: string;
category?: string;
display_name?: string;
}

export interface IMoveModalProps {
isOpenModal: boolean,
closeModal: () => void,
openModal: () => void,
courseId: string,
isOpenModal: boolean,
closeModal: () => void,
openModal: () => void,
courseId: string,
}

export interface IXBlockChildInfo {
category?: string;
display_name?: string;
children?: IXBlock[];
category?: string;
display_name?: string;
children?: IXBlock[];
}

export interface IXBlock {
id: string;
display_name: string;
category: string;
has_children: boolean;
child_info?: IXBlockChildInfo;
id: string;
display_name: string;
category: string;
has_children: boolean;
child_info?: IXBlockChildInfo;
}
2 changes: 1 addition & 1 deletion src/course-unit/move-modal/moveModal.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {render, screen, within} from '@testing-library/react';
import { render, within } from '@testing-library/react';
import { AppProvider } from '@edx/frontend-platform/react';
import { IntlProvider } from '@edx/frontend-platform/i18n';
import { initializeMockApp } from '@edx/frontend-platform';
Expand Down
4 changes: 4 additions & 0 deletions src/course-unit/move-modal/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { IntlShape } from 'react-intl';

import { BASIC_BLOCK_TYPES, CATEGORIES_KEYS } from './constants';
Expand Down Expand Up @@ -37,6 +38,7 @@ export const findParentIds = (
): string[] => {
let path: string[] = [];

// eslint-disable-next-line @typescript-eslint/no-shadow
function traverse(node: ITreeNode | undefined, targetId: string, currentPath: string[]): boolean {
if (!node) {
return false;
Expand Down Expand Up @@ -73,7 +75,9 @@ export const isValidCategory = (
sourceParentInfo: IXBlockInfo,
targetParentInfo: IXBlockInfo,
): boolean => {
// eslint-disable-next-line prefer-const
let { category: sourceParentCategory, hasChildren: sourceParentHasChildren } = sourceParentInfo;
// eslint-disable-next-line prefer-const
let { category: targetParentCategory, has_children: targetParentHasChildren } = targetParentInfo;

if (
Expand Down
5 changes: 3 additions & 2 deletions src/course-unit/xblock-container-iframe/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ interface XBlockContainerIframeProps {
blockId: string;
unitXBlockActions: {
handleDelete: (XBlockId: string) => void;
handleDuplicate: (XBlockId: string) => void;
};
xblocks: Array<{
name: string;
Expand Down Expand Up @@ -74,7 +75,7 @@ const XBlockContainerIframe: FC<XBlockContainerIframeProps> = ({
const [isConfigureModalOpen, openConfigureModal, closeConfigureModal] = useToggle(false);
const { setIframeRef, sendMessageToIframe } = useIframe();
const [editXblockId, setEditXblockId] = useState<string | null>(null);
const [currentXblockData, setCurrentXblockData] = useState<Record<string, any>>({});
const [currentXblockData, setCurrentXblockData] = useState<any>({});

const iframeUrl = `${getConfig().STUDIO_BASE_URL}/container_embed/${blockId}`;

Expand Down Expand Up @@ -144,7 +145,6 @@ const XBlockContainerIframe: FC<XBlockContainerIframeProps> = ({

const handleMessage = (event: MessageEvent) => {
const { type, payload } = event.data || {};
console.log('MESSAGE FROM IFRAME =================>', { type, payload });
if (type && messageHandlers[type]) {
messageHandlers[type](payload);
}
Expand Down Expand Up @@ -199,6 +199,7 @@ const XBlockContainerIframe: FC<XBlockContainerIframeProps> = ({
onClose={closeConfigureModal}
onConfigureSubmit={onConfigureSubmit}
currentItemData={currentXblockData}
isSelfPaced={false}
/>
<iframe
ref={iframeRef}
Expand Down
Loading

0 comments on commit 89ba553

Please sign in to comment.