user_actions.js
2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import axios from 'axios';
import {
LOGIN_USER,
REGISTER_USER,
AUTH_USER,
LOGOUT_USER,
ADD_TO_CART_USER,
GET_CART_ITEMS_USER,
REMOVE_CART_ITEM_USER,
ON_SUCCESS_BUY_USER
} from './types';
import { USER_SERVER } from '../components/Config.js';
export function registerUser(dataToSubmit) {
const request = axios.post(`${USER_SERVER}/register`, dataToSubmit)
.then(response => response.data);
return {
type: REGISTER_USER,
payload: request
}
}
export function loginUser(dataToSubmit) {
const request = axios.post(`${USER_SERVER}/login`, dataToSubmit)
.then(response => response.data);
return {
type: LOGIN_USER,
payload: request
}
}
export function auth() {
const request = axios.get(`${USER_SERVER}/auth`)
.then(response => response.data);
return {
type: AUTH_USER,
payload: request
}
}
export function logoutUser() {
const request = axios.get(`${USER_SERVER}/logout`)
.then(response => response.data);
return {
type: LOGOUT_USER,
payload: request
}
}
export function addToCart(_id) {
const request = axios.get(`${USER_SERVER}/addToCart?productId=${_id}`)
.then(response => response.data);
return {
type: ADD_TO_CART_USER,
payload: request
}
}
export function getCartItems(cartItems, userCart) {
const request = axios.get(`/api/product/products_by_id?id=${cartItems}&type=array`)
.then(response => {
//Make CartDetail inside Redux Store
// We need to add quantity data to Product Information that come from Product Collection.
userCart.forEach(cartItem => {
response.data.forEach((productDetail, i) => {
if (cartItem.id === productDetail._id) {
response.data[i].quantity = cartItem.quantity;
}
})
})
return response.data;
});
return {
type: GET_CART_ITEMS_USER,
payload: request
}
}
export function removeCartItem(id) {
const request = axios.get(`/api/users/removeFromCart?_id=${id}`)
.then(response => {
response.data.cart.forEach(item => {
response.data.cartDetail.forEach((k, i) => {
if (item.id === k._id) {
response.data.cartDetail[i].quantity = item.quantity
}
})
})
return response.data;
});
return {
type: REMOVE_CART_ITEM_USER,
payload: request
}
}
export function onSuccessBuy(data) {
const request = axios.post(`${USER_SERVER}/successBuy`, data)
.then(response => response.data);
return {
type: ON_SUCCESS_BUY_USER,
payload: request
}
}