-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathController.tsx
86 lines (78 loc) · 3.39 KB
/
Controller.tsx
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
import React from "react";
import axios from "axios";
import {useDispatch, useSelector} from "react-redux";
import {IconButton, Typography} from "@mui/material";
import {Close, Done} from "@mui/icons-material";
import {userActions} from "../store/user";
import {RootState} from "../store";
import {cartActions} from "../store/cart";
import {productActions} from "../store/product";
import {enqueueSnackbar} from "notistack";
const Controller = () => {
const dispatch = useDispatch();
const credential = useSelector((state: RootState) => state.userReducer.credential);
const insertedCoin = useSelector((state: RootState) => state.userReducer.userInformation!.inserted);
const cartDict = useSelector((state: RootState) => state.cartReducer.dict);
const totalPrice = useSelector((state: RootState) => state.cartReducer.totalPrice);
const completeRequestHandler = () => {
async function fetchData() {
const itemList = Object.values(cartDict);
const response = await axios.post("https://vending-machine-backend-6ov3.onrender.com/api/checkout",
{itemList},
{headers: {"Authorization": "Bearer " + credential, "Content-Type": "application/json"}})
.catch((error) => {
enqueueSnackbar(error, {variant: "error"});
});
if (response) {
const data = response!.data;
dispatch(cartActions.deleteCart());
dispatch(userActions.returnCoin(data.returnedCoin));
dispatch(productActions.requestFetch());
enqueueSnackbar(data.returnedCoin + "c Returned!", {variant: "info"});
data.returnedProduct.itemList.forEach((product: any) => {
enqueueSnackbar(product.name + " x" + product.count + " Returned!", {variant: "info"});
})
}
}
if (totalPrice === 0) {
enqueueSnackbar("What Product?", {variant: "error"});
return;
}
fetchData();
}
const cancelRequestHandler = () => {
async function fetchData() {
const response = await axios.get("https://vending-machine-backend-6ov3.onrender.com/api/user/refundCoin",
{headers: {"Authorization": "Bearer " + credential}})
.catch((error) => {
enqueueSnackbar(error, {variant: "error"});
});
if (response) {
dispatch(userActions.changeUserInformation(response!.data));
enqueueSnackbar("Coins Refunded!", {variant: "success"});
}
}
if (insertedCoin === 0) {
enqueueSnackbar("What Coin?", {variant: "error"});
return;
}
fetchData();
}
return (
<div className={"controller"}>
<div className={"py-2"}>
<Typography className={"label"}>{insertedCoin}c Inserted</Typography>
<Typography className={"label"}>{totalPrice}c Estimated Cost</Typography>
</div>
<div className={"button-wrapper bg-white"}>
<IconButton onClick={completeRequestHandler}>
<Done/>
</IconButton>
<IconButton onClick={cancelRequestHandler}>
<Close/>
</IconButton>
</div>
</div>
);
};
export default Controller;