@@ -3,56 +3,38 @@ import { v4 as uuidv4 } from 'uuid';
3
3
import WOO_CONFIG from 'utils/config/nextConfig' ;
4
4
5
5
/**
6
- * Convert price from string to floating value and convert it to use two decimals
7
- * @param {String } string
6
+ * Shorten inputted string (usually product description) to a maximum of 20 characters
7
+ * @param {String } string The string that we input
8
+ * @param {Integer } length The length that we want to shorten the text to
8
9
*/
9
- export const getFloatVal = ( string ) => {
10
- const stringWithoutKr = string . substring ( 2 ) ;
11
- const floatValue = parseFloat ( stringWithoutKr ) ;
12
- return null !== floatValue
13
- ? parseFloat ( parseFloat ( floatValue ) . toFixed ( 2 ) )
14
- : '' ;
10
+ export const trimmedStringToLength = ( string , length ) => {
11
+ if ( string . length > length ) {
12
+ const subStr = string . substring ( 0 , length ) ;
13
+ return subStr + '...' ;
14
+ } else {
15
+ return string ;
16
+ }
15
17
} ;
16
18
17
19
/**
18
- * Add first product to shopping cart
19
- * @param {Object } product
20
+ * Filter variant price. Changes from "kr198.00 - kr299.00" to kr299.00
21
+ * @param {String } price The inputted price that we need to convert
20
22
*/
21
- export const addFirstProduct = ( product ) => {
22
- const { price } = product . product . products . edges [ 0 ] . node ;
23
- const productData = product . product . products . edges [ 0 ] . node ;
24
- let productPrice = getFloatVal ( price ) ;
25
-
26
- // If no item in cart, push first product to an empty array
27
- let newCart = {
28
- products : [ ] ,
29
- totalProductsCount : 1 ,
30
- totalProductsPrice : productPrice ,
31
- } ;
32
-
33
- const newProduct = createNewProduct ( productData , productPrice , 1 ) ;
34
- newCart . products . push ( newProduct ) ;
35
-
36
- localStorage . setItem ( 'woocommerce-cart' , JSON . stringify ( newCart ) ) ;
37
- return newCart ;
23
+ export const filteredVariantPrice = ( price ) => {
24
+ // Filter price from "kr198.00 - kr299.00" to kr299.00
25
+ return price . substring ( price . length , price . indexOf ( '-' ) ) . replace ( '-' , '' ) ;
38
26
} ;
39
27
40
28
/**
41
- * Create a new product object
42
- * @param {Object } product
43
- * @param {Number } productPrice
44
- * @param {Number } quantity
45
- * @return { productId, image, name, price, quantity, totalPrice }
29
+ * Convert price from string to floating value and convert it to use two decimals
30
+ * @param {String } string
46
31
*/
47
- export const createNewProduct = ( product , productPrice , quantity ) => {
48
- return {
49
- productId : product . productId ,
50
- image : product . image . sourceUrl ,
51
- name : product . name ,
52
- price : productPrice ,
53
- quantity : quantity ,
54
- totalPrice : parseFloat ( ( quantity * productPrice ) . toFixed ( 2 ) ) ,
55
- } ;
32
+ export const getFloatVal = ( string ) => {
33
+ const stringWithoutKr = string . substring ( 2 ) ;
34
+ const floatValue = parseFloat ( stringWithoutKr ) ;
35
+ return null !== floatValue
36
+ ? parseFloat ( parseFloat ( floatValue ) . toFixed ( 2 ) )
37
+ : '' ;
56
38
} ;
57
39
58
40
/**
@@ -75,111 +57,6 @@ export const updateCart = (
75
57
) ;
76
58
} ;
77
59
78
- /**
79
- * Get updated products array
80
- * Update the product if it exists else,
81
- * add the new product to existing cart,
82
- *
83
- * @param {Object } existingProductsInCart Existing product in cart
84
- * @param {Object } product Product
85
- * @param {Integer } qtyToBeAdded Quantity
86
- * @param {Integer } newQty New qty of the product (optional)
87
- * @return {*[] }
88
- */
89
- export const getUpdatedProducts = (
90
- existingProductsInCart ,
91
- product ,
92
- qtyToBeAdded ,
93
- newQty = false
94
- ) => {
95
- // Check if the product already exits in the cart.
96
- const productExistsIndex = isProductInCart (
97
- existingProductsInCart ,
98
- product . productId
99
- ) ;
100
-
101
- // If product exists ( index of that product was found in the array ), update the product quantity and totalPrice
102
- if ( - 1 < productExistsIndex ) {
103
- let updatedProducts = existingProductsInCart ;
104
- let updatedProduct = updatedProducts [ productExistsIndex ] ;
105
-
106
- // If have new quantity of the product available, set that, else add the qtyToBeAdded
107
- updatedProduct . qty = newQty
108
- ? parseInt ( newQty )
109
- : parseInt ( updatedProduct . qty + qtyToBeAdded ) ;
110
- updatedProduct . totalPrice = parseFloat (
111
- ( updatedProduct . price * updatedProduct . qty ) . toFixed ( 2 )
112
- ) ;
113
- return updatedProducts ;
114
- }
115
- // If product not found push the new product to the existing product array.
116
- let productPrice = getFloatVal ( product . price ) ;
117
- const newProduct = createNewProduct ( product , productPrice , qtyToBeAdded ) ;
118
- existingProductsInCart . push ( newProduct ) ;
119
-
120
- return existingProductsInCart ;
121
- } ;
122
-
123
- /**
124
- * Returns index of the product if it exists.
125
- *
126
- * @param {Object } existingProductsInCart Existing Products.
127
- * @param {Integer } productId Product id.
128
- * @return {number | * } Index Returns -1 if product does not exist in the array, index number otherwise
129
- */
130
- const isProductInCart = ( existingProductsInCart , productId ) => {
131
- const returnItemThatExits = ( item ) => {
132
- if ( productId === item . productId ) {
133
- return item ;
134
- }
135
- return - 1 ;
136
- } ;
137
-
138
- // This new array will only contain the product which is matched.
139
- const newArray = existingProductsInCart . filter ( returnItemThatExits ) ;
140
-
141
- return existingProductsInCart . indexOf ( newArray [ 0 ] ) ;
142
- } ;
143
-
144
- /**
145
- * Remove item from the cart.
146
- *
147
- * @param {Integer } productId Product Id.
148
- * @return {any | string } Updated cart
149
- */
150
- export const removeItemFromCart = ( productId ) => {
151
- let existingCart = localStorage . getItem ( 'woo-next-cart' ) ;
152
- existingCart = JSON . parse ( existingCart ) ;
153
-
154
- // If there is only one item in the cart, delete the cart.
155
- if ( 1 === existingCart . products . length ) {
156
- localStorage . removeItem ( 'woo-next-cart' ) ;
157
- return null ;
158
- }
159
-
160
- // Check if the product already exits in the cart.
161
- const productExistsIndex = isProductInCart ( existingCart . products , productId ) ;
162
-
163
- // If product to be removed exits
164
- if ( - 1 < productExistsIndex ) {
165
- const productTobeRemoved = existingCart . products [ productExistsIndex ] ;
166
- const qtyToBeRemovedFromTotal = productTobeRemoved . qty ;
167
- const priceToBeDeductedFromTotal = productTobeRemoved . totalPrice ;
168
-
169
- // Remove that product from the array and update the total price and total quantity of the cart
170
- let updatedCart = existingCart ;
171
- updatedCart . products . splice ( productExistsIndex , 1 ) ;
172
- updatedCart . totalProductsCount =
173
- updatedCart . totalProductsCount - qtyToBeRemovedFromTotal ;
174
- updatedCart . totalProductsPrice =
175
- updatedCart . totalProductsPrice - priceToBeDeductedFromTotal ;
176
-
177
- localStorage . setItem ( 'woo-next-cart' , JSON . stringify ( updatedCart ) ) ;
178
- return updatedCart ;
179
- }
180
- return existingCart ;
181
- } ;
182
-
183
60
/**
184
61
* Returns cart data in the required format.
185
62
* @param {String } data Cart data
@@ -292,7 +169,7 @@ export const getUpdatedItems = (products, newQty, cartKey) => {
292
169
if ( cartItem . cartKey === cartKey ) {
293
170
updatedItems . push ( {
294
171
key : cartItem . cartKey ,
295
- quantity : parseInt ( newQty ) ,
172
+ quantity : parseInt ( newQty , 10 ) ,
296
173
} ) ;
297
174
298
175
// Otherwise just push the existing qty without updating.
0 commit comments