Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: [AXIMST-739] removed canEdit mock #224

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 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 All @@ -46,7 +45,6 @@ const CourseXBlock = ({
const dispatch = useDispatch();
const navigate = useNavigate();
const courseId = useSelector(getCourseId);
const canEdit = useSelector(getCanEdit);
const intl = useIntl();
const xblockIFrameHtmlAndResources = useSelector(getXBlockIFrameHtmlAndResources);
const xblockInstanceHtmlAndResources = find(xblockIFrameHtmlAndResources, { xblockId: id });
Expand Down Expand Up @@ -154,7 +152,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 @@ -3,7 +3,6 @@ import { createSelector } from '@reduxjs/toolkit';
import { RequestStatus } from '../../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 getSavingStatus = (state) => state.courseUnit.savingStatus;
export const getErrorMessage = (state) => state.courseUnit.errorMessage;
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';

Expand All @@ -44,9 +43,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 && !NOT_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
Loading