mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 16:23:06 +10:00
♻️ upsertField and delete Field to package
This commit is contained in:
@ -7,6 +7,7 @@ import { FieldType } from "@prisma/client";
|
|||||||
import { Listbox, RadioGroup, Transition } from "@headlessui/react";
|
import { Listbox, RadioGroup, Transition } from "@headlessui/react";
|
||||||
import { CheckIcon, ChevronUpDownIcon } from "@heroicons/react/24/outline";
|
import { CheckIcon, ChevronUpDownIcon } from "@heroicons/react/24/outline";
|
||||||
import { classNames } from "@documenso/lib";
|
import { classNames } from "@documenso/lib";
|
||||||
|
import { createOrUpdateField, deleteField } from "@documenso/lib/api";
|
||||||
const stc = require("string-to-color");
|
const stc = require("string-to-color");
|
||||||
|
|
||||||
const PDFViewer = dynamic(() => import("./pdf-viewer"), {
|
const PDFViewer = dynamic(() => import("./pdf-viewer"), {
|
||||||
@ -14,13 +15,13 @@ const PDFViewer = dynamic(() => import("./pdf-viewer"), {
|
|||||||
});
|
});
|
||||||
|
|
||||||
export default function PDFEditor(props: any) {
|
export default function PDFEditor(props: any) {
|
||||||
|
const router = useRouter();
|
||||||
const [selectedRecipient, setSelectedRecipient]: any = useState(
|
const [selectedRecipient, setSelectedRecipient]: any = useState(
|
||||||
props?.document?.Recipient[0]
|
props?.document?.Recipient[0]
|
||||||
);
|
);
|
||||||
const noRecipients = props?.document?.Recipient?.length === 0;
|
const noRecipients = props?.document?.Recipient?.length === 0;
|
||||||
const [fields, setFields] = useState<any[]>(props.document.Field);
|
const [fields, setFields] = useState<any[]>(props.document.Field);
|
||||||
const [adding, setAdding] = useState(false);
|
const [adding, setAdding] = useState(false);
|
||||||
const router = useRouter();
|
|
||||||
const fieldTypes = [
|
const fieldTypes = [
|
||||||
{ name: "Signature" },
|
{ name: "Signature" },
|
||||||
{ name: "Text" },
|
{ name: "Text" },
|
||||||
@ -35,7 +36,7 @@ export default function PDFEditor(props: any) {
|
|||||||
const movedField = fields.find((e) => e.id == id);
|
const movedField = fields.find((e) => e.id == id);
|
||||||
movedField.positionX = position.x.toFixed(0);
|
movedField.positionX = position.x.toFixed(0);
|
||||||
movedField.positionY = position.y.toFixed(0);
|
movedField.positionY = position.y.toFixed(0);
|
||||||
upsertField(props.document, movedField);
|
createOrUpdateField(props.document, movedField);
|
||||||
|
|
||||||
// no instant redraw neccessary, postion information for saving or later rerender is enough
|
// no instant redraw neccessary, postion information for saving or later rerender is enough
|
||||||
// setFields(newFields);
|
// setFields(newFields);
|
||||||
@ -221,7 +222,11 @@ export default function PDFEditor(props: any) {
|
|||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|
||||||
function addField(e: any, page: number) {
|
function addField(
|
||||||
|
e: any,
|
||||||
|
page: number,
|
||||||
|
type: FieldType = FieldType.SIGNATURE
|
||||||
|
) {
|
||||||
var rect = e.target.getBoundingClientRect();
|
var rect = e.target.getBoundingClientRect();
|
||||||
const fieldSize = { width: 192, height: 96 };
|
const fieldSize = { width: 192, height: 96 };
|
||||||
var newFieldX = e.clientX - rect.left - fieldSize.width / 2; //x position within the element.
|
var newFieldX = e.clientX - rect.left - fieldSize.width / 2; //x position within the element.
|
||||||
@ -243,74 +248,8 @@ export default function PDFEditor(props: any) {
|
|||||||
Recipient: selectedRecipient,
|
Recipient: selectedRecipient,
|
||||||
};
|
};
|
||||||
|
|
||||||
upsertField(props?.document, signatureField).then((res) => {
|
createOrUpdateField(props?.document, signatureField).then((res) => {
|
||||||
setFields(fields.concat(res));
|
setFields(fields.concat(res));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function upsertField(document: any, field: any): Promise<any> {
|
|
||||||
try {
|
|
||||||
const created = await toast.promise(
|
|
||||||
fetch("/api/documents/" + document.id + "/fields", {
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
body: JSON.stringify(field),
|
|
||||||
}).then((res) => {
|
|
||||||
if (!res.ok) {
|
|
||||||
throw new Error(res.status.toString());
|
|
||||||
}
|
|
||||||
return res.json();
|
|
||||||
}),
|
|
||||||
{
|
|
||||||
loading: "Saving...",
|
|
||||||
success: "Saved.",
|
|
||||||
error: "Could not save :/",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "saving field",
|
|
||||||
style: {
|
|
||||||
minWidth: "200px",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
);
|
|
||||||
return created;
|
|
||||||
} catch (error) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function deleteField(field: any) {
|
|
||||||
if (!field.id) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const deleted = toast.promise(
|
|
||||||
fetch("/api/documents/" + 0 + "/fields/" + field.id, {
|
|
||||||
method: "DELETE",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
body: JSON.stringify(field),
|
|
||||||
}).then((res) => {
|
|
||||||
if (!res.ok) {
|
|
||||||
throw new Error(res.status.toString());
|
|
||||||
}
|
|
||||||
return res;
|
|
||||||
}),
|
|
||||||
{
|
|
||||||
loading: "Deleting...",
|
|
||||||
success: "Deleted.",
|
|
||||||
error: "Could not delete :/",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "delete",
|
|
||||||
style: {
|
|
||||||
minWidth: "200px",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
);
|
|
||||||
return deleted;
|
|
||||||
} catch (error) {}
|
|
||||||
}
|
|
||||||
|
|||||||
@ -8,6 +8,7 @@ import { Button } from "@documenso/ui";
|
|||||||
import { CheckBadgeIcon } from "@heroicons/react/24/outline";
|
import { CheckBadgeIcon } from "@heroicons/react/24/outline";
|
||||||
import toast from "react-hot-toast";
|
import toast from "react-hot-toast";
|
||||||
import { FieldType } from "@prisma/client";
|
import { FieldType } from "@prisma/client";
|
||||||
|
import { createOrUpdateField, deleteField } from "@documenso/lib/api";
|
||||||
|
|
||||||
const PDFViewer = dynamic(() => import("./pdf-viewer"), {
|
const PDFViewer = dynamic(() => import("./pdf-viewer"), {
|
||||||
ssr: false,
|
ssr: false,
|
||||||
@ -161,7 +162,7 @@ export default function PDFSigner(props: any) {
|
|||||||
Recipient: recipient,
|
Recipient: recipient,
|
||||||
};
|
};
|
||||||
|
|
||||||
upsertField(props.document, signatureField).then((res) => {
|
createOrUpdateField(props.document, signatureField).then((res) => {
|
||||||
setFields(fields.concat(res));
|
setFields(fields.concat(res));
|
||||||
setDialogField(res);
|
setDialogField(res);
|
||||||
setOpen(true);
|
setOpen(true);
|
||||||
@ -193,67 +194,4 @@ export default function PDFSigner(props: any) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function deleteField(field: any) {
|
|
||||||
if (!field.id) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
fetch("/api/documents/" + 0 + "/fields/" + field.id, {
|
|
||||||
method: "DELETE",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
body: JSON.stringify(field),
|
|
||||||
}).then((res) => {
|
|
||||||
if (!res.ok) {
|
|
||||||
throw new Error(res.status.toString());
|
|
||||||
}
|
|
||||||
return res;
|
|
||||||
}),
|
|
||||||
{
|
|
||||||
loading: "Deleting...",
|
|
||||||
success: "Deleted.",
|
|
||||||
error: "Could not delete :/",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "delete",
|
|
||||||
style: {
|
|
||||||
minWidth: "200px",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
} catch (error) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function upsertField(document: any, field: any): Promise<any> {
|
|
||||||
try {
|
|
||||||
const created = await toast.promise(
|
|
||||||
fetch("/api/documents/" + document.id + "/fields", {
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
body: JSON.stringify(field),
|
|
||||||
}).then((res) => {
|
|
||||||
if (!res.ok) {
|
|
||||||
throw new Error(res.status.toString());
|
|
||||||
}
|
|
||||||
return res.json();
|
|
||||||
}),
|
|
||||||
{
|
|
||||||
loading: "Adding...",
|
|
||||||
success: "Added.",
|
|
||||||
error: "Could not add :/",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "saving field",
|
|
||||||
style: {
|
|
||||||
minWidth: "200px",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
);
|
|
||||||
return created;
|
|
||||||
} catch (error) {}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
35
packages/lib/api/createOrUpdateField.ts
Normal file
35
packages/lib/api/createOrUpdateField.ts
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import toast from "react-hot-toast";
|
||||||
|
|
||||||
|
export const createOrUpdateField = async (
|
||||||
|
document: any,
|
||||||
|
field: any
|
||||||
|
): Promise<any> => {
|
||||||
|
try {
|
||||||
|
const created = await toast.promise(
|
||||||
|
fetch("/api/documents/" + document.id + "/fields", {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify(field),
|
||||||
|
}).then((res) => {
|
||||||
|
if (!res.ok) {
|
||||||
|
throw new Error(res.status.toString());
|
||||||
|
}
|
||||||
|
return res.json();
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
loading: "Adding...",
|
||||||
|
success: "Added.",
|
||||||
|
error: "Could not add :/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "saving field",
|
||||||
|
style: {
|
||||||
|
minWidth: "200px",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
return created;
|
||||||
|
} catch (error) {}
|
||||||
|
};
|
||||||
36
packages/lib/api/deleteField.ts
Normal file
36
packages/lib/api/deleteField.ts
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
import toast from "react-hot-toast";
|
||||||
|
|
||||||
|
export const deleteField = async (field: any) => {
|
||||||
|
if (!field.id) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const deleted = toast.promise(
|
||||||
|
fetch("/api/documents/" + 0 + "/fields/" + field.id, {
|
||||||
|
method: "DELETE",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify(field),
|
||||||
|
}).then((res) => {
|
||||||
|
if (!res.ok) {
|
||||||
|
throw new Error(res.status.toString());
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
loading: "Deleting...",
|
||||||
|
success: "Deleted.",
|
||||||
|
error: "Could not delete :/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "delete",
|
||||||
|
style: {
|
||||||
|
minWidth: "200px",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
return deleted;
|
||||||
|
} catch (error) {}
|
||||||
|
};
|
||||||
2
packages/lib/api/index.ts
Normal file
2
packages/lib/api/index.ts
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
export { createOrUpdateField } from "./createOrUpdateField";
|
||||||
|
export { deleteField } from "./deleteField";
|
||||||
Reference in New Issue
Block a user