Skip to content

Commit

Permalink
Merge pull request #7 from sharifmarat/removed-update-button
Browse files Browse the repository at this point in the history
Removed update button. Made checkbox responsive. Split name into first and last names
  • Loading branch information
sharifmarat authored Jul 18, 2018
2 parents 7076c06 + 75786b7 commit 74f0ce3
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 15 deletions.
12 changes: 10 additions & 2 deletions cgi-bin/vb.cgi
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,15 @@ class VolleyDB:

def updateGuest(self, id, position, is_paid):
with self.__connection:
self.__cursor.execute('update guests set position=?, is_paid=?, updated_ts=current_timestamp where id=?', (position, is_paid, id))
params = ()
query = 'update guests set '
if position is not None:
query += ' position=?, '
params += (position,)
if is_paid is not None:
query += ' is_paid=?, '
params += (is_paid,)
self.__cursor.execute(query + ' updated_ts=current_timestamp where id=?', params + (id,))
self.__connection.commit()
return self.__cursor.rowcount == 1

Expand Down Expand Up @@ -227,7 +235,7 @@ class VolleyAPI:
return self.__error('Could not set such an event as primary.')

def update_guest(self, form):
if 'id' not in form or 'position' not in form or 'is_paid' not in form:
if 'id' not in form or ('position' not in form and 'is_paid' not in form):
return self.__error('Could not update a guest, fields are missing')
with VolleyDB() as db:
if db.updateGuest(form.getfirst('id'), form.getfirst('position'), form.getfirst('is_paid') == '1'):
Expand Down
20 changes: 12 additions & 8 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@
<script type="text/template" id="players-template">
<div class="row text-left font-weight-bold">
<div class="col-2 col-md-1">#</div>
<div class="col-4 col-md-3 col-lg-4">Name</div>
<div class="col-4 col-md-3">Position</div>
<div class="col-5 col-md-3 col-lg-4">Name</div>
<div class="col-3 col-md-3">Position</div>
<div class="col-2 col-md-1 text-center">Paid</div>
<div class="col-md-4 col-lg-3 d-none d-md-block"></div>
</div>
<% _.each( players, function( player, index ) { %>
<hr />
<div class="row form-inline text-left">
<div class="col-2 col-md-1"><label><%= index + 1 %></label></div>
<div class="col-4 col-md-3 col-lg-4"><label id="guest_name_<%= player.guest_id %>"><%= player.guest_name %></label></div>
<div class="col-4 col-md-3"><input class="form-control" type="text" id="guest_position_<%= player.guest_id %>" value="<%= player.guest_position %>" /></div>
<div class="col-2 col-md-1"><input class="form-control" type="checkbox" id="guest_paid_<%= player.guest_id %>" value="" <% if (player.guest_paid == 1) { %>checked<% } %> /></div>
<div class="col-5 col-md-3 col-lg-4"><label id="guest_name_<%= player.guest_id %>"><%= player.guest_name %></label></div>
<div class="col-3 col-md-3"><input class="form-control" type="text" id="guest_position_<%= player.guest_id %>" value="<%= player.guest_position %>" /></div>
<div class="col-2 col-md-1"><input class="form-control" type="checkbox" id="guest_paid_<%= player.guest_id %>" value="" <% if (player.guest_paid == 1) { %>checked<% } %> data-id="<%= player.guest_id %>" /></div>
<div class="col-12 col-md-4 col-lg-3 text-right text-md-left">
<div class="mt-3 d-block d-md-none"></div>
<input type="button" class="btn btn-success" value="Update" onclick="update_guest(<%= player.guest_id %>)"/>
Expand All @@ -37,9 +37,9 @@
<hr class="bold" />
<div class="row form-inline">
<div class="col-2 col-md-1"><label><b>New</b></label></div>
<div class="col-4 col-md-3 col-lg-4"><input class="form-control" type="text" id="guest_name_new" value="" placeholder="Name" /></div>
<div class="col-4 col-md-3"><input class="form-control" type="text" id="guest_position_new" value="" placeholder="Position" /></div>
<div class="col-2 col-md-1"><input class="form-control" type="checkbox" disabled /></div>
<div class="col-3 col-md-2 col-lg-2"><input class="form-control" type="text" id="guest_first_name_new" value="" placeholder="First Name" /></div>
<div class="col-3 col-md-2 col-lg-2"><input class="form-control" type="text" id="guest_last_name_new" value="" placeholder="Last Name" /></div>
<div class="col-4 col-md-3 col-lg-4"><input class="form-control" type="text" id="guest_position_new" value="" placeholder="Position" /></div>
<div class="col-12 col-md-4 col-lg-3 text-right text-md-left">
<div class="mt-3 d-block d-md-none"></div>
<input type="button" class="btn btn-success" value="Add player" onclick="add_guest(get_query_value('event_id'))" />
Expand Down Expand Up @@ -80,6 +80,10 @@
$('.addeventatc .end').text(m_date.add(3, 'hours').format(parse_format));

$( "#players" ).html(template( {players: result.message.guests} ));

$(":checkbox").change(function() {
update_paid($(this));
});
}
},
error: function(response){
Expand Down
63 changes: 58 additions & 5 deletions js/vb.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,49 @@ function update_guest(guest_id) {
action_reload({
action: 'update_guest',
id: guest_id,
position: position,
is_paid: paid
position: position
});
}
}

// updates paid checkbox chkbx of a guest.
function update_paid(chkbx) {
var guest_id = chkbx.attr('data-id');
var name = $('#guest_name_' + guest_id).text();
var paid = chkbx.is(':checked') ? 1 : 0;
var paid_rollback = 1 - paid;

if (confirm('Are you sure you want to update ' + name)) {
chkbx.attr('disabled', true);

$.ajax({type: "POST",
url: apiLink,
data: {
action: 'update_guest',
id: guest_id,
is_paid: paid
},
success: function(response){
var result = JSON.parse(response);
if (result.status != 0) {
alert('error: ' + result.message);
chkbx.prop('checked', paid_rollback);
} else {
chkbx.prop('checked', paid);
}
chkbx.removeAttr('disabled');
},
error: function(response){
alert('server error');
chkbx.prop('checked', paid_rollback);
chkbx.removeAttr('disabled');
}
});
} else {
chkbx.prop('checked', paid_rollback);
}
}

function remove_guest(guest_id) {
var name = $('#guest_name_' + guest_id).text();
var p = prompt('Type yes to remove ' + name + '. This is permanent!');
Expand All @@ -52,14 +89,30 @@ function remove_guest(guest_id) {
}

function add_guest(event_id) {
var name = $('#guest_name_new').val();
var first_name = $('#guest_first_name_new').val();
var last_name = $('#guest_last_name_new').val();
var position = $('#guest_position_new').val();

if (!first_name || !$.trim(first_name)) {
alert('First name is missing');
return;
}

if (!last_name || !$.trim(last_name)) {
alert('Last name is missing');
return;
}

if (!position || !$.trim(position)) {
alert('Position is missing');
return;
}

action_reload({
action: 'add_guest',
event_id: event_id,
name: name,
position: position
name: $.trim(first_name) + ' ' + $.trim(last_name),
position: $.trim(position)
});
}

Expand Down

0 comments on commit 74f0ce3

Please sign in to comment.