Skip to content

Commit

Permalink
fix(app-headless-cms): add delete prompt to objects and dynamic zones (
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel910 authored Dec 18, 2024
1 parent f2b573c commit edaf911
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
import { makeDecoratable } from "@webiny/react-composition";
import { TemplateProvider } from "~/admin/plugins/fieldRenderers/dynamicZone/TemplateProvider";
import { ParentValueIndexProvider } from "~/admin/components/ModelFieldProvider";
import { useConfirmationDialog } from "@webiny/app-admin";

const BottomMargin = styled.div`
margin-bottom: 20px;
Expand Down Expand Up @@ -189,6 +190,12 @@ interface MultiValueDynamicZoneProps {
}

export const MultiValueDynamicZone = (props: MultiValueDynamicZoneProps) => {
const { showConfirmation } = useConfirmationDialog({
message: `Are you sure you want to delete this item? This action is not reversible.`,
acceptLabel: `Yes, I'm sure!`,
cancelLabel: `No, leave it.`
});

const { bind, getBind, contentModel } = props;
const onTemplate = (template: CmsDynamicZoneTemplateWithTypename) => {
bind.appendValue({ _templateId: template.id, __typename: template.__typename });
Expand All @@ -211,6 +218,12 @@ export const MultiValueDynamicZone = (props: MultiValueDynamicZoneProps) => {
{values.map((value, index) => {
const Bind = getBind(index);

const onDelete = () => {
showConfirmation(() => {
bind.removeValue(index);
});
};

return (
<ParentValueIndexProvider key={index} index={index}>
<TemplateValueForm
Expand All @@ -221,7 +234,7 @@ export const MultiValueDynamicZone = (props: MultiValueDynamicZoneProps) => {
isLast={index === values.length - 1}
onMoveUp={() => bind.moveValueUp(index)}
onMoveDown={() => bind.moveValueDown(index)}
onDelete={() => bind.removeValue(index)}
onDelete={onDelete}
onClone={value => cloneValue(value, index)}
/>
</ParentValueIndexProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
ParentValueIndexProvider,
ModelFieldProvider
} from "~/admin/components/ModelFieldProvider";
import { useConfirmationDialog } from "@webiny/app-admin";

type GetBind = CmsModelFieldRendererProps["getBind"];

Expand All @@ -34,6 +35,12 @@ export const SingleValueDynamicZone = ({
contentModel,
getBind
}: SingleValueDynamicZoneProps) => {
const { showConfirmation } = useConfirmationDialog({
message: `Are you sure you want to remove this item? This action is not reversible.`,
acceptLabel: `Yes, I'm sure!`,
cancelLabel: `No, leave it.`
});

const onTemplate = (template: CmsDynamicZoneTemplateWithTypename) => {
bind.onChange({ _templateId: template.id, __typename: template.__typename });
};
Expand All @@ -47,7 +54,9 @@ export const SingleValueDynamicZone = ({
const Bind = getBind();

const unsetValue = () => {
bind.onChange(null);
showConfirmation(() => {
bind.onChange(null);
});
};

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
} from "./StyledComponents";
import { generateAlphaNumericLowerCaseId } from "@webiny/utils";
import { FieldSettings } from "~/admin/plugins/fieldRenderers/object/FieldSettings";
import { useConfirmationDialog } from "@webiny/app-admin";

const t = i18n.ns("app-headless-cms/admin/fields/text");

Expand All @@ -35,6 +36,12 @@ interface ActionsProps {
}

const Actions = ({ setHighlightIndex, bind, index }: ActionsProps) => {
const { showConfirmation } = useConfirmationDialog({
message: `Are you sure you want to delete this item? This action is not reversible.`,
acceptLabel: `Yes, I'm sure!`,
cancelLabel: `No, leave it.`
});

const { moveValueDown, moveValueUp } = bind.field;

const onDown = useCallback(
Expand All @@ -61,11 +68,21 @@ const Actions = ({ setHighlightIndex, bind, index }: ActionsProps) => {
[moveValueUp, index]
);

const onDelete = useCallback(
(ev: React.BaseSyntheticEvent) => {
ev.stopPropagation();
showConfirmation(() => {
bind.field.removeValue(index);
});
},
[index]
);

return (
<>
<IconButton icon={<ArrowDown />} onClick={onDown} />
<IconButton icon={<ArrowUp />} onClick={onUp} />
<IconButton icon={<DeleteIcon />} onClick={() => bind.field.removeValue(index)} />
<IconButton icon={<DeleteIcon />} onClick={onDelete} />
</>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
import { generateAlphaNumericLowerCaseId } from "@webiny/utils";
import { FieldSettings } from "./FieldSettings";
import { AccordionRenderSettings, getAccordionRenderSettings } from "../AccordionRenderSettings";
import { useConfirmationDialog } from "@webiny/app-admin";

const t = i18n.ns("app-headless-cms/admin/fields/text");

Expand All @@ -39,6 +40,12 @@ interface ActionsProps {
const Actions = ({ setHighlightIndex, bind, index }: ActionsProps) => {
const { moveValueDown, moveValueUp } = bind.field;

const { showConfirmation } = useConfirmationDialog({
message: `Are you sure you want to delete this item? This action is not reversible.`,
acceptLabel: `Yes, I'm sure!`,
cancelLabel: `No, leave it.`
});

const onDown = useCallback(
(ev: React.BaseSyntheticEvent) => {
ev.stopPropagation();
Expand All @@ -63,11 +70,21 @@ const Actions = ({ setHighlightIndex, bind, index }: ActionsProps) => {
[moveValueUp, index]
);

const onDelete = useCallback(
(ev: React.BaseSyntheticEvent) => {
ev.stopPropagation();
showConfirmation(() => {
bind.field.removeValue(index);
});
},
[index]
);

return (
<>
<IconButton icon={<ArrowDown />} onClick={onDown} />
<IconButton icon={<ArrowUp />} onClick={onUp} />
<IconButton icon={<DeleteIcon />} onClick={() => bind.field.removeValue(index)} />
<IconButton icon={<DeleteIcon />} onClick={onDelete} />
</>
);
};
Expand Down

0 comments on commit edaf911

Please sign in to comment.