Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Implement adding a wellness rule #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 73 additions & 28 deletions landingpage/static/js/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,17 @@
$( document ).ready(function() {
$(".button-collapse").sideNav();



$('form#ajax_form').submit(function (e) {
$('#wellness_rule_form').submit(function(){
event.preventDefault();
console.log("form submitted!");

var upc_code = $('input[name="upc_code"]').val();
var csrf_token = $("input[name='csrfmiddlewaretoken']").val()
// console.log("Wellness!");
var data = $(this).serialize();
// console.log(data);


var post_data = { 'upc_code': upc_code,
'csrfmiddlewaretoken': csrf_token,
};

console.log( post_data );

create_post(post_data);
return false;

});

function create_post( post_data ) {
console.log("create post is working!") // sanity check
$.ajax({
url : "upc/", // the endpoint
type : "POST", // http method
traditional: true,
data : post_data , // data sent with the post request
url : "wellness_rule/",
type : "POST",
// traditional: true,
data : post_data ,

// handle a successful response
success : function(data) {
Expand All @@ -40,7 +23,7 @@ $( document ).ready(function() {
$('div[name="request_status"]').remove();
// if ok == true
if (data['status'] == true){
$('form#ajax_form').parent().parent().append(' \
$('form#upc_ajax_form').parent().parent().append(' \
<div name="request_status" class="divider"></div> \
<div class="section"> \
<h4> UPC Requested </h4> \
Expand All @@ -52,7 +35,7 @@ $( document ).ready(function() {

}
else {
$('form#ajax_form').parent().parent().append('\
$('form#upc_ajax_form').parent().parent().append('\
<div name="request_status" class="divider"></div> \
<div class="section"> \
<h4> UPC Not Found (maybe too many requests) </h4> \
Expand All @@ -65,6 +48,68 @@ $( document ).ready(function() {
console.log(request.responseText);
}
});
}
});


$('form#upc_ajax_form').submit(function (e) {
event.preventDefault();
console.log("form submitted!");

var upc_code = $('input[name="upc_code"]').val();
var csrf_token = $("input[name='csrfmiddlewaretoken']").val()


var post_data = { 'upc_code': upc_code,
'csrfmiddlewaretoken': csrf_token,
};

console.log( post_data );

create_post(post_data);
return false;

});

function create_post( post_data ) {
console.log("create post is working!") // sanity check
$.ajax({
url : "upc/", // the endpoint
type : "POST", // http method
traditional: true,
data : post_data , // data sent with the post request

// handle a successful response
success : function(data) {
console.log("success"); // another sanity check
console.log(data);
$('div[name="request_status"]').remove();
// if ok == true
if (data['status'] == true){
$('form#upc_ajax_form').parent().parent().append(' \
<div name="request_status" class="divider"></div> \
<div class="section"> \
<h4> UPC Requested </h4> \
<p class="flow-text"> \
Item Name: ' + data['gtin_name'] + '<br>' +
'UPC Code: ' + data['gtin_code'] +
'</p> </div> ');


}
else {
$('form#upc_ajax_form').parent().parent().append('\
<div name="request_status" class="divider"></div> \
<div class="section"> \
<h4> UPC Not Found (maybe too many requests) </h4> \
<p class="flow-text"> \
UPC Code: ' + data['gtin_code'] + "</p> </div>");
}

},
error : function (request, status, error) {
console.log(request.responseText);
}
});
}

});
26 changes: 20 additions & 6 deletions landingpage/templates/landing_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,20 @@ <h4> What is this Project? </h4>

<h5>Request a UPC</h5>


<div class="row">
<form id='ajax_form' class="col s12"> {% csrf_token %}
<form id='upc_ajax_form' class="col s12"> {% csrf_token %}
<div class="input-field col">

<i class="material-icons prefix">playlist_add</i>
<input name="upc_code" type="text" pattern="\d*" class="validate">
<label for="upc_code">UPC Numeric Code</label>
<button id='ajax_form' class="btn waves-effect waves-light" type="submit" name="action">Submit
<button id='upc_ajax_form' class="btn waves-effect waves-light" type="submit" name="action">Submit
<i class="material-icons right">send</i>
</button>

</div>
</form>
</div>



</div>


Expand Down Expand Up @@ -97,8 +93,26 @@ <h3>Your Products List</h3>


</div>
<div class="divider"></div>
<div class="section">
<h5>Create a new Wellness Rule</h5>

<div class="row">
<form id='wellness_rule_form' class="col s12"> {% csrf_token %}
{% include 'partials/_input.html' with name="wellness_rule[category_id]" label="Category Id" %}

{% include 'partials/_input.html' with name="wellness_rule[nutrient]" label="Nutrient" %}

{% include 'partials/_input.html' with name="wellness_rule[nutritional_field]" label="Nutritional Field" %}

{% include 'partials/_input.html' with name="wellness_rule[rule_type]" label="Rule Type" %}

<button id='ajax_form' class="btn waves-effect waves-light" type="submit" name="action">Submit
<i class="material-icons right">send</i>
</button>
</form>
</div>
</div>
</body>

{% endblock content %}
Expand Down
4 changes: 4 additions & 0 deletions landingpage/templates/partials/_input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div class="input-field col">
<input name="{{ name }}" type="text" class="validate">
<label for="{{ name }}">{{ label }}</label>
</div>
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ dj-database-url==0.4.0
Django==1.9.4
django-bower==5.1.0
django-dotenv==1.4.1
django-filter==0.12.0
django-filter==0.13.0
djangorestframework==3.3.2
gunicorn==19.4.5
Markdown==2.6.5
Expand Down