Skip to content

Commit 6ddfc95

Browse files
committed
[CodeFactor] Apply fixes
1 parent eaf7d58 commit 6ddfc95

File tree

1 file changed

+69
-72
lines changed

1 file changed

+69
-72
lines changed

store/useCart.js

Lines changed: 69 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,80 @@
11
import { defineStore } from "pinia";
22

33
const state = {
4-
cart: [],
5-
order: {},
6-
customer: {},
7-
loading: true,
8-
error: null,
4+
cart: [],
5+
order: {},
6+
customer: {},
7+
loading: true,
8+
error: null,
99
};
1010

1111
export const useCart = defineStore("shopState", {
12-
state: () => state,
13-
actions: {
14-
async getProductsFromApi() {
15-
try {
16-
this.loading = true;
17-
const response = await axios.get("/api/products");
18-
this.products = response.data;
19-
this.loading = false;
20-
} catch (error) {
21-
this.error = error;
22-
}
23-
},
24-
addToCart({ item }) {
25-
const foundProductInCartIndex = this.cart.findIndex(
26-
(cartItem) => item.slug === cartItem.slug
27-
);
12+
state: () => state,
13+
actions: {
14+
async getProductsFromApi() {
15+
try {
16+
this.loading = true;
17+
const response = await axios.get("/api/products");
18+
this.products = response.data;
19+
this.loading = false;
20+
} catch (error) {
21+
this.error = error;
22+
}
23+
},
24+
addToCart({ item }) {
25+
const foundProductInCartIndex = this.cart.findIndex(
26+
(cartItem) => item.slug === cartItem.slug
27+
);
2828

29-
if (foundProductInCartIndex > -1) {
30-
this.cart[foundProductInCartIndex].quantity += 1;
31-
} else {
32-
item.quantity = 1;
33-
this.cart.push(item);
34-
}
35-
},
36-
removeProductFromCart({ item }) {
37-
this.cart.splice(this.cart.indexOf(item), 1);
38-
},
29+
if (foundProductInCartIndex > -1) {
30+
this.cart[foundProductInCartIndex].quantity += 1;
31+
} else {
32+
item.quantity = 1;
33+
this.cart.push(item);
34+
}
35+
},
36+
removeProductFromCart({ item }) {
37+
this.cart.splice(this.cart.indexOf(item), 1);
38+
},
3939

40-
clearCart() {
41-
this.cart.length = 0;
42-
},
43-
clearCustomer() {
44-
this.customer = {};
45-
},
46-
clearOrder() {
47-
this.order = {};
48-
},
49-
saveCustomerDetails(customer) {
50-
this.customer = customer;
51-
},
52-
saveOrderId(order) {
53-
this.order = order;
54-
},
55-
getSingleProduct(slug) {
56-
return this.products.find((product) => product.slug === slug);
57-
},
40+
clearCart() {
41+
this.cart.length = 0;
42+
},
43+
clearCustomer() {
44+
this.customer = {};
45+
},
46+
clearOrder() {
47+
this.order = {};
48+
},
49+
saveCustomerDetails(customer) {
50+
this.customer = customer;
51+
},
52+
saveOrderId(order) {
53+
this.order = order;
54+
},
55+
getSingleProduct(slug) {
56+
return this.products.find((product) => product.slug === slug);
57+
},
58+
},
59+
getters: {
60+
getCartQuantity() {
61+
return this.cart.reduce((total, product) => total + product.quantity, 0);
62+
},
63+
getOrderDetails() {
64+
return this.order;
65+
},
66+
getCartContent() {
67+
return this.cart;
68+
},
69+
getCustomerDetails() {
70+
return this.customer;
5871
},
59-
getters: {
60-
getCartQuantity() {
61-
return this.cart.reduce(
62-
(total, product) => total + product.quantity,
63-
0
64-
);
65-
},
66-
getOrderDetails() {
67-
return this.order;
68-
},
69-
getCartContent() {
70-
return this.cart;
71-
},
72-
getCustomerDetails() {
73-
return this.customer;
74-
},
75-
getCartTotal() {
76-
return this.cart.reduce(
77-
(total, product) => total + product.price * product.quantity,
78-
0
79-
);
80-
},
72+
getCartTotal() {
73+
return this.cart.reduce(
74+
(total, product) => total + product.price * product.quantity,
75+
0
76+
);
8177
},
82-
persist: true,
78+
},
79+
persist: true,
8380
});

0 commit comments

Comments
 (0)