Skip to content

Instantly share code, notes, and snippets.

@gje4
Created April 22, 2019 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gje4/4f550c8d5b0238129d4b1d0d66560ffe to your computer and use it in GitHub Desktop.
Save gje4/4f550c8d5b0238129d4b1d0d66560ffe to your computer and use it in GitHub Desktop.
import React, { Component } from "react";
import PropTypes from "prop-types";
import Link from "next/link";
import Title from "./styles/Title";
import ItemStyles from "./styles/ItemStyles";
import PriceTag from "./styles/PriceTag";
import formatMoney from "../lib/formatMoney";
import AddToCart from "./AddToCart";
import styled from "styled-components";
const { createClient } = require("@moltin/request");
const client = new createClient({
client_id: "CsuvAhjJzuWme6u1mrihA4Hef3xOSSJ9DLMfBqQ1Ez"
});
const StyledLink = styled(Link)`
color: #f44245;
background: #f44245;
`;
export default class Item extends Component {
static propTypes = {
item: PropTypes.object.isRequired
};
constructor(props) {
super(props);
// This binding is necessary to make `this` work in the callback
this.handleClick = this.handleClick.bind(this);
}
async handleClick() {
//TODO create cart id
const quantity = 1;
const cartId = localStorage.getItem("moltinCartId");
console.log("add item", "carts/" + cartId + "/items");
const cart = await client
.post("carts/" + cartId + "/items", {
type: "cart_item",
id: this.props.id,
quantity
})
.then(console.log)
.catch(console.error);
}
render() {
const style = {
color: "red"
};
const { item } = this.props;
return (
<ItemStyles>
<div className="hvrbox">
{item.image && <img src={item.image} alt={item.name} />}
<Title>
<Link
href={{
pathname: "/item",
query: { id: item.id }
}}
>
<p>{item.name}</p>
</Link>
<p>{formatMoney(item.price[0].amount)}</p>
</Title>
<div className="hvrbox-layer_top">
<div className="hvrbox-text-quickview">
<Link
href={{
pathname: "/products",
query: { id: item.id }
}}
>
<button>View</button>
</Link>
</div>
<div className="hvrbox-text-buynow">
<a>
<span
className="moltin-cart-button"
data-moltin-product-id={item.id}
data-moltin-text="Buy Now"
/>
</a>
</div>
</div>
</div>
</ItemStyles>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment