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

feat: add duplicate item action #932

Merged
merged 13 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
27 changes: 27 additions & 0 deletions src/components/main/ItemMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useContext, useState } from 'react';

import FileCopyIcon from '@mui/icons-material/FileCopy';
import FlagIcon from '@mui/icons-material/Flag';
import LabelImportantIcon from '@mui/icons-material/LabelImportant';
import MoreVertIcon from '@mui/icons-material/MoreVert';
Expand All @@ -11,9 +12,13 @@ import MenuItem from '@mui/material/MenuItem';
import { DiscriminatedItem } from '@graasp/sdk';
import { ActionButton } from '@graasp/ui';

import { mutations } from '@/config/queryClient';
import { getParentsIdsFromPath } from '@/utils/item';

import { useBuilderTranslation } from '../../config/i18n';
import {
ITEM_MENU_BUTTON_CLASS,
ITEM_MENU_DUPLICATE_BUTTON_CLASS,
ITEM_MENU_FLAG_BUTTON_CLASS,
ITEM_MENU_SHORTCUT_BUTTON_CLASS,
buildItemMenu,
Expand Down Expand Up @@ -49,6 +54,7 @@ const ItemMenu = ({
CreateShortcutModalContext,
);
const { openModal: openFlagModal } = useContext(FlagItemModalContext);
const { mutate: copyItems } = mutations.useCopyItems();

const handleClick: IconButtonProps['onClick'] = (event) => {
setAnchorEl(event.currentTarget);
Expand All @@ -68,6 +74,18 @@ const ItemMenu = ({
handleClose();
};

const handleDuplicate = () => {
const parentsIds = getParentsIdsFromPath(item.path);
// get the close parent if not then undefined
const to =
parentsIds.length > 1 ? parentsIds[parentsIds.length - 2] : undefined;

const newPayload = {
ids: [item.id],
to,
};
copyItems(newPayload);
};
const renderEditorActions = () => {
if (!canEdit) {
return null;
Expand Down Expand Up @@ -116,6 +134,15 @@ const ItemMenu = ({
itemIds={[item.id]}
onClick={handleClose}
/>,
<MenuItem
onClick={handleDuplicate}
className={ITEM_MENU_DUPLICATE_BUTTON_CLASS}
>
<ListItemIcon>
<FileCopyIcon />
</ListItemIcon>
{translateBuilder(BUILDER.ITEM_MENU_DUPLICATE_MENU_ITEM)}
</MenuItem>,
];
};

Expand Down
1 change: 1 addition & 0 deletions src/config/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export const SHARED_ITEMS_ERROR_ALERT_ID = 'sharedItemsErrorAlert';
export const PUBLISHED_ITEMS_ERROR_ALERT_ID = 'publishedItemsErrorAlert';
export const FAVORITE_ITEMS_ERROR_ALERT_ID = 'favoriteItemsErrorAlert';
export const ITEM_MENU_SHORTCUT_BUTTON_CLASS = 'itemMenuShortcutButton';
export const ITEM_MENU_DUPLICATE_BUTTON_CLASS = 'itemMenuDuplicateButton';
export const ITEM_MENU_FAVORITE_BUTTON_CLASS = 'itemMenuFavoriteButton';
export const ITEM_MENU_FLAG_BUTTON_CLASS = 'itemMenuFlagButton';
export const buildFlagListItemId = (type: string): string =>
Expand Down
2 changes: 2 additions & 0 deletions src/langs/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export const BUILDER = {
ITEM_MEMBERSHIPS_TABLE_PERMISSION_HEADER:
'ITEM_MEMBERSHIPS_TABLE_PERMISSION_HEADER',
ITEM_MENU_CREATE_SHORTCUT_MENU_ITEM: 'ITEM_MENU_CREATE_SHORTCUT_MENU_ITEM',
ITEM_MENU_DUPLICATE_MENU_ITEM: 'ITEM_MENU_DUPLICATE_MENU_ITEM',
ITEM_MENU_FLAG_MENU_ITEM: 'ITEM_MENU_FLAG_MENU_ITEM',
ITEM_METADATA_CREATED_AT_TITLE: 'ITEM_METADATA_CREATED_AT_TITLE',
ITEM_METADATA_LINK_TITLE: 'ITEM_METADATA_LINK_TITLE',
Expand Down Expand Up @@ -356,4 +357,5 @@ export const BUILDER = {
// temporary message
"You can also find the items of this page in ''My Graasp''. This page will be unavailable soon.":
"You can also find the items of this page in ''My Graasp''. This page will be unavailable soon.",
COPY: 'COPY',
LinaYahya marked this conversation as resolved.
Show resolved Hide resolved
};
4 changes: 3 additions & 1 deletion src/langs/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -291,5 +291,7 @@
"SHORT_LINK_MAX_CHARS_ERROR": "The short link must not exceed {{data}} chars.",
"SHORT_LINK_INVALID_CHARS_ERROR": "The short link contains invalid chars ({{data}}).",
"SHORT_LINK_UNKNOWN_ERROR": "An unknown error occurred.",
"You can also find the items of this page in ''My Graasp''. This page will be unavailable soon.": "You can also find the items of this page in ''My Graasp''. This page ''Shared Items'' will be unavailable soon."
"You can also find the items of this page in ''My Graasp''. This page will be unavailable soon.": "You can also find the items of this page in ''My Graasp''. This page ''Shared Items'' will be unavailable soon.",
"ITEM_MENU_DUPLICATE_MENU_ITEM": "Duplicate",
"COPY": "copy"
LinaYahya marked this conversation as resolved.
Show resolved Hide resolved
}