Skip to content

Commit

Permalink
Create m4-p6-s1-clear.html
Browse files Browse the repository at this point in the history
  • Loading branch information
divanov11 authored Apr 24, 2020
1 parent 9ceb69f commit ab7105e
Showing 1 changed file with 159 additions and 0 deletions.
159 changes: 159 additions & 0 deletions m4-p6-s1-clear.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
{% extends 'store/main.html' %}
{% load static %}
{% block content %}
<div class="row">
<div class="col-lg-6">
<div class="box-element" id="form-wrapper">
<form id="form">
<div id="user-info">
<div class="form-field">
<input required class="form-control" type="text" name="name" placeholder="Name..">
</div>
<div class="form-field">
<input required class="form-control" type="email" name="email" placeholder="Email..">
</div>
</div>

<div id="shipping-info">
<hr>
<p>Shipping Information:</p>
<hr>
<div class="form-field">
<input class="form-control" type="text" name="address" placeholder="Address..">
</div>
<div class="form-field">
<input class="form-control" type="text" name="city" placeholder="City..">
</div>
<div class="form-field">
<input class="form-control" type="text" name="state" placeholder="State..">
</div>
<div class="form-field">
<input class="form-control" type="text" name="zipcode" placeholder="Zip code..">
</div>
<div class="form-field">
<input class="form-control" type="text" name="country" placeholder="Zip code..">
</div>
</div>

<hr>
<input id="form-button" class="btn btn-success btn-block" type="submit" value="Continue">
</form>
</div>

<br>
<div class="box-element hidden" id="payment-info">
<small>Paypal Options</small>
<button id="make-payment">Make payment</button>
</div>

</div>

<div class="col-lg-6">
<div class="box-element">
<a class="btn btn-outline-dark" href="{% url 'cart' %}">&#x2190; Back to Cart</a>
<hr>
<h3>Order Summary</h3>
<hr>
{% for item in items %}
<div class="cart-row">
<div style="flex:2"><img class="row-image" src="{{item.product.imageURL}}"></div>
<div style="flex:2"><p>{{item.product.name}}</p></div>
<div style="flex:1"><p>${{item.product.price|floatformat:2}}</p></div>
<div style="flex:1"><p>x{{item.quantity}}</p></div>
</div>
{% endfor %}
<h5>Items: {{order.get_cart_items}}</h5>
<h5>Total: ${{order.get_cart_total|floatformat:2}}</h5>
</div>
</div>
</div>

<script type="text/javascript">
var shipping = '{{order.shipping}}'
var total = '{{order.get_cart_total|floatformat:2}}'

if (shipping == 'False'){
document.getElementById('shipping-info').innerHTML = ''
}

if (user != 'AnonymousUser'){
document.getElementById('user-info').innerHTML = ''
}

if (shipping == 'False' && user != 'AnonymousUser'){
//Hide entire form if user is logged in and shipping is false
document.getElementById('form-wrapper').classList.add("hidden");
//Show payment if logged in user wants to buy an item that does not require shipping
document.getElementById('payment-info').classList.remove("hidden");
}

var form = document.getElementById('form')
form.addEventListener('submit', function(e){
e.preventDefault()
console.log('Form Submitted...')
document.getElementById('form-button').classList.add("hidden");
document.getElementById('payment-info').classList.remove("hidden");
})

document.getElementById('make-payment').addEventListener('click', function(e){
submitFormData()
})

function submitFormData(){
console.log('Payment button clicked')

var userFormData = {
'name':null,
'email':null,
'total':total,
}

var shippingInfo = {
'address':null,
'city':null,
'state':null,
'zipcode':null,
}

if (shipping != 'False'){
shippingInfo.address = form.address.value
shippingInfo.city = form.city.value
shippingInfo.state = form.state.value
shippingInfo.zipcode = form.zipcode.value
}

if (user == 'AnonymousUser'){
userFormData.name = form.name.value
userFormData.email = form.email.value
}

console.log('Shipping Info:', shippingInfo)
console.log('User Info:', userFormData)

var url = "/process_order/"
fetch(url, {
method:'POST',
headers:{
'Content-Type':'applicaiton/json',
'X-CSRFToken':csrftoken,
},
body:JSON.stringify({'form':userFormData, 'shipping':shippingInfo}),

})
.then((response) => response.json())
.then((data) => {
console.log('Success:', data);
alert('Transaction completed');

cart = {}
document.cookie ='cart=' + JSON.stringify(cart) + ";domain=;path=/"

window.location.href = "{% url 'store' %}"

})
}
</script>
{% endblock content %}



0 comments on commit ab7105e

Please sign in to comment.