-
Notifications
You must be signed in to change notification settings - Fork 0
/
customer.js
175 lines (159 loc) · 4.66 KB
/
customer.js
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import axios from 'axios';
import { apiHost } from '../config';
export const LISTING_CUSTOMERS = 'LISTING_CUSTOMERS';
export const LIST_CUSTOMERS_SUCCESS = 'LIST_CUSTOMERS_SUCCESS';
export const LIST_CUSTOMERS_FAILED = 'LIST_CUSTOMERS_FAILED';
export const FETCHING_CUSTOMER = 'FETCHING_CUSTOMER';
export const FETCH_CUSTOMER_SUCCESS = 'FETCH_CUSTOMER_SUCCESS';
export const FETCH_CUSTOMER_FAILED = 'FETCH_CUSTOMER_FAILED';
export const UPDATING_CUSTOMER = 'UPDATING_CUSTOMER';
export const UPDATE_CUSTOMER_SUCCESS = 'UPDATE_CUSTOMER_SUCCESS';
export const UPDATE_CUSTOMER_FAILED = 'UPDATE_CUSTOMER_FAILED';
export const CLEAR_TOAST = 'CLEAR_TOAST';
export const initialState = {
customers: [],
currentCustomer: { id: '', CustomerNotes: [] },
loading: false,
toastMessage: null,
};
export default (state = initialState, action) => {
switch (action.type) {
case LISTING_CUSTOMERS:
return {
...state,
loading: true,
};
case LIST_CUSTOMERS_SUCCESS:
return {
...state,
customers: action.payload,
loading: false,
};
case LIST_CUSTOMERS_FAILED:
return {
...state,
loading: false,
toastMessage: { type: 'error', msg: 'failed to list customers' },
};
case FETCHING_CUSTOMER:
return {
...state,
loading: true,
};
case FETCH_CUSTOMER_SUCCESS:
return {
...state,
currentCustomer: action.payload,
loading: false,
};
case FETCH_CUSTOMER_FAILED:
return {
...state,
loading: false,
toastMessage: { type: 'error', msg: 'failed to get customer detail' },
};
case UPDATING_CUSTOMER:
return {
...state,
loading: true,
};
case UPDATE_CUSTOMER_SUCCESS:
return {
...state,
loading: false,
currentCustomer: action.payload,
toastMessage: { type: 'success', msg: 'successfully updated' },
};
case UPDATE_CUSTOMER_FAILED:
return {
...state,
loading: false,
toastMessage: { type: 'error', msg: 'failed to update' },
};
case CLEAR_TOAST:
return {
...state,
toastMessage: null,
};
default:
return state;
}
};
export const listCustomers = () => {
return async dispatch => {
dispatch({
type: LISTING_CUSTOMERS,
});
try {
const response = await axios.get(`${apiHost}/customers`);
dispatch({
type: LIST_CUSTOMERS_SUCCESS,
payload: response.data,
});
} catch (e) {
// console.log(e);
dispatch({
type: LIST_CUSTOMERS_FAILED,
});
} finally {
setTimeout(() => {
dispatch({
type: CLEAR_TOAST,
});
}, 1500);
}
};
};
export const fetchCustomer = id => {
return async dispatch => {
dispatch({
type: FETCHING_CUSTOMER,
});
try {
const response = await axios.get(`${apiHost}/customer/${id}`);
dispatch({
type: FETCH_CUSTOMER_SUCCESS,
payload: response.data,
});
} catch (e) {
// console.log(e);
dispatch({
type: FETCH_CUSTOMER_FAILED,
});
} finally {
setTimeout(() => {
dispatch({
type: CLEAR_TOAST,
});
}, 1500);
}
};
};
export const updateCustomer = (id, status, notes) => {
return async dispatch => {
dispatch({
type: UPDATING_CUSTOMER,
});
try {
const response = await axios.post(`${apiHost}/customer/${id}`, {
status,
notes,
});
dispatch({
type: UPDATE_CUSTOMER_SUCCESS,
payload: response.data,
});
} catch (e) {
// console.log(e);
dispatch({
type: UPDATE_CUSTOMER_FAILED,
});
} finally {
setTimeout(() => {
dispatch({
type: CLEAR_TOAST,
});
}, 1500);
}
};
};