baby_in_magento

f3k

May 4th, 2024
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. import React, { useEffect, useState } from 'react';
  2.  
  3. const ProductDetailsBlock = ({ customAttributes, classes, selectedColor, selectedSize,productDetails,breakDown }) => {
  4.  
  5. var defaultWeight = null;
  6.  
  7. if (Array.isArray(customAttributes)) {
  8. customAttributes.filter(customAttribute => {
  9. if (customAttribute.attribute_metadata.code === 'gold_weight') {
  10. defaultWeight = breakDown ? breakDown?.productDetails?.variant_weight : null;
  11. // defaultWeight = customAttribute?.entered_attribute_value?.value;
  12. }
  13. });
  14. }
  15.  
  16. var defaultSize = null;
  17.  
  18. if (Array.isArray(customAttributes)) {
  19. customAttributes.filter(customAttribute => {
  20. if (customAttribute.attribute_metadata.code === 'ring_size') {
  21. defaultSize = customAttribute?.selected_attribute_options?.attribute_option[0]?.label;
  22. }
  23. });
  24. }
  25.  
  26. const [goldWeight, setGoldWeight] = useState(defaultWeight);
  27.  
  28. useEffect(() => {
  29. selectedSize ?
  30. setGoldWeight(parseFloat(defaultWeight) + Math.max(parseFloat(selectedSize?.title) - defaultSize, 0) * 0.1)
  31. : setGoldWeight(defaultWeight)
  32. }, [selectedSize]);
  33.  
  34.  
  35. const hiddenTags = [
  36. 'setting_type',
  37. 'is_on_sale',
  38. 'ready_to_ship',
  39. 'best_seller',
  40. 'new_launch',
  41. 'make_on_order',
  42. 'design_family',
  43. 'is_outlet_available',
  44. 'is_virtual_try_on',
  45. 'glow_sku',
  46. 'solitaire_without_diamond_price',
  47. 'constituent_final_price'
  48. ];
  49.  
  50. const getAttributeValue = (i) => {
  51.  
  52. if (i?.attribute_metadata?.code == "gold_color") {
  53. return breakDown ? breakDown?.productDetails?.variant_color : null;
  54. // return selectedColor?.title ||
  55. // i?.selected_attribute_options?.attribute_option.map((j, index) => {
  56. // return index == i?.selected_attribute_options
  57. // ?.attribute_option?.length - 1 ? j.label : j.label + ", "
  58. // })
  59. }
  60.  
  61. if (i?.attribute_metadata?.code == "ring_size") {
  62. return selectedSize?.title || defaultSize
  63. }
  64. if (i?.attribute_metadata?.code == "gold_weight") {
  65. return parseFloat(goldWeight).toFixed(2)
  66. }
  67.  
  68. return i?.selected_attribute_options
  69. ?.attribute_option?.length ?
  70. i?.selected_attribute_options?.attribute_option.map((j, index) => {
  71. return index == i?.selected_attribute_options
  72. ?.attribute_option?.length - 1 ? j.label : j.label + ", "
  73. })
  74. : i?.entered_attribute_value
  75. ?.value
  76. ? i?.entered_attribute_value
  77. ?.value
  78. : ''
  79. }
  80.  
  81. const attribute_label_value = productDetails ? Object.keys(productDetails).map((label, index) => (
  82. <table>
  83. <tbody>
  84. {
  85. productDetails &&
  86. productDetails[label]?.attribute_code === 'gemstone_weight' &&
  87. productDetails[label]?.value == 0 ? null :
  88.  
  89. <tr key={index}>
  90. <td
  91. className={
  92. classes.nameColumn +
  93. ' font-semibold'
  94. }
  95. >
  96. {productDetails[label]?.label}
  97. </td>
  98. <td>
  99. {productDetails[label]?.value}
  100. {productDetails[label]?.units}
  101. </td>
  102. </tr>
  103. }
  104. </tbody>
  105. </table>
  106. )) : null
  107.  
  108. return (
  109. <div>
  110. <table>
  111. <tbody>
  112. {customAttributes &&
  113. customAttributes.map((i, index) => {
  114. return (
  115. !hiddenTags.includes(
  116. i?.attribute_metadata?.code
  117. ) && (
  118. <tr key={index}>
  119. <td
  120. className={
  121. classes.nameColumn +
  122. ' font-semibold'
  123. }
  124. >
  125. {i?.attribute_metadata?.label}
  126. </td>
  127. <td>
  128. {getAttributeValue(i)}
  129. </td>
  130. </tr>
  131. )
  132. );
  133. })}
  134. </tbody>
  135. </table>
  136. {attribute_label_value}
  137. </div>
  138. )
  139. }
  140.  
  141. export default ProductDetailsBlock;
Advertisement
Add Comment
Please, Sign In to add comment