|
| 1 | +import React, { useState } from "react"; |
| 2 | +import Modal from "react-bootstrap/Modal"; |
| 3 | +import ModalHeader from "react-bootstrap/esm/ModalHeader"; |
| 4 | +import { themeColor } from "../../utils/ThemeColor/backColor"; |
| 5 | + |
| 6 | +function PlaceholderCopy(props) { |
| 7 | + const copyType = ["All pages", "All pages but last", "All pages but first"]; |
| 8 | + const [selectCopyType, setSelectCopyType] = useState(""); |
| 9 | + |
| 10 | + //get RandomKey Id |
| 11 | + const randomKey = () => { |
| 12 | + const randomId = Math.floor(1000 + Math.random() * 9000); |
| 13 | + return randomId; |
| 14 | + }; |
| 15 | + |
| 16 | + //function for get copy placeholder position |
| 17 | + const getCopyPlaceholderPosition = ( |
| 18 | + type, |
| 19 | + rest, |
| 20 | + newPageNumber, |
| 21 | + existPlaceholderPosition |
| 22 | + ) => { |
| 23 | + let obj; |
| 24 | + |
| 25 | + const filterPosition = |
| 26 | + existPlaceholderPosition && |
| 27 | + existPlaceholderPosition.filter( |
| 28 | + (data) => data.xPosition !== rest.xPosition |
| 29 | + ); |
| 30 | + //copy all page placeholder at requested position except first page |
| 31 | + if (newPageNumber === 1 && type === "first") { |
| 32 | + if (filterPosition && filterPosition.length > 0) { |
| 33 | + return (obj = { |
| 34 | + pageNumber: newPageNumber, |
| 35 | + pos: [...filterPosition] |
| 36 | + }); |
| 37 | + } |
| 38 | + } |
| 39 | + //copy all page placeholder at requested position except last page |
| 40 | + else if (newPageNumber === props.allPages && type === "last") { |
| 41 | + if (existPlaceholderPosition) { |
| 42 | + return (obj = { |
| 43 | + pageNumber: newPageNumber, |
| 44 | + pos: [...filterPosition] |
| 45 | + }); |
| 46 | + } |
| 47 | + } |
| 48 | + //copy all page placeholder at requested position with existing placeholder position |
| 49 | + else if (existPlaceholderPosition) { |
| 50 | + return (obj = { |
| 51 | + pageNumber: newPageNumber, |
| 52 | + pos: [...filterPosition, rest] |
| 53 | + }); |
| 54 | + } |
| 55 | + //copy all page placeholder at requested position |
| 56 | + else { |
| 57 | + return (obj = { |
| 58 | + pageNumber: newPageNumber, |
| 59 | + pos: [rest] |
| 60 | + }); |
| 61 | + } |
| 62 | + }; |
| 63 | + |
| 64 | + //function for copy placeholder as per select copy type |
| 65 | + const copyPlaceholder = (type) => { |
| 66 | + let newPlaceholderPosition = []; |
| 67 | + let newPageNumber = 1; |
| 68 | + const signerPosition = props.xyPostion; |
| 69 | + |
| 70 | + //handle placeholder array and copy for multiple signers placeholder at requested location |
| 71 | + if (props.signerObjId) { |
| 72 | + //get current signers data |
| 73 | + const filterSignerPosition = signerPosition.filter( |
| 74 | + (data) => data.signerObjId === props.signerObjId |
| 75 | + ); |
| 76 | + //get current pagenumber's all placeholder position data |
| 77 | + const placeholderPosition = filterSignerPosition[0].placeHolder.filter( |
| 78 | + (data) => data.pageNumber === props.pageNumber |
| 79 | + ); |
| 80 | + //get current placeholder position data which user want to copy |
| 81 | + const currentPlaceholder = placeholderPosition[0].pos.filter( |
| 82 | + (position) => position.key === props.signKey |
| 83 | + ); |
| 84 | + const { key, ...rest } = currentPlaceholder[0]; |
| 85 | + for (let i = 0; i < props.allPages; i++) { |
| 86 | + const newId = randomKey(); |
| 87 | + rest.key = newId; |
| 88 | + //get exist placeholder position for particular page |
| 89 | + const existPlaceholder = filterSignerPosition[0].placeHolder.filter( |
| 90 | + (data) => data.pageNumber == newPageNumber |
| 91 | + ); |
| 92 | + const existPlaceholderPosition = |
| 93 | + existPlaceholder[0] && existPlaceholder[0].pos; |
| 94 | + |
| 95 | + //function for get copy to requested location of placeholder position |
| 96 | + const getPlaceholderObj = getCopyPlaceholderPosition( |
| 97 | + type, |
| 98 | + rest, |
| 99 | + newPageNumber, |
| 100 | + existPlaceholderPosition |
| 101 | + ); |
| 102 | + if (getPlaceholderObj) { |
| 103 | + newPlaceholderPosition.push(getPlaceholderObj); |
| 104 | + } |
| 105 | + newPageNumber++; |
| 106 | + } |
| 107 | + |
| 108 | + const updatedSignerPlaceholder = signerPosition.map( |
| 109 | + (signersData, ind) => { |
| 110 | + if (signersData.signerObjId === props.signerObjId) { |
| 111 | + return { |
| 112 | + ...signersData, |
| 113 | + placeHolder: newPlaceholderPosition |
| 114 | + }; |
| 115 | + } |
| 116 | + return signersData; |
| 117 | + } |
| 118 | + ); |
| 119 | + |
| 120 | + const signersData = signerPosition; |
| 121 | + signersData.splice(0, signerPosition.length, ...updatedSignerPlaceholder); |
| 122 | + props.setXyPostion(signersData); |
| 123 | + } |
| 124 | + //handle signyourself array and copy for single signers placeholder at requested location |
| 125 | + else { |
| 126 | + const xyPostion = props.xyPostion; |
| 127 | + |
| 128 | + const placeholderPosition = xyPostion.filter( |
| 129 | + (data) => data.pageNumber === props.pageNumber |
| 130 | + ); |
| 131 | + //get current placeholder position data which user want to copy |
| 132 | + const currentPlaceholder = placeholderPosition[0].pos.filter( |
| 133 | + (pos) => pos.key === props.signKey |
| 134 | + ); |
| 135 | + const { key, ...rest } = currentPlaceholder[0]; |
| 136 | + for (let i = 0; i < props.allPages; i++) { |
| 137 | + //get exist placeholder position for particular page |
| 138 | + const existPlaceholder = xyPostion.filter( |
| 139 | + (data) => data.pageNumber == newPageNumber |
| 140 | + ); |
| 141 | + const existPlaceholderPosition = |
| 142 | + existPlaceholder[0] && existPlaceholder[0].pos; |
| 143 | + |
| 144 | + const newId = randomKey(); |
| 145 | + rest.key = newId; |
| 146 | + //function for get copy to requested location of placeholder position |
| 147 | + const getPlaceholderObj = getCopyPlaceholderPosition( |
| 148 | + type, |
| 149 | + rest, |
| 150 | + newPageNumber, |
| 151 | + existPlaceholderPosition |
| 152 | + ); |
| 153 | + if (getPlaceholderObj) { |
| 154 | + newPlaceholderPosition.push(getPlaceholderObj); |
| 155 | + } |
| 156 | + |
| 157 | + newPageNumber++; |
| 158 | + } |
| 159 | + props.setXyPostion(newPlaceholderPosition); |
| 160 | + } |
| 161 | + }; |
| 162 | + |
| 163 | + //function for getting selected type placeholder copy |
| 164 | + const handleApplyCopy = () => { |
| 165 | + if (selectCopyType === "All pages") { |
| 166 | + copyPlaceholder("all"); |
| 167 | + } else if (selectCopyType === "All pages but last") { |
| 168 | + copyPlaceholder("last"); |
| 169 | + } else if (selectCopyType === "All pages but first") { |
| 170 | + copyPlaceholder("first"); |
| 171 | + } |
| 172 | + }; |
| 173 | + |
| 174 | + return ( |
| 175 | + <Modal show={props.isPageCopy}> |
| 176 | + <ModalHeader style={{ background: themeColor() }}> |
| 177 | + <span style={{ color: "white" }}>Place All pages</span> |
| 178 | + </ModalHeader> |
| 179 | + |
| 180 | + <Modal.Body> |
| 181 | + {copyType.map((data, key) => { |
| 182 | + return ( |
| 183 | + <div key={key} style={{ display: "flex", flexDirection: "column" }}> |
| 184 | + <label key={key} style={{ fontSize: "16px", fontWeight: "500" }}> |
| 185 | + <input |
| 186 | + style={{ accentColor: "red", marginRight: "10px" }} |
| 187 | + type="radio" |
| 188 | + value={data} |
| 189 | + onChange={() => setSelectCopyType(data)} |
| 190 | + checked={selectCopyType === data} |
| 191 | + /> |
| 192 | + |
| 193 | + {data} |
| 194 | + </label> |
| 195 | + </div> |
| 196 | + ); |
| 197 | + })} |
| 198 | + </Modal.Body> |
| 199 | + |
| 200 | + <Modal.Footer> |
| 201 | + <button |
| 202 | + style={{ |
| 203 | + color: "black" |
| 204 | + }} |
| 205 | + type="button" |
| 206 | + className="finishBtn" |
| 207 | + onClick={() => props.setIsPageCopy(false)} |
| 208 | + > |
| 209 | + Cancel |
| 210 | + </button> |
| 211 | + |
| 212 | + <button |
| 213 | + onClick={() => { |
| 214 | + handleApplyCopy(); |
| 215 | + props.setIsPageCopy(false); |
| 216 | + }} |
| 217 | + style={{ |
| 218 | + background: themeColor() |
| 219 | + }} |
| 220 | + type="button" |
| 221 | + disabled={!selectCopyType} |
| 222 | + className="finishBtn" |
| 223 | + > |
| 224 | + Apply |
| 225 | + </button> |
| 226 | + </Modal.Footer> |
| 227 | + </Modal> |
| 228 | + ); |
| 229 | +} |
| 230 | + |
| 231 | +export default PlaceholderCopy; |
0 commit comments