-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditbob.js
117 lines (102 loc) · 3.18 KB
/
editbob.js
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
* Runs when the document has loaded
* $ -> jQuery shorthand for vanilla JS "document.ready()"
*/
$(function() {
// Get bob id from url (last parameter)
url = window.location.href.split('/');
bobid = url[4];
$('#start-date').datepicker();
$('#end-date').datepicker();
$('#start-date').datepicker('setDate', new Date());
createAndPrefillForm(bobid);
/**
* On clicking submit button
* Submits the Bob data created from reading values from the form fields
*/
$('#add-bob-form').submit(function(event) {
event.preventDefault();
let $form = $(this)
let tags = [];
$form.find('[name="tags"]:checked').each(function() {
tags.push($(this).val());
});
let bobData = {};
$form.find('#data input').each(function() {
let $input = $(this);
bobData[$input.attr('name')] = $input.val();
});
// Create bob data from the values of each input form
let data = {
id: $form.find('#bob-id').val(),
data: bobData,
startDate: $form.find('#start-date').val(),
endDate: $form.find('#end-date').val(),
'tags[]': tags
}
// Send the bob through the api with a PUT request. (ajax is required to create PUT requests)
$.ajax({
url: '/api/bobs/' + bobid,
type: 'PUT',
data: data,
success: function(res) {
alert(res);
// Redirect to previous page or FUTUREboard
if (document.referrer !== "") {
window.location = document.referrer;
} else {
window.location = "/";
}
},
error: function(res) {
alert(res.statusText);
}
});
});
});
/**
* Populates input forms with current bob values
*/
function createAndPrefillForm(bobid) {
if (bobid.length !== 24){
throw("bobid not valid");
}
/**
* Loads Bob Object from the server using a HTTP GET request,
* @param {string} url - A string containing the route to Bob GET request
* @param {successCallback} function - Fills elements with Bob data values.
*/
$.get('/api/bobs/' + bobid, function (bob) {
console.log(bob);
let $form = $('add-bob-form');
// Populate fields from the current bob values
$('#bob-id').val(bob._id);
$('#flavor').val(bob.flavor);
$('#start-date').val(bob.startDate);
$('#end-date').val(bob.endDate);
// Create the correct fields based on the bob's flavor
$.get('/api/flavors/' + bob.flavor, function (flavor) {
$.each(flavor.fields, function(i, field) {
console.log(bob.data[field.name]);
$('#data')
.append($('<label>', {for: field.name, text: field.name, class: "mui--text-title"}))
.append($('<div>', {class: "data-field mui-" + field.input + "field"})
.append($('<input>', {id: field.name, name: field.name, type: field.input, val: bob.data[field.name]}))
);
});
});
// Create tag checkboxes and prefill them
$.get('/api/tags', function(tagArray) {
let checked = false;
$.each(tagArray, function(index, tag) {
// checked = bob.tags includes current tag
checked = bob.tags.indexOf(tag.title) !== -1;
// Add checkbox and field, with pre-checked values
$('#tag-holder').append('<label>\
<input type="checkbox" name="tags" value="' + tag.title + '"' + (checked?' checked="true" ':'') +'></input>' + tag.title + '\
</label><br>');
});
});
});
}
console.log("controller.js running");