add out of stock action cable
This commit is contained in:
2
Gemfile
2
Gemfile
@@ -1,6 +1,6 @@
|
||||
source 'https://rubygems.org'
|
||||
|
||||
#ruby '2.4.1'
|
||||
ruby '2.4.1'
|
||||
#ruby '2.5.7'
|
||||
|
||||
|
||||
|
||||
@@ -380,5 +380,8 @@ DEPENDENCIES
|
||||
web-console (>= 3.3.0)
|
||||
whenever
|
||||
|
||||
RUBY VERSION
|
||||
ruby 2.4.1p111
|
||||
|
||||
BUNDLED WITH
|
||||
2.0.2
|
||||
|
||||
@@ -1543,11 +1543,11 @@ $(function() {
|
||||
image_path = menu_items[field].image;
|
||||
}
|
||||
if (out_of_stock === true) {
|
||||
// oos_item = out_of_stock_item
|
||||
oos_item = '<div class="card custom-card testimonial-card fadeInRight" style="height:100%;background-image:url(../../'+image_path+');background-repeat: no-repeat;pointer-events: none;opacity: 0.4;">'
|
||||
oos_header = '<div style="position: absolute;z-index: 5;top: 0;left: 0;right: 0;text-align: center;background: #fb483a;margin: auto;color: #fff;padding: 8px;font-weight: bolder;">OUT OF STOCK</div>'
|
||||
oos_item = out_of_stock_item
|
||||
oos_item = '<div class="card custom-card testimonial-card fadeInRight" id="'+ code +'" style="height:100%;background-image:url(../../'+image_path+');background-repeat: no-repeat;pointer-events: none;opacity: 0.4;">'
|
||||
oos_header = '<div class="head'+code+'" style="position: absolute;z-index: 5;top: 0;left: 0;right: 0;text-align: center;background: #fb483a;margin: auto;color: #fff;padding: 8px;font-weight: bolder;">OUT OF STOCK</div>'
|
||||
}else{
|
||||
oos_item = '<div class="card custom-card testimonial-card fadeInRight" style="height:100%;background-image:url(../../'+image_path+');background-repeat: no-repeat;">'
|
||||
oos_item = '<div class="card custom-card testimonial-card fadeInRight" id="'+ code +'" style="height:100%;background-image:url(../../'+image_path+');background-repeat: no-repeat;">'
|
||||
oos_header = ''
|
||||
}
|
||||
row = '<div class="col-md-6 col-sm-6 col-lg-3">'
|
||||
@@ -1596,15 +1596,15 @@ $(function() {
|
||||
}else{
|
||||
if (out_of_stock === true) {
|
||||
// oos_item = out_of_stock_item
|
||||
oos_item = '<div class="card custom-card testimonial-card fadeInRight" style="height:100%; pointer-events: none;opacity: 0.4;">'
|
||||
oos_header = '<div style="position: absolute;z-index: 5;top: 0;left: 0;right: 0;text-align: center;background: #fb483a;margin: auto;color: #fff;padding: 8px;font-weight: bolder;">OUT OF STOCK</div>'
|
||||
oos_item = '<div class="card custom-card testimonial-card fadeInRight" id="'+ code +'" style="height:100%; pointer-events: none;opacity: 0.4;">'
|
||||
oos_header = '<div class="head'+code+'" style="position: absolute;z-index: 5;top: 0;left: 0;right: 0;text-align: center;background: #fb483a;margin: auto;color: #fff;padding: 8px;font-weight: bolder;">OUT OF STOCK</div>'
|
||||
}else{
|
||||
oos_item = '<div class="card custom-card testimonial-card fadeInRight" style="height:100%;">'
|
||||
oos_item = '<div class="card custom-card testimonial-card fadeInRight" id="'+ code +'" style="height:100%;">'
|
||||
oos_header = ''
|
||||
}
|
||||
row = '<div class="col-md-6 col-sm-6 col-lg-3 mt-1">'
|
||||
+ oos_item + oos_header
|
||||
+'<div class="custom-card-no-img-head card-head '+add_icon+' " style="margin:0px;display:flex;!important "'
|
||||
+'<div class="custom-card-no-img-head card-head '+add_icon+' " id="oos'+ code +'" style="margin:0px;display:flex;!important "'
|
||||
+" data-item-code='"+ menu_items[field].code +"' "
|
||||
+" data-name='" + menu_items[field].name +"' "
|
||||
+" data-qty = '"+ qty +"' "
|
||||
|
||||
43
app/assets/javascripts/channels/out_of_stock.js
Normal file
43
app/assets/javascripts/channels/out_of_stock.js
Normal file
@@ -0,0 +1,43 @@
|
||||
App.checkin = App.cable.subscriptions.create('OutOfStockChannel', {
|
||||
|
||||
connected: function() {},
|
||||
|
||||
disconnected: function() {},
|
||||
|
||||
received: function(data) {
|
||||
if (data && data.item_instance_code) {
|
||||
console.log("out of stock channel!!!!");
|
||||
item_code = data.item_instance_code;
|
||||
item_code_id = '#' + data.item_instance_code;
|
||||
oos_id = '#oos' + data.item_instance_code;
|
||||
head_oos = 'head' + data.item_instance_code;
|
||||
rm_oos = '.' + head_oos;
|
||||
menus = localStorage.getItem('menus');
|
||||
|
||||
if (menus) {
|
||||
menus = jQuery.parseJSON(menus);
|
||||
menus.forEach(m =>
|
||||
m.categories.forEach(c =>
|
||||
c.items.forEach(i =>
|
||||
i.instances.forEach(instance => {
|
||||
if (instance.code == item_code) {
|
||||
if (data.status == true) {
|
||||
instance.out_of_stock = true;
|
||||
oos_header = '<div class="'+head_oos+'" style="position: absolute;z-index: 5;top: 0;left: 0;right: 0;text-align: center;background: #fb483a;margin: auto;color: #fff;padding: 8px;font-weight: bolder;">OUT OF STOCK</div>';
|
||||
$(item_code_id).css({"height": "100%", "pointer-events": "none", "opacity": "0.4"});
|
||||
$(oos_header).insertBefore($(oos_id));
|
||||
} else {
|
||||
instance.out_of_stock = false;
|
||||
$(item_code_id).css({"height": "", "pointer-events": "", "opacity": ""});
|
||||
$(rm_oos).remove();
|
||||
}
|
||||
}
|
||||
})
|
||||
)
|
||||
)
|
||||
);
|
||||
localStorage.setItem('menus', JSON.stringify(menus));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
10
app/channels/out_of_stock_channel.rb
Normal file
10
app/channels/out_of_stock_channel.rb
Normal file
@@ -0,0 +1,10 @@
|
||||
class OutOfStockChannel < ApplicationCable::Channel
|
||||
def subscribed
|
||||
stream_from "out_of_stock_channel"
|
||||
end
|
||||
|
||||
def unsubscribed
|
||||
stop_all_streams
|
||||
# Any cleanup needed when channel is unsubscribed
|
||||
end
|
||||
end
|
||||
@@ -27,6 +27,7 @@ class Settings::OutOfStockController < ApplicationController
|
||||
item_instance.update(is_out_of_stock: true)
|
||||
items_arr.each do |i|
|
||||
OutOfStock.create_out_of_stock(date,i)
|
||||
ActionCable.server.broadcast "out_of_stock_channel", item_instance_code: i, status: true
|
||||
end
|
||||
elsif item_instance.first.is_out_of_stock?
|
||||
item_instance.update(is_out_of_stock: false)
|
||||
@@ -41,6 +42,8 @@ class Settings::OutOfStockController < ApplicationController
|
||||
item_instance = MenuItemInstance.find_by_id(params[:id])
|
||||
unless item_instance.nil?
|
||||
item_instance.update(is_out_of_stock: false)
|
||||
ActionCable.server.broadcast "out_of_stock_channel", item_instance_code: item_instance.item_instance_code, status: false
|
||||
OutOfStock.where(item_instance_code: item_instance.item_instance_code, created_at: Time.zone.now.beginning_of_day..Time.zone.now.end_of_day).destroy_all
|
||||
flash[:message] = 'Out of stock was successfully destroyed.'
|
||||
render :json => {:status=> "Success", :url => settings_out_of_stock_index_url }.to_json
|
||||
end
|
||||
@@ -48,9 +51,15 @@ class Settings::OutOfStockController < ApplicationController
|
||||
|
||||
def reset_all
|
||||
item_instance = MenuItemInstance.where(is_out_of_stock: true)
|
||||
item_instance.update(is_out_of_stock: false)
|
||||
flash[:message] = 'Out of stock was successfully destroyed.'
|
||||
render :json => {:status=> "Success", :url => settings_out_of_stock_index_url }.to_json
|
||||
unless item_instance.nil?
|
||||
item_instance.update(is_out_of_stock: false)
|
||||
item_instance.each do |i|
|
||||
ActionCable.server.broadcast "out_of_stock_channel", item_instance_code: i.item_instance_code, status: false
|
||||
OutOfStock.where(item_instance_code: i.item_instance_code, created_at: Time.zone.now.beginning_of_day..Time.zone.now.end_of_day).destroy_all
|
||||
end
|
||||
flash[:message] = 'Out of stock was successfully destroyed.'
|
||||
render :json => {:status=> "Success", :url => settings_out_of_stock_index_url }.to_json
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
Reference in New Issue
Block a user