@omniumretail/component-library 1.2.14 → 1.2.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,7 +4,7 @@ interface CategoryResponse {
4
4
  serverReadyData: any;
5
5
  onNextCategoryAvailabilityChange: (hasNext: boolean) => void;
6
6
  onPreviousCategoryAvailabilityChange: (hasPrevious: boolean) => void;
7
- exportPdf: boolean;
7
+ exportPdf?: boolean;
8
8
  }
9
9
  export declare const CategoryReadOnly: React.ForwardRefExoticComponent<CategoryResponse & React.RefAttributes<unknown>>;
10
10
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@omniumretail/component-library",
3
- "version": "1.2.14",
3
+ "version": "1.2.15",
4
4
  "private": false,
5
5
  "main": "dist/bundle.js",
6
6
  "typings": "./dist/types/index",
@@ -13,6 +13,7 @@ const Template: Story<any> = (args) => {
13
13
  const categoryResponseRef = useRef(null);
14
14
  const [hasNext, setHasNext] = useState(true);
15
15
  const [hasPrevious, setHasPrevious] = useState(false);
16
+ const [teste, setTeste] = useState(false);
16
17
 
17
18
  const handleNextCategoryAvailabilityChange = (hasNext: boolean) => {
18
19
  setHasNext(hasNext);
@@ -43,6 +44,7 @@ const Template: Story<any> = (args) => {
43
44
  ref={categoryResponseRef}
44
45
  onNextCategoryAvailabilityChange={handleNextCategoryAvailabilityChange}
45
46
  onPreviousCategoryAvailabilityChange={handlePreviousCategoryAvailabilityChange}
47
+ exportPdf={ teste}
46
48
  >
47
49
  </CategoryReadOnly>
48
50
  </div>
@@ -55,6 +57,8 @@ const Template: Story<any> = (args) => {
55
57
  {hasNext && (
56
58
  <Button onClick={handleNextClickOutside}>Next on Parent</Button>
57
59
  )}
60
+
61
+ <Button onClick={() => setTeste(true)}>teste</Button>
58
62
  </div>
59
63
  </div>
60
64
  )
@@ -13,7 +13,7 @@ interface CategoryResponse {
13
13
  serverReadyData: any;
14
14
  onNextCategoryAvailabilityChange: (hasNext: boolean) => void;
15
15
  onPreviousCategoryAvailabilityChange: (hasPrevious: boolean) => void;
16
- exportPdf: boolean;
16
+ exportPdf?: boolean;
17
17
  };
18
18
 
19
19
  type Category = {
@@ -149,43 +149,56 @@ export const CategoryReadOnly = React.forwardRef((props: CategoryResponse, ref)
149
149
  const pdf = new jsPDF('p', 'mm', 'a4');
150
150
  const pdfWidth = pdf.internal.pageSize.getWidth();
151
151
  const pdfHeight = pdf.internal.pageSize.getHeight();
152
- const totalCategories = data.CategoryAnswers.length;
152
+ const spaceBetweenCategories = 20;
153
153
 
154
- let currentY = 0; // Track the current vertical position in the PDF
154
+ let currentY = 0;
155
155
 
156
- for (let index = 0; index < totalCategories; index++) {
157
- // Update the current category
158
- setCurrentKey(data.CategoryAnswers[index].Key);
159
- setSelectedCategory(data.CategoryAnswers[index]);
156
+ // Função recursiva para percorrer e capturar apenas os nós mais profundos
157
+ const processLeafNodes = async (category: any) => {
158
+ if (category.Children && category.Children.length > 0) {
159
+ // Se houver subcategorias, processa cada uma delas recursivamente
160
+ for (const childCategory of category.Children) {
161
+ await processLeafNodes(childCategory);
162
+ }
163
+ } else {
164
+ // Se não houver subcategorias, processa o nó como um nó folha
165
+ setCurrentKey(category.Key);
166
+ setSelectedCategory(category);
160
167
 
161
- // Wait until the new category is rendered
162
- await new Promise((resolve) => {
163
- setTimeout(async () => {
164
- // Capture the content of the PDF reference
165
- const canvas = await html2canvas(pdfRef.current!, {
166
- width: pdfRef.current!.scrollWidth,
167
- height: pdfRef.current!.scrollHeight,
168
- useCORS: true,
169
- allowTaint: true,
170
- scale: 1,
171
- });
168
+ // Aguarda a renderização da nova categoria
169
+ await new Promise((resolve) => {
170
+ setTimeout(async () => {
171
+ // Captura o conteúdo da referência PDF
172
+ const canvas = await html2canvas(pdfRef.current!, {
173
+ width: pdfRef.current!.scrollWidth,
174
+ height: pdfRef.current!.scrollHeight,
175
+ useCORS: true,
176
+ allowTaint: true,
177
+ scale: 1,
178
+ });
172
179
 
173
- const imgData = canvas.toDataURL('image/png');
174
- const imgHeight = (canvas.height * pdfWidth) / canvas.width;
180
+ const imgData = canvas.toDataURL('image/png');
181
+ const imgHeight = (canvas.height * pdfWidth) / canvas.width;
175
182
 
176
- // Check if there's enough space to add the image
177
- if (currentY + imgHeight > pdfHeight) {
178
- pdf.addPage(); // Add a new page if there's not enough space
179
- currentY = 0; // Reset the Y position to the top of the new page
180
- }
183
+ // Adiciona uma nova página se não houver espaço suficiente
184
+ if (currentY + imgHeight + spaceBetweenCategories > pdfHeight) {
185
+ pdf.addPage();
186
+ currentY = 0;
187
+ }
181
188
 
182
- // Add the image to the PDF at the current position
183
- pdf.addImage(imgData, 'PNG', 0, currentY, pdfWidth, imgHeight);
184
- currentY += imgHeight; // Update the Y position for the next image
189
+ // Adiciona a imagem ao PDF na posição atual
190
+ pdf.addImage(imgData, 'PNG', 0, currentY, pdfWidth, imgHeight);
191
+ currentY += imgHeight + spaceBetweenCategories;
185
192
 
186
- resolve(null);
187
- }, 150); // Delay to ensure the new category is rendered
188
- });
193
+ resolve(null);
194
+ }, 150);
195
+ });
196
+ }
197
+ };
198
+
199
+ // Percorre todas as categorias principais
200
+ for (const mainCategory of data.CategoryAnswers) {
201
+ await processLeafNodes(mainCategory);
189
202
  }
190
203
 
191
204
  pdf.save('evaluation_details.pdf');