From 1c2b4794f39b646ae0bf392ddc8d4edb9677667b Mon Sep 17 00:00:00 2001
From: RaktimaNXG <raktima.c@nxglabs.com>
Date: Wed, 21 Aug 2024 13:55:54 +0530
Subject: [PATCH] fix: add copy option eside to widget for signature,stamp and
 text widgets

---
 .../public/locales/en/translation.json        |  3 +-
 .../public/locales/fr/translation.json        |  3 +-
 .../src/components/pdf/PlaceholderCopy.js     | 81 +++++++++++++++----
 apps/OpenSign/src/constant/Utils.js           | 27 +++----
 apps/OpenSign/src/pages/SignyourselfPdf.js    |  1 +
 5 files changed, 81 insertions(+), 34 deletions(-)

diff --git a/apps/OpenSign/public/locales/en/translation.json b/apps/OpenSign/public/locales/en/translation.json
index 88f4de9de..80dfbaed3 100644
--- a/apps/OpenSign/public/locales/en/translation.json
+++ b/apps/OpenSign/public/locales/en/translation.json
@@ -369,7 +369,8 @@
   "copy-type": {
     "All pages": "All pages",
     "All pages but last": "All pages but last",
-    "All pages but first": "All pages but first"
+    "All pages but first": "All pages but first",
+    "Beside to widget":"Beside to widget"
   },
   "options": "Options",
   "minimun-check": "Minimun check",
diff --git a/apps/OpenSign/public/locales/fr/translation.json b/apps/OpenSign/public/locales/fr/translation.json
index 14eb96d39..bffc22749 100644
--- a/apps/OpenSign/public/locales/fr/translation.json
+++ b/apps/OpenSign/public/locales/fr/translation.json
@@ -368,7 +368,8 @@
   "copy-type": {
     "All pages": "Toutes les pages",
     "All pages but last": "Toutes les pages sauf la dernière",
-    "All pages but first": "Toutes les pages sauf la première"
+    "All pages but first": "Toutes les pages sauf la première",
+    "Beside to widget": "À côté du widget"
   },
   "options": "Possibilités",
   "minimun-check": "Vérification minimale",
diff --git a/apps/OpenSign/src/components/pdf/PlaceholderCopy.js b/apps/OpenSign/src/components/pdf/PlaceholderCopy.js
index 64c85c413..a10bde125 100644
--- a/apps/OpenSign/src/components/pdf/PlaceholderCopy.js
+++ b/apps/OpenSign/src/components/pdf/PlaceholderCopy.js
@@ -1,12 +1,21 @@
 import React, { useState } from "react";
 import ModalUi from "../../primitives/ModalUi";
-import { randomId, textWidget } from "../../constant/Utils";
+import {
+  handleCopyNextToWidget,
+  randomId,
+  textWidget
+} from "../../constant/Utils";
 import { useTranslation } from "react-i18next";
 
 function PlaceholderCopy(props) {
   const { t } = useTranslation();
-  const copyType = ["All pages", "All pages but last", "All pages but first"];
-  const [selectCopyType, setSelectCopyType] = useState("");
+  const copyType = [
+    { id: 1, type: "All pages" },
+    { id: 2, type: "All pages but last" },
+    { id: 3, type: "All pages but first" },
+    { id: 4, type: "Beside to widget" }
+  ];
+  const [selectCopyType, setSelectCopyType] = useState(1);
   //function for get copy placeholder position
   const getCopyPlaceholderPosition = (
     type,
@@ -20,7 +29,7 @@ function PlaceholderCopy(props) {
         (data) => data.xPosition !== currentPlaceholder.xPosition
       );
     //copy all page placeholder at requested position except first page
-    if (newPageNumber === 1 && type === "first") {
+    if (newPageNumber === 1 && type === 3) {
       if (filterPosition && filterPosition.length > 0) {
         return {
           pageNumber: newPageNumber,
@@ -29,7 +38,7 @@ function PlaceholderCopy(props) {
       }
     }
     //copy all page placeholder at requested position except  last page
-    else if (newPageNumber === props.allPages && type === "last") {
+    else if (newPageNumber === props.allPages && type === 2) {
       if (existPlaceholderPosition) {
         return {
           pageNumber: newPageNumber,
@@ -172,20 +181,60 @@ function PlaceholderCopy(props) {
 
   //function for getting selected type placeholder copy
   const handleApplyCopy = () => {
-    if (selectCopyType === "All pages") {
-      copyPlaceholder("all");
-    } else if (selectCopyType === "All pages but last") {
-      copyPlaceholder("last");
-    } else if (selectCopyType === "All pages but first") {
-      copyPlaceholder("first");
+    if (selectCopyType === 4) {
+      const signerPosition = props.xyPostion;
+      let currentXYposition;
+      const signerId = props.signerObjId ? props.signerObjId : props.Id;
+      if (signerId) {
+        const filterSignerPosition = signerPosition.filter(
+          (item) => item?.Id === signerId
+        );
+        currentXYposition = filterSignerPosition[0].placeHolder.filter(
+          (data) => data.pageNumber === props?.pageNumber
+        );
+        //get current placeholder position data which user want to copy
+        currentXYposition = currentXYposition[0].pos.find(
+          (position) => position.key === props.signKey
+        );
+        //function to create new widget next to just widget
+        handleCopyNextToWidget(
+          currentXYposition,
+          props.widgetType,
+          props.xyPostion,
+          props.pageNumber,
+          props.setXyPostion,
+          props?.Id
+        );
+      } else {
+        const getIndex = props?.xyPostion.findIndex(
+          (data) => data.pageNumber === props.pageNumber
+        );
+        const placeholderPosition = props?.xyPostion[getIndex];
+        //get current placeholder position data which user want to copy
+        currentXYposition = placeholderPosition.pos.find(
+          (pos) => pos.key === props.signKey
+        );
+        //function to create new widget next to just widget
+        handleCopyNextToWidget(
+          currentXYposition,
+          props.widgetType,
+          props.xyPostion,
+          getIndex,
+          props.setXyPostion
+        );
+      }
+    } else {
+      copyPlaceholder(selectCopyType);
     }
   };
   const handleUniqueId = () => {
-    if (props.widgetType === textWidget) {
+    const signerId = props.signerObjId ? props.signerObjId : props.Id;
+    if (signerId && props.widgetType === textWidget) {
       props.setUniqueId(props?.tempSignerId);
       props.setTempSignerId("");
     }
     props.setIsPageCopy(false);
+    setSelectCopyType(1);
   };
   return (
     <ModalUi
@@ -201,11 +250,11 @@ function PlaceholderCopy(props) {
                 <input
                   className="mr-[8px] op-radio op-radio-xs"
                   type="radio"
-                  value={data}
-                  onChange={() => setSelectCopyType(data)}
-                  checked={selectCopyType === data}
+                  value={data.id}
+                  onChange={() => setSelectCopyType(data.id)}
+                  checked={selectCopyType === data.id}
                 />
-                {t(`copy-type.${data}`)}
+                {t(`copy-type.${data.type}`)}
               </label>
             </div>
           );
diff --git a/apps/OpenSign/src/constant/Utils.js b/apps/OpenSign/src/constant/Utils.js
index 108c97a92..458608b52 100644
--- a/apps/OpenSign/src/constant/Utils.js
+++ b/apps/OpenSign/src/constant/Utils.js
@@ -872,7 +872,6 @@ export const onChangeInput = (
     }
   } else {
     let getXYdata = xyPostion[index].pos;
-
     const updatePosition = getXYdata.map((positionData) => {
       if (positionData.key === signKey) {
         if (dateFormat) {
@@ -1859,29 +1858,26 @@ export const handleCopyNextToWidget = (
   setXyPostion,
   userId
 ) => {
-  const isSigners = xyPostion.some((data) => data.signerPtr);
   let filterSignerPos;
   //get position of previous widget and create new widget next to that widget on same data except
   // xPosition and key
-  let newpos = position;
+  let newposition = position;
   const calculateXPosition =
     parseInt(position.xPosition) +
     defaultWidthHeight(widgetType).width +
     resizeBorderExtraWidth();
   const newId = randomId();
-  newpos = { ...newpos, xPosition: calculateXPosition, key: newId };
+  newposition = { ...newposition, xPosition: calculateXPosition, key: newId };
   //if condition to create widget in request-sign flow
-  if (isSigners) {
-    if (userId) {
-      filterSignerPos = xyPostion.filter((data) => data.Id === userId);
-    }
-    const getPlaceHolder = filterSignerPos[0]?.placeHolder;
-    const getPageNumer = getPlaceHolder.filter(
+  if (userId) {
+    filterSignerPos = xyPostion.filter((data) => data.Id === userId);
+    const getPlaceHolder = filterSignerPos && filterSignerPos[0]?.placeHolder;
+    const getPageNumer = getPlaceHolder?.filter(
       (data) => data.pageNumber === index
     );
-    const getXYdata = getPageNumer[0].pos;
-    getXYdata.push(newpos);
-    if (getPageNumer.length > 0) {
+    const getXYdata = getPageNumer && getPageNumer[0]?.pos;
+    getXYdata.push(newposition);
+    if (getPageNumer && getPageNumer.length > 0) {
       const newUpdateSignPos = getPlaceHolder.map((obj) => {
         if (obj.pageNumber === index) {
           return { ...obj, pos: getXYdata };
@@ -1899,9 +1895,8 @@ export const handleCopyNextToWidget = (
       setXyPostion(newUpdateSigner);
     }
   } else {
-    // else condition to create widget in sign-yourself flow
-    let getXYdata = xyPostion[index].pos;
-    getXYdata.push(newpos);
+    let getXYdata = xyPostion[index]?.pos || [];
+    getXYdata.push(newposition);
     const updatePlaceholder = xyPostion.map((obj, ind) => {
       if (ind === index) {
         return { ...obj, pos: getXYdata };
diff --git a/apps/OpenSign/src/pages/SignyourselfPdf.js b/apps/OpenSign/src/pages/SignyourselfPdf.js
index 55acc3e7c..345acedb8 100644
--- a/apps/OpenSign/src/pages/SignyourselfPdf.js
+++ b/apps/OpenSign/src/pages/SignyourselfPdf.js
@@ -1333,6 +1333,7 @@ function SignYourSelf() {
                   allPages={allPages}
                   pageNumber={pageNumber}
                   signKey={signKey}
+                  widgetType={widgetType}
                 />
                 {/* this is modal of signature pad */}
                 <SignPad
