-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart-action.js
63 lines (60 loc) · 1.92 KB
/
cart-action.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
import { cartActions } from "./cart-slice";
import { uiActions } from "./ui-slice";
export const fetchCartData = () => {
return (dispatch) => {
fetch("https://test-e9746-default-rtdb.firebaseio.com/cart.json")
.then((res) => {
if (!res.ok) throw new Error("Fetching cart data failed.");
return res.json();
})
.then((data) => {
dispatch(cartActions.replaceCart(data));
})
.catch((err) =>
dispatch(
uiActions.showNotification({
status: "error",
title: "Error!",
message: err.message,
})
)
);
};
};
export const sendCartData = (cart) => {
return (dispatch) => {
dispatch(
uiActions.showNotification({
status: "pending",
title: "Sending...",
message: "Sending Cart Data...",
})
);
fetch("https://test-e9746-default-rtdb.firebaseio.com/cart.json", {
method: "PUT",
body: JSON.stringify(cart),
})
.then((res) => {
if (!res.ok) throw new Error("Sending cart data failed.");
return res.json();
})
.then((data) =>
dispatch(
uiActions.showNotification({
status: "success",
title: "Success!",
message: "Sent cart data Successfully!",
})
)
)
.catch((err) =>
dispatch(
uiActions.showNotification({
status: "error",
title: "Error!",
message: err.message,
})
)
);
};
};