diff --git a/app/channels/count_channel.rb b/app/channels/count_channel.rb index 013cb0a..5d23b98 100644 --- a/app/channels/count_channel.rb +++ b/app/channels/count_channel.rb @@ -1,4 +1,5 @@ class CountChannel < ApplicationCable::Channel + def subscribed stream_from "counts" end @@ -8,7 +9,7 @@ class CountChannel < ApplicationCable::Channel end def plus_one(params) - unless current_user.nil? + unless current_user.nil? || params["id"].nil? total = Total.find(params["id"]) unless total.nil? total.update(count: total.count + 1) @@ -17,7 +18,7 @@ class CountChannel < ApplicationCable::Channel end def minus_one(params) - unless current_user.nil? + unless current_user.nil? || params["id"].nil? total = Total.find(params["id"]) unless total.nil? total.update(count: total.count - 1) @@ -25,12 +26,13 @@ class CountChannel < ApplicationCable::Channel end end - def self.create(total) - - end - def self.update(total) - ActionCable.server.broadcast("counts", {"update": total}) + ActionCable.server.broadcast("counts", {"update": { + id: total.id, + name: total.name, + count: total.count, + edit_link: Rails.application.routes.url_helpers.edit_count_path(total) + }}) end def self.destroy(total) diff --git a/app/javascript/channels/count_channel.js b/app/javascript/channels/count_channel.js index 4b9fd3f..ea2bf70 100644 --- a/app/javascript/channels/count_channel.js +++ b/app/javascript/channels/count_channel.js @@ -13,8 +13,12 @@ const count_channel = consumer.subscriptions.create("CountChannel", { received(data) { // Called when there's incoming data on the websocket for this channel console.log(data) - if (data.update !== undefined) - this.update(data.update) + if (data.update !== undefined) { + if ($("[data-count-id='" + data.update.id + "']").length) + this.update(data.update) + else + this.create(data.update) + } if (data.destroy !== undefined) this.destroy(data.destroy) @@ -28,10 +32,41 @@ const count_channel = consumer.subscriptions.create("CountChannel", { destroy(data) { $("[data-count-id='"+ data + "']").remove() }, + + create(data) { + let created = $("#template").clone() + created.attr("data-count-id", data.id) + created.find(".name").text(data.name) + created.find(".count").text(data.count) + created.find(".minus").click(function () { + count_channel.minus_one(data.id) + }) + created.find(".plus").click(function () { + count_channel.plus_one(data.id) + }) + created.find(".edit").attr("href", data.edit_link) + created.removeClass("d-none") + $("#count-list").append(created) + }, + + minus_one(id) { + if (count_channel !== undefined && !count_channel.consumer.connection.disconnected) { + count_channel.perform("minus_one", { "id": id }) + } + }, + + plus_one(id) { + if (count_channel !== undefined && !count_channel.consumer.connection.disconnected) { + count_channel.perform("plus_one", { "id": id }) + } + }, }); $(document).on('turbo:load', function() { - if (count_channel !== undefined && !count_channel.consumer.connection.disconnected) { - $("") - } + $("[data-count-id] .minus").click(function () { + count_channel.minus_one($(this).parents("[data-count-id]").attr("data-count-id")) + }); + $("[data-count-id] .plus").click(function () { + count_channel.plus_one($(this).parents("[data-count-id]").attr("data-count-id")) + }); }) diff --git a/app/models/total.rb b/app/models/total.rb index 6410551..2e6e0e4 100644 --- a/app/models/total.rb +++ b/app/models/total.rb @@ -16,8 +16,8 @@ class Total < ApplicationRecord validates_uniqueness_of :name validates_length_of :name, in: 1..32 - after_save do - CountChannel.create(self) + after_create do + CountChannel.update(self) end after_update do diff --git a/app/views/count/index.html.erb b/app/views/count/index.html.erb index 7c6beed..1c9d7b9 100644 --- a/app/views/count/index.html.erb +++ b/app/views/count/index.html.erb @@ -1,4 +1,19 @@ -
+
+
+
+
+ +
+ +
+ +
+
+
+ + <%= link_to "", "", class: "btn btn-sm btn-primary bi-pen-fill edit" %> +
+
<% @counts.each { |c| %>
@@ -11,11 +26,11 @@
- <%= c.name %> + <%= c.name %> <%= link_to "", edit_count_path(c), class: "btn btn-sm btn-primary bi-pen-fill" %>
- <% } %> + <% } %>
<%= link_to "", new_count_path, class: "btn btn-lg btn-success fs-3 bi-plus-lg" %> diff --git a/app/views/home/index.html.erb b/app/views/home/index.html.erb index d06f199..3b1e791 100644 --- a/app/views/home/index.html.erb +++ b/app/views/home/index.html.erb @@ -1,4 +1,8 @@ -
+
+
+ +
+
<% @counts.each { |c| %>
<%= c.count %>