Files
sign/apps/web/components/editor/field.tsx

100 lines
2.6 KiB
TypeScript
Raw Normal View History

2023-02-08 15:43:14 +01:00
import { ResizableBox, ResizeCallbackData } from "react-resizable";
2023-02-09 15:48:26 +01:00
import React, { SyntheticEvent, useEffect, useState } from "react";
2023-02-08 15:43:14 +01:00
import Draggable from "react-draggable";
2023-02-14 14:26:54 +01:00
import { CircleStackIcon, TrashIcon } from "@heroicons/react/24/solid";
2023-02-08 15:43:14 +01:00
import Logo from "../logo";
2023-02-14 14:26:54 +01:00
import { IconButton } from "@documenso/ui";
import toast from "react-hot-toast";
2023-02-09 15:48:26 +01:00
const stc = require("string-to-color");
2023-02-08 15:43:14 +01:00
type FieldPropsType = {
2023-02-09 15:48:26 +01:00
field: {
color: string;
type: string;
position: any;
2023-02-14 12:28:33 +01:00
positionX: number;
positionY: number;
2023-02-09 15:48:26 +01:00
id: string;
recipient: string;
};
2023-02-13 18:04:44 +01:00
onPositionChanged: any;
2023-02-08 15:43:14 +01:00
};
export default function Field(props: FieldPropsType) {
2023-02-09 15:48:26 +01:00
const [field, setField]: any = useState(props.field);
2023-02-14 12:28:33 +01:00
const [position, setPosition]: any = useState({
x: props.field.positionX,
y: props.field.positionY,
});
2023-02-09 15:48:26 +01:00
const nodeRef = React.createRef<HTMLDivElement>();
const onControlledDrag = (e: any, position: any) => {
const { x, y } = position;
setPosition({ x, y });
};
const onDragStop = (e: any, position: any) => {
if (!position) return;
const { x, y } = position;
2023-02-13 18:04:44 +01:00
props.onPositionChanged({ x, y }, props.field.id);
2023-02-09 15:48:26 +01:00
};
2023-02-08 15:43:14 +01:00
return (
2023-02-09 15:48:26 +01:00
<Draggable
nodeRef={nodeRef}
bounds="parent"
position={position}
onDrag={onControlledDrag}
onStop={onDragStop}
2023-02-14 12:28:33 +01:00
defaultPosition={{ x: 0, y: 0 }}
2023-02-09 15:48:26 +01:00
>
<div
ref={nodeRef}
style={{ background: stc(props.field.recipient) }}
2023-02-14 12:28:33 +01:00
className="cursor-move opacity-80 p-2 m-auto w-auto flex-row-reverse text-lg font-bold text-center absolute top-0 left-0"
2023-02-08 15:43:14 +01:00
>
2023-02-08 15:54:17 +01:00
<div className="m-auto w-auto flex-row-reverse text-lg font-bold text-center">
2023-02-14 14:26:54 +01:00
<IconButton
icon={TrashIcon}
onClick={(e: any) => {
if (confirm("Delete field?")) {
deleteField(e, props.field);
}
}}
></IconButton>
2023-02-08 15:54:17 +01:00
{/* todo icons */}
2023-02-14 13:09:43 +01:00
{field.type}
2023-02-09 15:48:26 +01:00
<div className="text-xs text-center">{props.field.recipient}</div>
2023-02-08 15:43:14 +01:00
</div>
2023-02-09 15:48:26 +01:00
</div>
2023-02-08 15:43:14 +01:00
</Draggable>
);
}
2023-02-14 14:26:54 +01:00
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",
},
}
);
}