Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions app/controllers/template_previews_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
class TemplatePreviewsController < ApplicationController
skip_before_action :authenticate_user!
skip_after_action :verify_authorized

def index
@templates = SnailMail::Components::Registry.available_templates
end

def show
template = params[:id]
include_qr_code = params[:qr].present?

mock_letter = create_mock_letter

pdf = SnailMail::PhlexService.generate_label(mock_letter, { template:, include_qr_code: })
send_data pdf.render, type: "application/pdf", disposition: "inline"
end

private

def create_mock_letter
return_address = OpenStruct.new(
name: "Hack Club",
line_1: "15 Falls Rd",
city: "Shelburne",
state: "VT",
postal_code: "05482",
country: "US",
)

names = [
"Orpheus",
"Heidi Hakkuun",
"Dinobox",
"Arcadius",
"Cap'n Trashbeard",
]

usps_mailer_id = OpenStruct.new(mid: "111111")
sender, recipient = names.sample(2)

OpenStruct.new(
address: SnailMail::Preview::FakeAddress.new(
line_1: "8605 Santa Monica Blvd",
line_2: "Apt. 86294",
city: "West Hollywood",
state: "CA",
postal_code: "90069",
country: "US",
name_line: sender,
),
return_address:,
return_address_name_line: recipient,
postage_type: "stamps",
postage: 0.73,
usps_mailer_id:,
imb_serial_number: "1337",
metadata: {},
rubber_stamps: "here's\n where\n rubber stamps go!",
public_id: "ltr!PR3V13W",
)
end
end
22 changes: 22 additions & 0 deletions app/views/template_previews/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<% content_for :title, "Template Previews" %>

<div class="max-w-4xl mx-auto p-8">
<h1 class="text-3xl font-bold mb-8">Letter Template Previews</h1>

<div class="grid gap-4">
<% @templates.each do |template| %>
<div class="border rounded-lg p-4 bg-white shadow-sm">
<h2 class="text-xl font-semibold mb-2"><%= template.to_s.split("::").last.humanize %></h2>
<p class="text-gray-600 mb-4 font-mono text-sm"><%= template %></p>
<div class="flex gap-3">
<%= link_to "Preview PDF", template_preview_path(template),
target: "_blank",
class: "bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 rounded" %>
<%= link_to "Preview with QR", template_preview_path(template, qr: true),
target: "_blank",
class: "bg-green-500 hover:bg-green-600 text-white px-4 py-2 rounded" %>
</div>
</div>
<% end %>
</div>
</div>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -705,5 +705,6 @@ def self.matches?(request)
# root "posts#index"
if Rails.env.development?
mount LetterOpenerWeb::Engine, at: "/letter_opener"
resources :template_previews, only: [:index, :show], path: "previews/templates"
end
end