From e3dc6a7c6834d85f078d61633e71281ce900e214 Mon Sep 17 00:00:00 2001 From: Benjamin ter Kuile Date: Sun, 2 Dec 2012 19:53:19 +0100 Subject: [PATCH] event based table join system --- app/assets/javascripts/user/quser.js.coffee | 105 +++++++++++-------- app/controllers/user_controller.rb | 27 ++--- app/models/list.rb | 45 ++++++++ app/templates/user/_join_request.mustache | 13 +++ app/views/layouts/phone.html.slim | 1 + app/views/user/join_occupied_table.html.slim | 12 +-- config/locales/en.yml | 2 + config/locales/nl.yml | 2 + 8 files changed, 131 insertions(+), 76 deletions(-) create mode 100644 app/templates/user/_join_request.mustache diff --git a/app/assets/javascripts/user/quser.js.coffee b/app/assets/javascripts/user/quser.js.coffee index 3e5379f1..27c66238 100644 --- a/app/assets/javascripts/user/quser.js.coffee +++ b/app/assets/javascripts/user/quser.js.coffee @@ -15,22 +15,27 @@ class Quser if(e.event == 'list_closed') #redirect_to 'user_root', {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 @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') - if(e.event == 'order_being_delivered') + else if(e.event == 'order_being_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) - if(e.event == 'list_needs_help') + else if(e.event == 'list_needs_help') window.active_list.needs_help = true @list_needs_help_default_action() - if(e.event == 'list_needs_payment') + else if(e.event == 'list_needs_payment') window.active_list.needs_payment = true @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) false home_loader: -> @@ -49,7 +54,7 @@ class Quser handle_active_list_default_actions: (response)-> response ||= {} if(response.ok == false && response.status && response.status == 401) - direct_to_site('obtain_user_token') + direct_to_site('obtain_user_token') return $('.table-number').text(response.table_number) if response.table_number @@ -61,33 +66,50 @@ class Quser @list_needs_help_default_action(response) # 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) + @handle_join_requests(response) + handle_join_requests: (response)-> if !window.join_request_active && response.join_requests && response.join_requests.length window.join_request_active = true for join_request in response.join_requests - wrapper = $('') - join_callback = ( (request)-> - -> - $.post(data_host + '/user/approve_join_request', $.extend({user_id: request.user_id}, authentication_object), -> window.join_request_active = false; wrapper.modal('hide') ) - )(join_request) - reject_callback = ( (request)-> - -> - $.post(data_host + '/user/reject_join_request', $.extend({user_id: request.user_id}, authentication_object), -> window.join_request_active = false; wrapper.modal('hide' )) - )(join_request) - header = $('') - .append('') - .append($('

').text(t('join_request.title'))).appendTo(wrapper) + @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 = $('') + join_callback = ( (request)-> + -> + $.post(data_host + '/user/approve_join_request', $.extend({user_id: request.user_id}, authentication_object), -> window.join_request_active = false; wrapper.modal('hide') ) + )(join_request) + reject_callback = ( (request)-> + -> + $.post(data_host + '/user/reject_join_request', $.extend({user_id: request.user_id}, authentication_object), -> window.join_request_active = false; wrapper.modal('hide' )) + )(join_request) + header = $('') + .append('') + .append($('

').text(t('join_request.title'))).appendTo(wrapper) - body = $('') - #body.append(join_request.user_email + ' wants to join the table') - body.text(t('join_request.body', {email: join_request.user_email})) - - body.appendTo(wrapper) + body = $('') + #body.append(join_request.user_email + ' wants to join the table') + body.text(t('join_request.body', {email: join_request.user_email})) + + body.appendTo(wrapper) - footer = $('') - .append($('').text(t('join_request.reject')).click(reject_callback)) - .append($('').text(t('join_request.approve')).click(join_callback)) - .appendTo(wrapper) - wrapper.modal() + footer = $('') + .append($('').text(t('join_request.reject')).click(reject_callback)) + .append($('').text(t('join_request.approve')).click(join_callback)) + .appendTo(wrapper) + wrapper.modal() list_needs_help_default_action: (response)=> response ||= window.active_list @@ -349,12 +371,13 @@ class Quser #$.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} , '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: () -> - match = window.document.URL.toString().match('table_id=([0-9a-zA-Z]+)') - if match - table_id = match[1] - else + unless table_id = getUrlVars().table_id redirect_to 'user_root', {message: 'cannot_identify_table'} return $('.form-actions').remove() @@ -363,17 +386,7 @@ class Quser cont.append $('') cont.append $('

Waiting for approval of the person on this table

') $.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) - 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'} - ) + #setInterval('Quser.check_if_can_join_occupied_table("'+table_id+'")', 7500) add_product: (product_id, count) -> count ||= 1 window.active_products_list ||= {} diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index 807047a4..bbe935d7 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -78,6 +78,7 @@ class UserController < ApplicationController # GET /suppliers/1/product_list # GET /suppliers/1/product_list.json def list_products + redirect_to(user_root_path(message: 'the_list_has_been_closed')) and return unless list @supplier = list.supplier respond_to do |format| format.html do @@ -113,44 +114,30 @@ class UserController < ApplicationController end end - + # GET /user/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]) end + # POST /user/join_occupied_table def request_to_join_occupied_table @table = Table.find(params[:table_id]) if @list = @table.active_list - unless @list.join_requests.include?(current_user.id) - @list.join_requests << current_user.id - @list.is_dirty - @list.save - end - + @list.send_table_join_request_for_user! current_user end render nothing: true end def reject_join_request return unless params[:user_id] - if list && list.join_requests.include?(params[:user_id]) - list.join_requests.delete(params[:user_id]) - list.is_dirty - list.save - end + list && list.reject_join_request_for_user!(params[:user_id]) render js: '' end def approve_join_request return unless params[:user_id] @user = User.find(params[:user_id]) - if list && list.join_requests.include?(params[:user_id]) - 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 + list && list.approve_join_request_for_user!(@user) render nothing: true end diff --git a/app/models/list.rb b/app/models/list.rb index 6afe5e85..332f1869 100644 --- a/app/models/list.rb +++ b/app/models/list.rb @@ -148,6 +148,51 @@ class List 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 list_total = 0.0 diff --git a/app/templates/user/_join_request.mustache b/app/templates/user/_join_request.mustache new file mode 100644 index 00000000..a85e4d59 --- /dev/null +++ b/app/templates/user/_join_request.mustache @@ -0,0 +1,13 @@ + diff --git a/app/views/layouts/phone.html.slim b/app/views/layouts/phone.html.slim index d1fb7f5d..e956d83e 100644 --- a/app/views/layouts/phone.html.slim +++ b/app/views/layouts/phone.html.slim @@ -77,6 +77,7 @@ html lang="en" = yield = javascript_include_tag "user/application" script#alert-template[type="text/html"]= render 'user/alert.mustache' + script#join-request-template[type="text/html"]= render 'user/join_request.mustache' = yield :footer javascript: jQuery(function(){#{onload_javascript}}); diff --git a/app/views/user/join_occupied_table.html.slim b/app/views/user/join_occupied_table.html.slim index f0b2df47..bf825dd6 100644 --- a/app/views/user/join_occupied_table.html.slim +++ b/app/views/user/join_occupied_table.html.slim @@ -3,16 +3,8 @@ .form-actions = 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') #join-occupied-table-progress-container -- content_for :footer do - 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]); - } - }); +- onload_javascript 'Quser.watch_events()' diff --git a/config/locales/en.yml b/config/locales/en.yml index ab577690..a8dd6be4 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -22,11 +22,13 @@ en: order_is_placed: Your order has been received in good order the_list_has_been_closed: The list has been closed 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_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 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_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 moved_to_another_table: You successfully moved to another table cannot_identify_table: The application cannot determine the table number diff --git a/config/locales/nl.yml b/config/locales/nl.yml index 735dee53..6a38ff29 100644 --- a/config/locales/nl.yml +++ b/config/locales/nl.yml @@ -21,11 +21,13 @@ nl: order_is_placed: Je bestelling is in goede orde aangekomen 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 + 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_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 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_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 moved_to_another_table: De tafel is gewijzigd cannot_identify_table: De applicatie kan niet bepalen om welke tafel het gaat