Skip to content

Commit

Permalink
refactor: removed canEdit mock (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
PKulkoRaccoonGang authored and monteri committed May 3, 2024
1 parent 7e1e651 commit 4766395
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 39 deletions.
3 changes: 1 addition & 2 deletions src/course-unit/course-xblock/CourseXBlock.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import SortableItem from '../../generic/drag-helper/SortableItem';
import { scrollToElement } from '../../course-outline/utils';
import { COURSE_BLOCK_NAMES } from '../../constants';
import {
getCanEdit,
getCourseId,
getXBlockIFrameHtmlAndResources,
} from '../data/selectors';
Expand Down Expand Up @@ -162,7 +161,7 @@ const CourseXBlock = ({
{intl.formatMessage(messages.blockLabelButtonMove)}
</Dropdown.Item>
)}
{canEdit && canCopy && (
{canCopy && (
<Dropdown.Item onClick={() => dispatch(copyToClipboard(id))}>
{intl.formatMessage(messages.blockLabelButtonCopyToClipboard)}
</Dropdown.Item>
Expand Down
1 change: 0 additions & 1 deletion src/course-unit/data/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { createSelector } from '@reduxjs/toolkit';
import { RequestStatus } from 'CourseAuthoring/data/constants';

export const getCourseUnitData = (state) => state.courseUnit.unit;
export const getCanEdit = (state) => state.courseUnit.canEdit;
export const getStaticFileNotices = (state) => state.courseUnit.staticFileNotices;
export const getCourseUnit = (state) => state.courseUnit;
export const getSavingStatus = (state) => state.courseUnit.savingStatus;
Expand Down
1 change: 0 additions & 1 deletion src/course-unit/data/slice.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const slice = createSlice({
errorMessage: '',
isQueryPending: false,
isTitleEditFormOpen: false,
canEdit: true,
loadingStatus: {
fetchUnitLoadingStatus: RequestStatus.IN_PROGRESS,
courseSectionVerticalLoadingStatus: RequestStatus.IN_PROGRESS,
Expand Down
4 changes: 1 addition & 3 deletions src/course-unit/hooks.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
getErrorMessage,
getSequenceStatus,
getStaticFileNotices,
getCanEdit,
} from './data/selectors';
import { changeEditTitleFormOpen, updateQueryPendingStatus } from './data/slice';
import { PUBLISH_TYPES } from './constants';
Expand All @@ -45,9 +44,8 @@ export const useCourseUnit = ({ courseId, blockId }) => {
const staticFileNotices = useSelector(getStaticFileNotices);
const navigate = useNavigate();
const isTitleEditFormOpen = useSelector(state => state.courseUnit.isTitleEditFormOpen);
const canEdit = useSelector(getCanEdit);
const { currentlyVisibleToStudents } = courseUnit;
const { sharedClipboardData, showPasteXBlock, showPasteUnit } = useCopyToClipboard(canEdit);
const { sharedClipboardData, showPasteXBlock, showPasteUnit } = useCopyToClipboard();
const { canPasteComponent } = courseVerticalChildren;

const unitTitle = courseUnit.metadata?.displayName || '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Button } from '@openedx/paragon';
import { useIntl } from '@edx/frontend-platform/i18n';

import { Divider } from '../../../../generic/divider';
import { getCanEdit, getCourseUnitData } from '../../../data/selectors';
import { getCourseUnitData } from '../../../data/selectors';
import { copyToClipboard } from '../../../../generic/data/thunks';
import messages from '../../messages';

Expand All @@ -17,7 +17,6 @@ const ActionButtons = ({ openDiscardModal, handlePublishing }) => {
hasChanges,
enableCopyPasteUnits,
} = useSelector(getCourseUnitData);
const canEdit = useSelector(getCanEdit);

return (
<>
Expand All @@ -36,7 +35,7 @@ const ActionButtons = ({ openDiscardModal, handlePublishing }) => {
{intl.formatMessage(messages.actionButtonDiscardChangesTitle)}
</Button>
)}
{enableCopyPasteUnits && canEdit && (
{enableCopyPasteUnits && (
<>
<Divider className="course-unit-sidebar-footer__divider" />
<Button
Expand Down
18 changes: 6 additions & 12 deletions src/generic/clipboard/hooks/useCopyToClipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import { getClipboardData } from '../../data/selectors';
/**
* Custom React hook for managing clipboard functionality.
*
* @param {boolean} canEdit - Flag indicating whether the clipboard is editable.
* @returns {Object} - An object containing state variables and functions related to clipboard functionality.
* @property {boolean} showPasteUnit - Flag indicating whether the "Paste Unit" button should be visible.
* @property {boolean} showPasteXBlock - Flag indicating whether the "Paste XBlock" button should be visible.
* @property {Object} sharedClipboardData - The shared clipboard data object.
*/
const useCopyToClipboard = (canEdit = true) => {
const useCopyToClipboard = () => {
const [clipboardBroadcastChannel] = useState(() => new BroadcastChannel(STUDIO_CLIPBOARD_CHANNEL));
const [showPasteUnit, setShowPasteUnit] = useState(false);
const [showPasteXBlock, setShowPasteXBlock] = useState(false);
Expand All @@ -22,7 +21,7 @@ const useCopyToClipboard = (canEdit = true) => {

// Function to refresh the paste button's visibility
const refreshPasteButton = (data) => {
const isPasteable = canEdit && data?.content && data.content.status !== CLIPBOARD_STATUS.expired;
const isPasteable = data?.content && data.content.status !== CLIPBOARD_STATUS.expired;
const isPasteableXBlock = isPasteable && !STRUCTURAL_XBLOCK_TYPES.includes(data.content.blockType);
const isPasteableUnit = isPasteable && data.content.blockType === 'vertical';

Expand All @@ -32,15 +31,10 @@ const useCopyToClipboard = (canEdit = true) => {

useEffect(() => {
// Handle updates to clipboard data
if (canEdit) {
refreshPasteButton(clipboardData);
setSharedClipboardData(clipboardData);
clipboardBroadcastChannel.postMessage(clipboardData);
} else {
setShowPasteXBlock(false);
setShowPasteUnit(false);
}
}, [clipboardData, canEdit, clipboardBroadcastChannel]);
refreshPasteButton(clipboardData);
setSharedClipboardData(clipboardData);
clipboardBroadcastChannel.postMessage(clipboardData);
}, [clipboardData, clipboardBroadcastChannel]);

useEffect(() => {
// Handle messages from the broadcast channel
Expand Down
17 changes: 0 additions & 17 deletions src/generic/clipboard/hooks/useCopyToClipboard.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,6 @@ describe('useCopyToClipboard', () => {
});

describe('clipboard data update effect', () => {
it('returns falsy flags if canEdit = false', async () => {
const { result } = renderHook(() => useCopyToClipboard(false), { wrapper });

axiosMock
.onPost(getClipboardUrl())
.reply(200, clipboardUnit);
axiosMock
.onGet(getClipboardUrl())
.reply(200, clipboardUnit);

await act(async () => {
await executeThunk(copyToClipboard(unitId), store.dispatch);
});
expect(result.current.showPasteUnit).toBe(false);
expect(result.current.showPasteXBlock).toBe(false);
});

it('returns flag to display the Paste Unit button', async () => {
const { result } = renderHook(() => useCopyToClipboard(true), { wrapper });

Expand Down

0 comments on commit 4766395

Please sign in to comment.