Skip to content
This repository has been archived by the owner on Jan 23, 2025. It is now read-only.

Commit

Permalink
feat: refactor move modal, use accessible with pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
pyphilia committed Jan 12, 2024
1 parent f014f13 commit 0fe8da0
Show file tree
Hide file tree
Showing 16 changed files with 639 additions and 456 deletions.
6 changes: 3 additions & 3 deletions cypress/support/commands/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import {
SHARE_ITEM_EMAIL_INPUT_ID,
SHARE_ITEM_SHARE_BUTTON_ID,
TREE_MODAL_CONFIRM_BUTTON_ID,
buildHomeModalItemID,
buildItemFormAppOptionId,
buildItemRowArrowId,
buildNavigationModalItemId,
buildPermissionOptionId,
buildTreeItemId,
} from '../../../src/config/selectors';
Expand Down Expand Up @@ -58,7 +58,7 @@ Cypress.Commands.add(
// click on the element
if (idx === array.length - 1) {
cy.wrap($tree)
.get(`#${buildHomeModalItemID(value)}`)
.get(`#${buildNavigationModalItemId(value)}`)
.first()
.click();
}
Expand All @@ -68,7 +68,7 @@ Cypress.Commands.add(
!$tree.find(`#${buildTreeItemId(array[idx + 1], treeRootId)}`).length
) {
cy.wrap($tree)
.get(`#${buildHomeModalItemID(value)}`)
.get(`#${buildNavigationModalItemId(value)}`)
.trigger('mouseover')
.get(`#${buildItemRowArrowId(value)}`)
.first()
Expand Down
69 changes: 54 additions & 15 deletions src/components/common/MoveButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@ import {
MoveButton as GraaspMoveButton,
} from '@graasp/ui';

import { validate } from 'uuid';

import { mutations } from '@/config/queryClient';
import { hooks, mutations } from '@/config/queryClient';
import { applyEllipsisOnLength, getDirectParentId } from '@/utils/item';

import { useBuilderTranslation } from '../../config/i18n';
import {
HOME_MODAL_ITEM_ID,
ITEM_MENU_MOVE_BUTTON_CLASS,
ITEM_MOVE_BUTTON_CLASS,
} from '../../config/selectors';
import { BUILDER } from '../../langs/constants';
import TreeModal, { TreeModalProps } from '../main/MoveTreeModal';
import { NavigationElement } from '../main/itemSelectionModal/Breadcrumbs';
import ItemSelectionModal, {
ItemSelectionModalProps,
} from '../main/itemSelectionModal/ItemSelectionModal';

const TITLE_MAX_NAME_LENGTH = 15;

type MoveButtonProps = {
itemIds: string[];
Expand All @@ -42,6 +45,8 @@ const MoveButton = ({
const [open, setOpen] = useState(false);
const [itemIds, setItemIds] = useState<string[]>(defaultItemsIds || []);

const { data: items } = hooks.useItems(itemIds);

const openMoveModal = (newItemIds: string[]) => {
setOpen(true);
setItemIds(newItemIds);
Expand All @@ -51,14 +56,11 @@ const MoveButton = ({
setOpen(false);
};

const onConfirm: TreeModalProps['onConfirm'] = (payload) => {
const onConfirm: ItemSelectionModalProps['onConfirm'] = (payload) => {
// change item's root id to null
const newPayload = {
...payload,
to:
payload.to && payload.to !== HOME_MODAL_ITEM_ID && validate(payload.to)
? payload.to
: undefined,
ids: itemIds,
to: payload,
};
moveItems(newPayload);
onClose();
Expand All @@ -73,6 +75,41 @@ const MoveButton = ({
onClick?.();
};

const isDisabled = (item: NavigationElement, homeId: string) => {
// cannot move inside self
const moveInSelf = itemIds.includes(item.id);

// cannot move in same direct parent
// todo: not opti because we only have the ids from the table
const directParentIds = Object.values(items?.data ?? {})?.map((i) =>
getDirectParentId(i.path),
);
const moveInDirectParent = directParentIds?.includes(item.id);

// cannot move to home if was already on home
let moveToHome = false;

if (items?.data) {
moveToHome =
item.id === homeId &&
!getDirectParentId(Object.values(items.data)[0].path);
}
return moveInSelf || moveInDirectParent || moveToHome;
};

const title = items
? translateBuilder(BUILDER.MOVE_ITEM_MODAL_TITLE, {
name: applyEllipsisOnLength(
Object.values(items.data)[0].name,
TITLE_MAX_NAME_LENGTH,
),
count: itemIds.length,
})
: translateBuilder(BUILDER.MOVE_ITEM_MODAL_TITLE);

const buttonText = (name?: string) =>
translateBuilder(BUILDER.MOVE_BUTTON, { name, count: name ? 1 : 0 });

return (
<>
<GraaspMoveButton
Expand All @@ -85,13 +122,15 @@ const MoveButton = ({
iconClassName={ITEM_MOVE_BUTTON_CLASS}
/>

{itemIds.length > 0 && open && (
<TreeModal
{items?.data && open && (
<ItemSelectionModal
title={title}
isDisabled={isDisabled}
buttonText={buttonText}
onClose={onClose}
open={open}
itemIds={itemIds}
onConfirm={onConfirm}
title={BUILDER.MOVE_ITEM_MODAL_TITLE}
items={Object.values(items.data)}
/>
)}
</>
Expand Down
198 changes: 0 additions & 198 deletions src/components/main/MoveTreeModal.tsx

This file was deleted.

Loading

0 comments on commit 0fe8da0

Please sign in to comment.