Skip to content

Commit

Permalink
🥔✨ Marketplace: UI to manage OrderNotificationMethod
Browse files Browse the repository at this point in the history
  • Loading branch information
zspencer committed Jun 28, 2023
1 parent 36545a7 commit 2451dd5
Show file tree
Hide file tree
Showing 13 changed files with 186 additions and 1 deletion.
17 changes: 17 additions & 0 deletions app/furniture/marketplace/breadcrumbs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@
link t("marketplace.orders.index.link_to"), marketplace.location(child: :orders)
end

crumb :marketplace_order_notification_methods do |marketplace|
parent :edit_marketplace, marketplace
link t("marketplace.order_notification_methods.index.link_to"), marketplace.location(child: :order_notification_methods)
end

crumb :new_marketplace_order_notification_method do |order_notification_method|
parent :marketplace_order_notification_methods, order_notification_method.marketplace
link t("marketplace.order_notification_methods.index.link_to"),
order_notification_method.marketplace.location(child: :order_notification_methods)
end

crumb :edit_marketplace_order_notification_method do |order_notification_method|
parent :marketplace_order_notification_methods, order_notification_method.marketplace
link t("marketplace.order_notification_methods.edit.link_to", contact_location: order_notification_method.contact_location)
order_notification_method.marketplace.location(child: :order_notification_methods)
end

crumb :marketplace_products do |marketplace|
parent :edit_marketplace, marketplace
link t("marketplace.products.index.link_to"), marketplace.location(child: :products)
Expand Down
13 changes: 13 additions & 0 deletions app/furniture/marketplace/locales/en.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

en:
activerecord:
attributes:
marketplace/order_notification_method:
contact_location: "Email Address"
errors:
models:
marketplace/delivery:
Expand Down Expand Up @@ -62,6 +65,16 @@ en:
link_to: "Order History"
show:
link_to: "Order %{order_id}"
order_notification_methods:
index:
link_to: "Order Notifications"
edit:
link_to: "Edit Order Notification '%{contact_location}'"
update:
success: "Order Notification '%{contact_location}' Saved!"

new:
link_to: "Add Order Notification"
products:
index:
link_to: "Products"
Expand Down
8 changes: 8 additions & 0 deletions app/furniture/marketplace/management_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@
href: marketplace.location(child: :orders),
turbo_stream: false, method: :get, scheme: :secondary
) if policy(marketplace.orders).index? %>
<%= render ButtonComponent.new(
label: t("marketplace.order_notification_methods.index.link_to"),
icon: :letter,
href: marketplace.location(child: :order_notification_methods),
method: :get, turbo_stream: false, scheme: :secondary
) if policy(marketplace.order_notification_methods).index? %>
</nav>
<% end %>
<% end %>
1 change: 0 additions & 1 deletion app/furniture/marketplace/marketplaces/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<%- breadcrumb :edit_marketplace, marketplace %>
<%= render Marketplace::ManagementComponent.new(marketplace: marketplace) do %>
<%= render "form", marketplace: marketplace %>
<p class="flex justify-between my-5 flex-wrap">
<%- if marketplace.stripe_api_key? %>
<%- if marketplace.stripe_account_connected? %>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<%= render CardComponent.new(dom_id: dom_id(order_notification_method), classes: "flex flex-col justify-between gap-y-2 w-full") do %>

<div class="font-bold">
<%= contact_location %>
</div>

<footer class="mt-3 flex flex-row justify-between">
<%= render edit_button if edit_button? %>
<%= render destroy_button if destroy_button? %>
</footer>
<%- end %>
36 changes: 36 additions & 0 deletions app/furniture/marketplace/order_notification_method_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Marketplace
class OrderNotificationMethodComponent < ApplicationComponent
attr_accessor :order_notification_method
delegate :contact_location, to: :order_notification_method

def initialize(order_notification_method:, **kwargs)
super(**kwargs)

self.order_notification_method = order_notification_method
end

def edit_button
super(title: t("marketplace.order_notification_methods.edit.link_to", contact_location: contact_location),
href: order_notification_method.location(:edit))
end

def edit_button?
order_notification_method.persisted? && policy(order_notification_method).edit?
end

def destroy_button
return unless destroy_button?

ButtonComponent.new(label: "#{t("icons.destroy")} #{t("destroy.link_to")}",
title: t("marketplace.order_notification_methods.destroy.link_to", contact_location: contact_location),
href: order_notification_method.location, turbo_stream: true,
method: :delete,
confirm: t("destroy.confirm"),
scheme: :secondary)
end

def destroy_button?
order_notification_method.persisted? && policy(order_notification_method).destroy?
end
end
end
7 changes: 7 additions & 0 deletions app/furniture/marketplace/order_notification_method_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Marketplace
class OrderNotificationMethodPolicy < Policy
def permitted_attributes(_)
[:contact_location]
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<%= form_with(model: order_notification_method.location) do |form| %>
<%= render "email_field", attribute: :contact_location, form: form %>
<%= form.submit %>
<%- end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<%- breadcrumb :edit_marketplace_order_notification_method, order_notification_method %>
<%= render "form", order_notification_method: order_notification_method %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<%- breadcrumb(:marketplace_order_notification_methods, marketplace) %>
<%= render Marketplace::ManagementComponent.new(marketplace: marketplace) do %>
<section class="mt-3">
<main>

<%= render Marketplace::OrderNotificationMethodComponent.with_collection(order_notification_methods) %>

</main>

<div class="text-center mt-3">
<%- order_notification_methods = marketplace.order_notification_methods.new %>
<%- if policy(order_notification_methods).create? %>
<%= render ButtonComponent.new(
label: "#{t('marketplace.order_notification_methods.new.link_to')} #{t('icons.new')}",
title: t('marketplace.order_notification_methods.new.link_to'),
href: marketplace.location(:new, child: :order_notification_method),
method: :get,
scheme: :secondary) %>
<%- end %>
</div>
</section>
<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<%- breadcrumb :new_marketplace_order_notification_method, order_notification_method %>
<%= render "form", order_notification_method: order_notification_method %>
59 changes: 59 additions & 0 deletions app/furniture/marketplace/order_notification_methods_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
class Marketplace
class OrderNotificationMethodsController < Controller
def new
order_notification_method
end

def edit
order_notification_method
end

def update
if order_notification_method.update(order_notification_method_params)
redirect_to marketplace.location(child: :order_notification_methods), notice: t(".success", contact_location: order_notification_method.contact_location)
else
render :edit
end
end

def create
if order_notification_method.save
redirect_to marketplace.location(child: :order_notification_methods)
else
render :new
end
end

def destroy
order_notification_method.destroy

respond_to do |format|
format.turbo_stream do
if order_notification_method.destroyed?
render turbo_stream: turbo_stream.remove(order_notification_method)
else
render turbo_stream: turbo_stream.replace(order_notification_method)
end
end
end
end

helper_method delegate :order_notification_methods, to: :marketplace

helper_method def order_notification_method
if params[:id]
order_notification_methods.find(params[:id])
elsif params[:order_notification_method]
order_notification_methods.new(order_notification_method_params)
else
order_notification_methods.new
end.tap do |order_notification_method|
authorize(order_notification_method)
end
end

def order_notification_method_params
policy(OrderNotificationMethod).permit(params.require(:order_notification_method))
end
end
end
2 changes: 2 additions & 0 deletions app/furniture/marketplace/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ def self.append_routes(router)
router.resources :marketplaces, only: [:show, :edit, :update], module: "marketplace" do
router.resources :stripe_events

router.resources :order_notification_methods

router.resources :carts, only: [] do
router.resources :cart_products
router.resource :checkout, only: [:show, :create]
Expand Down

0 comments on commit 2451dd5

Please sign in to comment.