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: adding permissions to core actions and disabling on Hero #574

Merged
merged 11 commits into from
Aug 28, 2024
3 changes: 3 additions & 0 deletions apps/demo/app/custom-ui/[...puckPath]/client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,9 @@ export function Client({ path, isEdit }: { path: string; isEdit: boolean }) {
...config.components.Hero,
permissions: {
debug: false,
drag: false,
delete: false,
duplicate: false,
chrisvxd marked this conversation as resolved.
Show resolved Hide resolved
},
},
},
Expand Down
38 changes: 30 additions & 8 deletions packages/core/components/DraggableComponent/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { Loader } from "../Loader";
import { ActionBar } from "../ActionBar";
import { DefaultOverride } from "../DefaultOverride";
import { useLoadedOverrides } from "../../lib/use-loaded-overrides";
import { getPermissions } from "../../lib/get-permissions";
chrisvxd marked this conversation as resolved.
Show resolved Hide resolved

const getClassName = getClassNameFactory("DraggableComponent", styles);

Expand Down Expand Up @@ -82,7 +83,15 @@ export const DraggableComponent = ({
indicativeHover?: boolean;
style?: CSSProperties;
}) => {
const { zoomConfig, status, overrides, plugins } = useAppContext();
const {
zoomConfig,
status,
overrides,
plugins,
selectedItem,
config,
globalPermissions,
} = useAppContext();
const isModifierHeld = useModifierHeld("Alt");

const El = status !== "LOADING" ? Draggable : DefaultDraggable;
Expand All @@ -109,12 +118,20 @@ export const DraggableComponent = ({
[loadedOverrides]
);

const permissions =
selectedItem &&
getPermissions({
selectedItem,
globalPermissions: globalPermissions || {},
config,
});

return (
<El
key={id}
draggableId={id}
index={index}
isDragDisabled={isDragDisabled}
isDragDisabled={!permissions?.drag || isDragDisabled}
xaviemirmon marked this conversation as resolved.
Show resolved Hide resolved
disableSecondaryAnimation={disableSecondaryAnimation}
>
{(provided, snapshot) => (
Expand Down Expand Up @@ -163,12 +180,17 @@ export const DraggableComponent = ({
}}
>
<CustomActionBar label={label}>
xaviemirmon marked this conversation as resolved.
Show resolved Hide resolved
<ActionBar.Action onClick={onDuplicate} label="Duplicate">
<Copy size={16} />
</ActionBar.Action>
<ActionBar.Action onClick={onDelete} label="Delete">
<Trash size={16} />
</ActionBar.Action>
{permissions && permissions.duplicate && (
<ActionBar.Action onClick={onDuplicate} label="Duplicate">
<Copy size={16} />
</ActionBar.Action>
)}

{permissions && permissions.delete && (
<ActionBar.Action onClick={onDelete} label="Delete">
<Trash size={16} />
</ActionBar.Action>
)}
</CustomActionBar>
</div>
</div>
Expand Down
Loading