event based table join system

This commit is contained in:
2012-12-02 19:53:19 +01:00
parent 926be8ec48
commit e3dc6a7c68
8 changed files with 131 additions and 76 deletions
+36 -23
View File
@@ -15,22 +15,27 @@ class Quser
if(e.event == 'list_closed') if(e.event == 'list_closed')
#redirect_to 'user_root', {list_closed: 'true'} #redirect_to 'user_root', {list_closed: 'true'}
redirect_to 'history_list', {list_id: e.data.id, list_closed: true} redirect_to 'history_list', {list_id: e.data.id, list_closed: true}
if(e.event == 'list_helped') else if(e.event == 'list_helped')
window.active_list.needs_help = false window.active_list.needs_help = false
@list_needs_help_default_action() @list_needs_help_default_action()
if(e.event == 'order_being_processed') else if(e.event == 'order_being_processed')
$('#order-row-'+e.data.id).addClass('active') $('#order-row-'+e.data.id).addClass('active')
if(e.event == 'order_being_delivered') else if(e.event == 'order_being_delivered')
$('#order-row-'+e.data.id).addClass('delivered') $('#order-row-'+e.data.id).addClass('delivered')
if(e.event == 'list_changed_table') else if(e.event == 'list_changed_table')
$('.table-number').text(e.data.table.number) $('.table-number').text(e.data.table.number)
if(e.event == 'list_needs_help') else if(e.event == 'list_needs_help')
window.active_list.needs_help = true window.active_list.needs_help = true
@list_needs_help_default_action() @list_needs_help_default_action()
if(e.event == 'list_needs_payment') else if(e.event == 'list_needs_payment')
window.active_list.needs_payment = true window.active_list.needs_payment = true
@list_needs_payment_default_action() @list_needs_payment_default_action()
else if(e.event == 'user_join_request')
@show_join_request(e.data)
else if(e.event == 'join_request_approved')
redirect_to 'list_products', message: 'join_request_approved'
else if(e.event == 'join_request_rejected')
redirect_to 'user_root', message: 'join_request_rejected'
console.log(e) console.log(e)
false false
home_loader: -> home_loader: ->
@@ -61,9 +66,26 @@ class Quser
@list_needs_help_default_action(response) @list_needs_help_default_action(response)
# join_request_active is to ensure that there are no more modals loaded when one is still active # join_request_active is to ensure that there are no more modals loaded when one is still active
# console.log('join_request_active=' + window.join_request_active) # console.log('join_request_active=' + window.join_request_active)
@handle_join_requests(response)
handle_join_requests: (response)->
if !window.join_request_active && response.join_requests && response.join_requests.length if !window.join_request_active && response.join_requests && response.join_requests.length
window.join_request_active = true window.join_request_active = true
for join_request in response.join_requests for join_request in response.join_requests
@show_join_request(join_request)
show_join_request: (join_request)->
$(@mustache('#join-request-template',
title: t('join_request.title')
message: t('join_request.body', {email: join_request.user_email})
reject: t('join_request.reject')
approve: t('join_request.approve')
requester_id: join_request.user_id
)).modal()
false
approve_join_request: (user_id)->
$.post(data_host + '/user/approve_join_request', $.extend({user_id: user_id}, authentication_object), -> window.join_request_active = false)
reject_join_request: (user_id)->
$.post(data_host + '/user/reject_join_request', $.extend({user_id: user_id}, authentication_object), -> window.join_request_active = false)
bogus1: ->
wrapper = $('<div class="modal"></div>') wrapper = $('<div class="modal"></div>')
join_callback = ( (request)-> join_callback = ( (request)->
-> ->
@@ -349,12 +371,13 @@ class Quser
#$.post(data_host + '/user/create_list.json', {table_id: table.table_id}, (res)-> Quser.handle_response(res)) #$.post(data_host + '/user/create_list.json', {table_id: table.table_id}, (res)-> Quser.handle_response(res))
redirect_to 'list_products_for_table', {table_id: table.table_id} redirect_to 'list_products_for_table', {table_id: table.table_id}
, 'json') , 'json')
show_table_products: ->
unless table_id = getUrlVars().table_id
redirect_to 'user_root', {message: 'cannot_identify_table'}
return
redirect_to 'list_products_for_table', table_id: table_id
join_occupied_table: () -> join_occupied_table: () ->
match = window.document.URL.toString().match('table_id=([0-9a-zA-Z]+)') unless table_id = getUrlVars().table_id
if match
table_id = match[1]
else
redirect_to 'user_root', {message: 'cannot_identify_table'} redirect_to 'user_root', {message: 'cannot_identify_table'}
return return
$('.form-actions').remove() $('.form-actions').remove()
@@ -363,17 +386,7 @@ class Quser
cont.append $('<img src="/assets/spinner.gif" />') cont.append $('<img src="/assets/spinner.gif" />')
cont.append $('<p>Waiting for approval of the person on this table</p>') cont.append $('<p>Waiting for approval of the person on this table</p>')
$.post(data_host + '/user/join_occupied_table', $.extend({table_id: table_id}, authentication_object)) $.post(data_host + '/user/join_occupied_table', $.extend({table_id: table_id}, authentication_object))
setInterval('Quser.check_if_can_join_occupied_table("'+table_id+'")', 7500) #setInterval('Quser.check_if_can_join_occupied_table("'+table_id+'")', 7500)
check_if_can_join_occupied_table: (table_id)->
$.post(data_host + '/user/check_table_join_status', $.extend({table_id: table_id}, authentication_object), (res) ->
res ||= {}
if res.approved
redirect_to 'list_products'
else if res.waiting
# do nothing, keep waiting....
else
redirect_to 'user_root', {message: 'join_request_rejected'}
)
add_product: (product_id, count) -> add_product: (product_id, count) ->
count ||= 1 count ||= 1
window.active_products_list ||= {} window.active_products_list ||= {}
+7 -20
View File
@@ -78,6 +78,7 @@ class UserController < ApplicationController
# GET /suppliers/1/product_list # GET /suppliers/1/product_list
# GET /suppliers/1/product_list.json # GET /suppliers/1/product_list.json
def list_products def list_products
redirect_to(user_root_path(message: 'the_list_has_been_closed')) and return unless list
@supplier = list.supplier @supplier = list.supplier
respond_to do |format| respond_to do |format|
format.html do format.html do
@@ -113,44 +114,30 @@ class UserController < ApplicationController
end end
end end
# GET /user/join_occupied_table
def join_occupied_table def join_occupied_table
redirect_to user_root_path(message: 'table_not_found') and return unless params[:table_id].present?
@table = Table.find(params[:table_id]) @table = Table.find(params[:table_id])
end end
# POST /user/join_occupied_table
def request_to_join_occupied_table def request_to_join_occupied_table
@table = Table.find(params[:table_id]) @table = Table.find(params[:table_id])
if @list = @table.active_list if @list = @table.active_list
unless @list.join_requests.include?(current_user.id) @list.send_table_join_request_for_user! current_user
@list.join_requests << current_user.id
@list.is_dirty
@list.save
end
end end
render nothing: true render nothing: true
end end
def reject_join_request def reject_join_request
return unless params[:user_id] return unless params[:user_id]
if list && list.join_requests.include?(params[:user_id]) list && list.reject_join_request_for_user!(params[:user_id])
list.join_requests.delete(params[:user_id])
list.is_dirty
list.save
end
render js: '' render js: ''
end end
def approve_join_request def approve_join_request
return unless params[:user_id] return unless params[:user_id]
@user = User.find(params[:user_id]) @user = User.find(params[:user_id])
if list && list.join_requests.include?(params[:user_id]) list && list.approve_join_request_for_user!(@user)
list.join_requests.delete(params[:user_id])
@user.active_list_id = list.id
list.add_user(@user)
@user.save
list.is_dirty
list.save
end
render nothing: true render nothing: true
end end
+45
View File
@@ -148,6 +148,51 @@ class List
end end
end end
def approve_join_request_for_user!(user)
if join_requests.include?(user.id)
join_requests.delete(user.id)
user.active_list_id = self.id
add_user(user)
user.save
self.is_dirty
binding.pry
if save
broadcast_user user.id, 'join_request_approved'
end
end
end
def unlink_user(user)
changed = join_requests.delete(user.id)
changed ||= user_ids.delete(user.id)
if user.active_list_id == id
user.active_list_id = nil
user.save
end
save if changed
end
def send_table_join_request_for_user!(requester)
unless join_requests.include?(requester.id)
self.join_requests << requester.id
self.is_dirty
if save
for user in users
broadcast_user user.id, 'user_join_request', list_id: id, user_email: requester.email, user_id: requester.id
end
end
end
end
def reject_join_request_for_user!(user_id)
if join_requests.include?(user_id)
join_requests.delete(user_id)
self.is_dirty
if save
broadcast_user user_id, 'join_request_rejected'
end
end
end
def set_price def set_price
list_total = 0.0 list_total = 0.0
+13
View File
@@ -0,0 +1,13 @@
<div class="modal hide fade" role="dialog" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h3>{{title}}</h3>
</div>
<div class="modal-body">
<p>{{message}}</p>
</div>
<div class="modal-footer">
<a href="#" class="btn" onclick="Quser.reject_join_request('{{requester_id}}')" data-dismiss="modal" aria-hidden="true">{{reject}}</a>
<a href="#" class="btn btn-primary" onclick="Quser.approve_join_request('{{requester_id}}')" data-dismiss="modal" aria-hidden="true">{{approve}}</a>
</div>
</div>
+1
View File
@@ -77,6 +77,7 @@ html lang="en"
= yield = yield
= javascript_include_tag "user/application" = javascript_include_tag "user/application"
script#alert-template[type="text/html"]= render 'user/alert.mustache' script#alert-template[type="text/html"]= render 'user/alert.mustache'
script#join-request-template[type="text/html"]= render 'user/join_request.mustache'
= yield :footer = yield :footer
javascript: javascript:
jQuery(function(){#{onload_javascript}}); jQuery(function(){#{onload_javascript}});
+2 -10
View File
@@ -3,16 +3,8 @@
.form-actions .form-actions
= link_to t('user.join_occupied_table.back'), user_root_path, class: :btn = link_to t('user.join_occupied_table.back'), user_root_path, class: :btn
' '
= link_to t('user.join_occupied_table.show_the_products'), user_root_path(message: 'cannot_identify_table'), class: [:btn, 'btn-primary'], id: 'join_table_products' button.btn.btn-primary onclick="Quser.show_table_products()" = t('user.join_occupied_table.show_the_products')
' '
button.btn.btn-warning{onClick="Quser.join_occupied_table()"} = t('user.join_occupied_table.join_this_table') button.btn.btn-warning{onClick="Quser.join_occupied_table()"} = t('user.join_occupied_table.join_this_table')
#join-occupied-table-progress-container #join-occupied-table-progress-container
- content_for :footer do - onload_javascript 'Quser.watch_events()'
javascript:
$(function(){
var list_products_for_table_base = '#{raw user_list_products_for_table_path}';
var match = window.document.URL.toString().match('table_id=([0-9a-zA-Z]+)');
if(match){
$('#join_table_products').attr('href', list_products_for_table_base + '?table_id=' + match[1]);
}
});
+2
View File
@@ -22,11 +22,13 @@ en:
order_is_placed: Your order has been received in good order order_is_placed: Your order has been received in good order
the_list_has_been_closed: The list has been closed the_list_has_been_closed: The list has been closed
illegal_history_list_attempt: The list you want to access is not yours illegal_history_list_attempt: The list you want to access is not yours
table_not_found: The requested table cannot be found or is not given
table_is_occupied: The table you want to sit on is already occupied table_is_occupied: The table you want to sit on is already occupied
table_is_reserved: The table you want to sit on is reserved by someone else table_is_reserved: The table you want to sit on is reserved by someone else
table_is_closed: The table you want to sit on is not available for service table_is_closed: The table you want to sit on is not available for service
supplier_is_closed: The owner of this table is currently not handling orders supplier_is_closed: The owner of this table is currently not handling orders
join_request_rejected: Your request to join the table has been rejected join_request_rejected: Your request to join the table has been rejected
join_request_approved: Your request to join the table has been approved
table_is_from_other_supplier: You cannot move to another table when you have an open list table_is_from_other_supplier: You cannot move to another table when you have an open list
moved_to_another_table: You successfully moved to another table moved_to_another_table: You successfully moved to another table
cannot_identify_table: The application cannot determine the table number cannot_identify_table: The application cannot determine the table number
+2
View File
@@ -21,11 +21,13 @@ nl:
order_is_placed: Je bestelling is in goede orde aangekomen order_is_placed: Je bestelling is in goede orde aangekomen
the_list_has_been_closed: De lijst is afgesloten the_list_has_been_closed: De lijst is afgesloten
illegal_history_list_attempt: Je probeert een lijst op te vragen die niet van jou is illegal_history_list_attempt: Je probeert een lijst op te vragen die niet van jou is
table_not_found: De gezochte tafel kan niet worden gevonden of is niet opgegeven
table_is_occupied: De tafel waar je aan wil gaan zitten is reeds bezet table_is_occupied: De tafel waar je aan wil gaan zitten is reeds bezet
table_is_reserved: De tafel waar je aan wil gaan zitten is gereserveerd table_is_reserved: De tafel waar je aan wil gaan zitten is gereserveerd
table_is_closed: De tafel waar je aan wil gaan zitten is niet beschikbaar voor bediening table_is_closed: De tafel waar je aan wil gaan zitten is niet beschikbaar voor bediening
supplier_is_closed: De eigenaar van deze tafel is momenteel gesloten supplier_is_closed: De eigenaar van deze tafel is momenteel gesloten
join_request_rejected: Je verzoek om te mogen bestellen op een bestaande lijst is afgewezen join_request_rejected: Je verzoek om te mogen bestellen op een bestaande lijst is afgewezen
join_request_approved: Je verzoek om te mogen bestellen op een bestaande lijst is goedgekeurd
table_is_from_other_supplier: Je kan geen lijst openen bij een andere zaak zolang je huidige lijst nog niet is afgesloten table_is_from_other_supplier: Je kan geen lijst openen bij een andere zaak zolang je huidige lijst nog niet is afgesloten
moved_to_another_table: De tafel is gewijzigd moved_to_another_table: De tafel is gewijzigd
cannot_identify_table: De applicatie kan niet bepalen om welke tafel het gaat cannot_identify_table: De applicatie kan niet bepalen om welke tafel het gaat