|
| 1 | +import { LinkButton } from "@strapi/design-system"; |
| 2 | +import { Duplicate } from "@strapi/icons"; |
| 3 | +import React from "react"; |
| 4 | +import { useIntl } from "react-intl"; |
| 5 | +import { useLocation, useNavigate, useParams } from "react-router-dom"; |
| 6 | + |
| 7 | +const DuplicateButton = () => { |
| 8 | + const { id: documentId, slug: modelUID, collectionType } = useParams(); |
| 9 | + const isSingleType = collectionType === "single-types"; |
| 10 | + |
| 11 | + const { formatMessage } = useIntl(); |
| 12 | + |
| 13 | + const content = { |
| 14 | + id: "duplicate-button.components.duplicate.button", |
| 15 | + defaultMessage: "Duplicate", |
| 16 | + }; |
| 17 | + |
| 18 | + const navigate = useNavigate(); |
| 19 | + const { pathname, search } = useLocation(); |
| 20 | + const goToCloneRoute = (evt) => { |
| 21 | + evt.preventDefault(); |
| 22 | + navigate(pathname.replace(modelUID, `${modelUID}/clone`) + search); |
| 23 | + }; |
| 24 | + |
| 25 | + if (isSingleType || !documentId || !modelUID) return null; |
| 26 | + if (documentId === "create" || documentId === "clone") return null; |
| 27 | + return ( |
| 28 | + <> |
| 29 | + {window && window.location ? ( |
| 30 | + <LinkButton |
| 31 | + href={ |
| 32 | + // we use `window.location` here because strapi has set a |
| 33 | + // base path in the admin panel and `pathname` from `useLocation()` |
| 34 | + // does not include the base path |
| 35 | + window.location.href.replace(modelUID, `${modelUID}/clone`) + search |
| 36 | + } |
| 37 | + variant="secondary" |
| 38 | + style={{ width: "100%" }} |
| 39 | + startIcon={<Duplicate />} |
| 40 | + onClick={goToCloneRoute} |
| 41 | + > |
| 42 | + {formatMessage(content)} |
| 43 | + </LinkButton> |
| 44 | + ) : null} |
| 45 | + </> |
| 46 | + ); |
| 47 | +}; |
| 48 | + |
| 49 | +export default DuplicateButton; |
0 commit comments