Skip to content

Commit 74184cd

Browse files
committed
finish productCategories
1 parent 56d04f6 commit 74184cd

12 files changed

Lines changed: 1151 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export const CATEGORIES_REQUEST = 'CATEGORIES_REQUEST';
2+
export const CATEGORIES_RECEIVE = 'CATEGORIES_RECEIVE';
3+
export const CATEGORIES_FAILURE = 'CATEGORIES_FAILURE';
4+
5+
export const CATEGORIES_SELECT = 'CATEGORIES_SELECT';
6+
export const CATEGORIES_DESELECT = 'CATEGORIES_DESELECT';
7+
8+
export const CATEGORY_UPDATE_REQUEST = 'CATEGORY_UPDATE_REQUEST';
9+
export const CATEGORY_UPDATE_SUCCESS = 'CATEGORY_UPDATE_SUCCESS';
10+
export const CATEGORY_UPDATE_FAILURE = 'CATEGORY_UPDATE_FAILURE';
11+
12+
export const CATEGORY_CREATE_SUCCESS = 'CATEGORY_CREATE_SUCCESS';
13+
export const CATEGORY_DELETE_SUCCESS = 'CATEGORY_DELETE_SUCCESS';
14+
export const CATEGORY_MOVE_UPDOWN_SUCCESS = 'CATEGORY_MOVE_UPDOWN_SUCCESS';
15+
export const CATEGORY_REPLACE_SUCCESS = 'CATEGORY_REPLACE_SUCCESS';
16+
17+
export const CATEGORY_IMAGE_UPLOAD_START = 'CATEGORY_IMAGE_UPLOAD_START';
18+
export const CATEGORY_IMAGE_UPLOAD_END = 'CATEGORY_IMAGE_UPLOAD_END';
Lines changed: 327 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,327 @@
1+
import * as t from './actionTypes';
2+
import api from 'lib/api';
3+
import messages from 'lib/text';
4+
5+
function requestCategories() {
6+
return {
7+
type: t.CATEGORIES_REQUEST
8+
};
9+
}
10+
11+
function receiveCategories(items) {
12+
return {
13+
type: t.CATEGORIES_RECEIVE,
14+
items
15+
};
16+
}
17+
18+
function receiveErrorCategories(error) {
19+
return {
20+
type: t.CATEGORIES_FAILURE,
21+
error
22+
};
23+
}
24+
25+
export function selectCategory(id) {
26+
return {
27+
type: t.CATEGORIES_SELECT,
28+
selectedId: id
29+
};
30+
}
31+
32+
export function deselectCategory() {
33+
return {
34+
type: t.CATEGORIES_DESELECT
35+
};
36+
}
37+
38+
function requestUpdateCategory(id) {
39+
return {
40+
type: t.CATEGORY_UPDATE_REQUEST
41+
};
42+
}
43+
44+
function receiveUpdateCategory() {
45+
return {
46+
type: t.CATEGORY_UPDATE_SUCCESS
47+
};
48+
}
49+
50+
function errorUpdateCategory(error) {
51+
return {
52+
type: t.CATEGORY_UPDATE_FAILURE,
53+
error
54+
};
55+
}
56+
57+
function successCreateCategory(id) {
58+
return {
59+
type: t.CATEGORY_CREATE_SUCCESS
60+
};
61+
}
62+
63+
function successDeleteCategory(id) {
64+
return {
65+
type: t.CATEGORY_DELETE_SUCCESS
66+
};
67+
}
68+
69+
function successMoveUpDownCategory(newPosition) {
70+
return {
71+
type: t.CATEGORY_MOVE_UPDOWN_SUCCESS,
72+
position: newPosition
73+
};
74+
}
75+
76+
function successReplaceCategory(newParentId) {
77+
return {
78+
type: t.CATEGORY_REPLACE_SUCCESS
79+
};
80+
}
81+
82+
function imageUploadStart() {
83+
return {
84+
type: t.CATEGORY_IMAGE_UPLOAD_START
85+
};
86+
}
87+
88+
function imageUploadEnd() {
89+
return {
90+
type: t.CATEGORY_IMAGE_UPLOAD_END
91+
};
92+
}
93+
94+
export function fetchCategories() {
95+
return dispatch => {
96+
dispatch(requestCategories());
97+
return api.productCategories
98+
.list()
99+
.then(({ status, json }) => {
100+
json.forEach((element, index, theArray) => {
101+
if (theArray[index].name === '') {
102+
theArray[index].name = `<${messages.draft}>`;
103+
}
104+
});
105+
106+
dispatch(receiveCategories(json));
107+
})
108+
.catch(error => {
109+
dispatch(receiveErrorCategories(error));
110+
});
111+
};
112+
}
113+
114+
function shouldFetchCategories(state) {
115+
const categories = state.productCategories;
116+
if (categories.isFetched || categories.isFetching) {
117+
return false;
118+
} else {
119+
return true;
120+
}
121+
}
122+
123+
export function fetchCategoriesIfNeeded() {
124+
return (dispatch, getState) => {
125+
if (shouldFetchCategories(getState())) {
126+
return dispatch(fetchCategories());
127+
}
128+
};
129+
}
130+
131+
function sendUpdateCategory(id, data) {
132+
return dispatch => {
133+
dispatch(requestUpdateCategory(id));
134+
return api.productCategories
135+
.update(id, data)
136+
.then(({ status, json }) => {
137+
dispatch(receiveUpdateCategory());
138+
dispatch(fetchCategories());
139+
})
140+
.catch(error => {
141+
dispatch(errorUpdateCategory(error));
142+
});
143+
};
144+
}
145+
146+
export function updateCategory(data) {
147+
return (dispatch, getState) => {
148+
return dispatch(sendUpdateCategory(data.id, data));
149+
};
150+
}
151+
152+
export function createCategory() {
153+
return (dispatch, getState) => {
154+
return api.productCategories
155+
.create({ enabled: false })
156+
.then(({ status, json }) => {
157+
dispatch(successCreateCategory(json.id));
158+
dispatch(fetchCategories());
159+
dispatch(selectCategory(json.id));
160+
})
161+
.catch(error => {
162+
//dispatch error
163+
console.log(error);
164+
});
165+
};
166+
}
167+
168+
export function deleteImage() {
169+
return (dispatch, getState) => {
170+
const state = getState();
171+
const categoryId = state.productCategories.selectedId;
172+
173+
return api.productCategories
174+
.deleteImage(categoryId)
175+
.then(({ status, json }) => {
176+
if (status === 200) {
177+
dispatch(fetchCategories());
178+
} else {
179+
throw status;
180+
}
181+
})
182+
.catch(error => {
183+
//dispatch error
184+
console.log(error);
185+
});
186+
};
187+
}
188+
189+
export function deleteCategory(id) {
190+
return (dispatch, getState) => {
191+
return api.productCategories
192+
.delete(id)
193+
.then(({ status, json }) => {
194+
if (status === 200) {
195+
dispatch(successDeleteCategory(id));
196+
dispatch(deselectCategory());
197+
dispatch(fetchCategories());
198+
} else {
199+
throw status;
200+
}
201+
})
202+
.catch(error => {
203+
//dispatch error
204+
console.log(error);
205+
});
206+
};
207+
}
208+
209+
function moveCategory(allCategories = [], selectedCategory, isUp = true) {
210+
return new Promise((resolve, reject) => {
211+
if (isUp) {
212+
allCategories = allCategories
213+
.filter(
214+
e =>
215+
e.parent_id === selectedCategory.parent_id &&
216+
e.id !== selectedCategory.id &&
217+
e.position < selectedCategory.position
218+
)
219+
.sort((a, b) => b.position - a.position);
220+
} else {
221+
allCategories = allCategories
222+
.filter(
223+
e =>
224+
e.parent_id === selectedCategory.parent_id &&
225+
e.id !== selectedCategory.id &&
226+
e.position > selectedCategory.position
227+
)
228+
.sort((a, b) => a.position - b.position);
229+
}
230+
231+
if (allCategories.length > 0) {
232+
let targetCategory = allCategories[0];
233+
let newPosition = targetCategory.position;
234+
235+
api.productCategories
236+
.update(selectedCategory.id, { position: targetCategory.position })
237+
.then(() => {
238+
api.productCategories
239+
.update(targetCategory.id, { position: selectedCategory.position })
240+
.then(() => {
241+
resolve(newPosition);
242+
})
243+
.catch(err => {
244+
reject(err);
245+
});
246+
})
247+
.catch(err => {
248+
reject(err);
249+
});
250+
}
251+
});
252+
}
253+
254+
export function moveUpCategory() {
255+
return (dispatch, getState) => {
256+
let state = getState();
257+
var allCategories = state.productCategories.items;
258+
var selectedCategory = allCategories.find(
259+
item => item.id === state.productCategories.selectedId
260+
);
261+
262+
var isUp = true;
263+
264+
return moveCategory(allCategories, selectedCategory, isUp).then(
265+
newPosition => {
266+
dispatch(successMoveUpDownCategory(newPosition));
267+
dispatch(fetchCategories());
268+
}
269+
);
270+
};
271+
}
272+
273+
export function moveDownCategory() {
274+
return (dispatch, getState) => {
275+
let state = getState();
276+
var allCategories = state.productCategories.items;
277+
var selectedCategory = allCategories.find(
278+
item => item.id === state.productCategories.selectedId
279+
);
280+
var isUp = false;
281+
282+
return moveCategory(allCategories, selectedCategory, isUp).then(
283+
newPosition => {
284+
dispatch(successMoveUpDownCategory(newPosition));
285+
dispatch(fetchCategories());
286+
}
287+
);
288+
};
289+
}
290+
291+
export function replaceCategory(parentId) {
292+
return (dispatch, getState) => {
293+
let state = getState();
294+
var selectedCategory = state.productCategories.items.find(
295+
item => item.id === state.productCategories.selectedId
296+
);
297+
298+
return api.productCategories
299+
.update(selectedCategory.id, { parent_id: parentId })
300+
.then(({ status, json }) => {
301+
dispatch(successReplaceCategory());
302+
dispatch(fetchCategories());
303+
})
304+
.catch(error => {
305+
//dispatch error
306+
console.log(error);
307+
});
308+
};
309+
}
310+
311+
export function uploadImage(form) {
312+
return (dispatch, getState) => {
313+
const state = getState();
314+
const categoryId = state.productCategories.selectedId;
315+
316+
dispatch(imageUploadStart());
317+
return api.productCategories
318+
.uploadImage(categoryId, form)
319+
.then(() => {
320+
dispatch(imageUploadEnd());
321+
dispatch(fetchCategories());
322+
})
323+
.catch(error => {
324+
dispatch(imageUploadEnd());
325+
});
326+
};
327+
}

0 commit comments

Comments
 (0)