File size: 1,915 Bytes
f5071ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React from "react";
import { makeStyles } from "@material-ui/styles";
import axios from "axios";
import { deleteFromCartUrl } from "./../urls";
import jwtToken from "./custom_hooks/GetJwtToken";
import { connect } from "react-redux";

const style = makeStyles({
	main: {
		display: "flex",
		justifyContent: "space-between",
		maxHeight: "400px",
		padding: "10px",
		width: "90%",
		marginRight: "auto !important",
		marginLeft: "auto !important",
		marginTop: "5px",
		border: "1px solid #e7e7e7",
		"& button": {
			width: "20%",
			height: "15%",
			fontSize: "85%",
			textAlign: "center",
			backgroundColor: " #f1c75c",
			borderRadius: "3px",
			border: "2.5px solid #f8b303",
		},
		"& ul": {
			listStyle: "none !important",
		},
	},
	c_img: {
		maxWidth: "25%",
		maxHeight: "200px",
	},

	body_amazon_cart: {
		width: "70%",
		maxHeight: "250px",
	},
});

function CardForCart({
	img,
	name,
	price,
	description,
	cart_id,
	setFetch,
	dispatch,
}) {
	const classes = style();
	const { main, c_img, body_amazon_cart } = classes;

	const deleteFromCart = async () => {
		try {
			const { status } = await axios.delete(deleteFromCartUrl(cart_id), {
				headers: { token: jwtToken() },
			});
			if (status === 200) {
				setFetch();
				dispatch({ type: "COUNT" });
				// alert("item deleted");
			}
		} catch (e) {
			setFetch();
			alert(e.message);
		}
	};

	return (
		<div className={main}>
			<img src={img} alt="" className={c_img} />
			<div className={body_amazon_cart}>
				<h5 style={{ padding: "5px" }}>{name}</h5>

				<p style={{ padding: "5px" }}>{description}</p>

				<ul>
					<li>
						Price : $<strong>{price}</strong>
					</li>
				</ul>

				<button onClick={deleteFromCart}>
					<i className="ion-trash-b"> Remove </i>
				</button>
			</div>
		</div>
	);
}

function mapStateToProps(dispatch) {
	return dispatch;
}

export default connect(mapStateToProps)(CardForCart);