From 2f0fd4757e4c9cd4a4593049cea19be8ff89fa0e Mon Sep 17 00:00:00 2001 From: Timur Ercan Date: Tue, 14 Feb 2023 17:04:53 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20ondelete=20to=20editor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/web/components/editor/field.tsx | 32 ++------------- apps/web/components/editor/pdf-editor.tsx | 49 +++++++++++++++++++++++ apps/web/components/editor/pdf-viewer.jsx | 5 +++ 3 files changed, 57 insertions(+), 29 deletions(-) diff --git a/apps/web/components/editor/field.tsx b/apps/web/components/editor/field.tsx index 507b4bae1..04e76c408 100644 --- a/apps/web/components/editor/field.tsx +++ b/apps/web/components/editor/field.tsx @@ -18,6 +18,7 @@ type FieldPropsType = { recipient: string; }; onPositionChanged: any; + onDelete: any; }; export default function Field(props: FieldPropsType) { @@ -56,9 +57,9 @@ export default function Field(props: FieldPropsType) {
{ + onClick={(event: any) => { if (confirm("Delete field?")) { - deleteField(e, props.field); + props.onDelete(props.field.id); } }} > @@ -70,30 +71,3 @@ export default function Field(props: FieldPropsType) { ); } - -function deleteField(event: any, field: any) { - if (!field.id) { - return; - } - - return toast.promise( - fetch("/api/documents/" + 0 + "/fields/" + field.id, { - method: "DELETE", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify(field), - }), - { - loading: "Deleting...", - success: "Deleted.", - error: "Could not delete :/", - }, - { - id: "delete", - style: { - minWidth: "200px", - }, - } - ); -} diff --git a/apps/web/components/editor/pdf-editor.tsx b/apps/web/components/editor/pdf-editor.tsx index 3cdec78b1..81f2e9b47 100644 --- a/apps/web/components/editor/pdf-editor.tsx +++ b/apps/web/components/editor/pdf-editor.tsx @@ -28,6 +28,19 @@ export default function PDFEditor(props: any) { // setFields(newFields); } + function onDeleteHandler(id: any) { + const field = fields.find((e) => e.id == id); + const fieldIndex = fields.map((item) => item.id).indexOf(id); + console.log(fieldIndex); + if (fieldIndex > -1) { + const newFields = [...fields]; + newFields.splice(fieldIndex, 1); + + setFields(newFields); + deleteField(field); + } + } + return ( <>