Skip to content

Commit

Permalink
broadcast map changes to admin page
Browse files Browse the repository at this point in the history
  • Loading branch information
digitaltom committed Feb 4, 2025
1 parent 8fa72e4 commit 1ba321c
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 9 deletions.
16 changes: 16 additions & 0 deletions app/models/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@ class Map
include ActiveModel::Model
include Mongoid::Document
include Mongoid::Timestamps
include Turbo::Broadcastable

# mongoid callbacks: https://www.mongodb.com/docs/mongoid/current/data-modeling/callbacks/
# broadcasts: https://www.rubydoc.info/github/hotwired/turbo-rails/Turbo/Streams/Broadcasts
after_create do
broadcast_prepend_to("admin_maps_list", target: "maps", partial: "maps/map",
locals: { rw: true, avatar: true, delete: true, last_change: true })
end
# TODO: this doen't trigger on touch
after_update do
broadcast_update_to("admin_maps_list", target: self, partial: "maps/map",
locals: { rw: true, avatar: true, delete: true, last_change: true })
end
after_destroy do
broadcast_remove_to("admin_maps_list", target: self)
end

has_many :layers
belongs_to :user, optional: true, counter_cache: true
Expand Down
4 changes: 3 additions & 1 deletion app/views/admin/index.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
= turbo_stream_from "admin_maps_list"

#maps.flex-container
- @maps.each do |map|
.flex-item{id: dom_id(map)}= render map, rw: true, last_change: true, avatar: true, delete: true, auth: true
= render map, rw: true, last_change: true, avatar: true, delete: true

- if @maps.empty?
.container.pt-4.mt-4.mb-4.empty-map-list.background-bright
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/_head.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
%meta{ name: 'view-transition', content: 'same-origin' }
%meta{ name: 'turbo-prefetch', content: 'false' }
%meta{ name: 'apple-mobile-web-app-capable', content: 'yes' }
%meta{ name: 'mobile-web-app-capable', content: 'yes' }
%meta{ name: 'description', content: "Create and share individual maps for events and locations. Choose from a base map like streets or satellite and 'paint' your custom layer onto it." }
Expand Down
14 changes: 9 additions & 5 deletions app/views/maps/_map.html.haml
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
.map-preview.background-bright
- rw = local_assigns[:rw] || false
- avatar = local_assigns[:avatar] || false
- delete = local_assigns[:delete] || false
- last_change = local_assigns[:last_change] || false

- if auth
- if map.private
.map-auth{title: 'This map is private and not linked from the public listing.'}
%i.bi.bi-lock-fill
.flex-item.map-preview.background-bright{id: dom_id(map)}

- if map.private
.map-auth{title: 'This map is private and not linked from the public listing.'}
%i.bi.bi-lock-fill

- if map.user && avatar
-if map.user.image
Expand Down
2 changes: 1 addition & 1 deletion app/views/maps/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

#maps.flex-container
- @maps.each do |map|
.flex-item{id: dom_id(map)}= render map, rw: false, last_change: false, avatar: false, delete: false, auth: false
= render map, rw: false, last_change: false, avatar: false, delete: false

2 changes: 1 addition & 1 deletion app/views/maps/my.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#maps.flex-container
- @maps.each do |map|
.flex-item{id: dom_id(map)}= render map, rw: true, last_change: false, avatar: false, delete: true, auth: true
= render map, rw: true, last_change: false, avatar: false, delete: true

- if @maps.empty?
.container.pt-4.mt-4.mb-4.empty-map-list.background-bright
Expand Down

0 comments on commit 1ba321c

Please sign in to comment.