This repository has been archived by the owner on Aug 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
product.js
94 lines (80 loc) · 2.73 KB
/
product.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
import {
addAction,
listAction,
editAction,
deleteAction
} from "commons/utils/action-creators";
import { handleError } from "utils";
import firebase, { firestore } from "firebase.js";
export const PRODUCT_LIST = listAction("product");
export const PRODUCT_ADD = addAction("product");
export const PRODUCT_EDIT = editAction("product");
export const PRODUCT_DELETE = deleteAction("product");
// ***** Plain Actions ***** //
export const productDeleteConfirm = productId => ({
type: PRODUCT_DELETE.CONFIRM,
payload: productId
});
export const productDeleteCancel = () => ({ type: PRODUCT_DELETE.CANCEL });
// ***** Thunks ***** //
export const productList = () => (dispatch, getState) => {
const { organization } = getState().profile.data;
dispatch({ type: PRODUCT_LIST.START });
return firestore
.collection("products")
.where("organization", "==", firestore.doc(organization.ref))
.where("isDeleted", "==", false)
.orderBy("category", "asc")
.orderBy("name", "asc")
.get()
.then(({ docs }) => docs.map(doc => ({ id: doc.id, ...doc.data() })))
.then(payload => dispatch({ type: PRODUCT_LIST.SUCCESS, payload }))
.catch(err => {
handleError(err);
dispatch({ type: PRODUCT_LIST.ERROR, payload: err });
});
};
export const productAdd = product => (dispatch, getState) => {
const { profile } = getState();
const values = {
...product,
organization: firestore.doc(profile.data.organization.ref),
createdAt: firebase.firestore.FieldValue.serverTimestamp(),
createdBy: firestore.doc(profile.data.ref),
isDeleted: false
};
dispatch({ type: PRODUCT_ADD.START });
return firestore
.collection("products")
.add(values)
.then(ref => ref.get())
.then(res => dispatch({ type: PRODUCT_ADD.SUCCESS, payload: res.data() }))
.catch(err => {
handleError(err); // TODO: actually handle the error
dispatch({ type: PRODUCT_ADD.ERROR, payload: err });
});
};
export const productEdit = product => dispatch => {
dispatch({ type: PRODUCT_EDIT.START });
const ref = firestore.collection("products").doc(product.id);
return ref
.update(product)
.then(() => ref.get())
.then(res => dispatch({ type: PRODUCT_EDIT.SUCCESS, payload: res.data() }))
.catch(err => {
handleError(err); // TODO: actually handle the error
dispatch({ type: PRODUCT_EDIT.ERROR, payload: err });
});
};
export const productDelete = productId => dispatch => {
dispatch({ type: PRODUCT_DELETE.START });
return firestore
.collection("products")
.doc(productId)
.update({ isDeleted: true })
.then(() => dispatch({ type: PRODUCT_DELETE.SUCCESS }))
.catch(err => {
handleError(err);
dispatch({ type: PRODUCT_DELETE.ERROR });
});
};