Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert commish email controller to html #1253

Merged
merged 1 commit into from
Feb 28, 2024
Merged
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
6 changes: 6 additions & 0 deletions lib/ex338/fantasy_leagues.ex
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,10 @@ defmodule Ex338.FantasyLeagues do
|> FantasyLeague.changeset(attrs)
|> Repo.update()
end

def format_leagues_for_select(leagues) do
Enum.map(leagues, fn league ->
{league.fantasy_league_name, league.id}
end)
end
end
6 changes: 4 additions & 2 deletions lib/ex338_web/components/core_components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ defmodule Ex338Web.CoreComponents do

attr :for, :any, required: true, doc: "the datastructure for the form"
attr :as, :any, default: nil, doc: "the server side parameter to collect all input under"
attr :show_form_error, :boolean, default: true

attr :rest, :global,
include: ~w(autocomplete name rel action enctype method novalidate target multipart),
Expand Down Expand Up @@ -244,7 +245,7 @@ defmodule Ex338Web.CoreComponents do
<div class="shadow sm:rounded-md sm:overflow-hidden">
<div class="px-4 py-5 bg-white sm:p-6">
<div class="grid grid-cols-3 gap-6">
<.error :if={f.source.valid? == false} class="!mt-0 col-span-3">
<.error :if={@show_form_error && f.source.valid? == false} class="!mt-0 col-span-3">
Oops, something went wrong! Please check the errors below.
</.error>
<div class="col-span-3 sm:col-span-2 space-y-6">
Expand Down Expand Up @@ -272,6 +273,7 @@ defmodule Ex338Web.CoreComponents do
"""
end

attr :submit_text, :string, default: "Save"
attr :back_route, :string, required: true

def submit_buttons(assigns) do
Expand All @@ -289,7 +291,7 @@ defmodule Ex338Web.CoreComponents do
type="submit"
class="inline-flex justify-center py-2 px-4 border border-transparent text-sm leading-5 font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-500 focus:outline-none focus:border-indigo-700 focus:shadow-outline-indigo active:bg-indigo-700 transition duration-150 ease-in-out"
>
Save
<%= @submit_text %>
</button>
</span>
"""
Expand Down
6 changes: 4 additions & 2 deletions lib/ex338_web/controllers/commish_email_controller.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
defmodule Ex338Web.CommishEmailController do
use Ex338Web, :controller
use Ex338Web, :controller_html

alias Ex338.FantasyLeagues.FantasyLeague
alias Ex338.Repo
alias Ex338Web.CommishEmail

def new(conn, _params) do
render(conn, "new.html",
render(
conn,
:new,
fantasy_leagues: Repo.all(FantasyLeague),
page_title: "Commish Email"
)
Expand Down
40 changes: 40 additions & 0 deletions lib/ex338_web/controllers/commish_email_html.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
defmodule Ex338Web.CommishEmailHTML do
use Ex338Web, :html

alias Ex338.FantasyLeagues

def new(assigns) do
~H"""
<.two_col_form
:let={f}
for={@conn}
as={:commish_email}
action={~p"/commish_email"}
show_form_error={false}
>
<:title>
Send an email to fantasy leagues
</:title>
<:description>
Select one or more leagues to send an email.
</:description>

<.input
field={f[:leagues]}
label="Select leagues to email"
type="select"
multiple
options={FantasyLeagues.format_leagues_for_select(@fantasy_leagues)}
/>
<p class="mt-2 text-sm text-gray-500" id="email-description">
Hold CTRL (Windows) or Command (Mac) to select multiple leagues
</p>
<.input field={f[:subject]} label="Subject" type="text" required />
<.input field={f[:message]} label="Message" type="textarea" required rows={6} />
<:actions>
<.submit_buttons back_route={~p"/"} submit_text="Send" />
</:actions>
</.two_col_form>
"""
end
end
61 changes: 0 additions & 61 deletions lib/ex338_web/templates/commish_email/form.html.eex

This file was deleted.

1 change: 0 additions & 1 deletion lib/ex338_web/templates/commish_email/new.html.eex

This file was deleted.

10 changes: 0 additions & 10 deletions lib/ex338_web/views/commish_email_view.ex

This file was deleted.

13 changes: 13 additions & 0 deletions test/ex338/fantasy_leagues_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,17 @@ defmodule Ex338.FantasyLeaguesTest do
assert fantasy_league == FantasyLeagues.get_fantasy_league!(fantasy_league.id)
end
end

describe "format_leagues_for_select/1" do
test "formats leagues for multiple select in form" do
leagues = [
%{fantasy_league_name: "Div A", id: 1},
%{fantasy_league_name: "Div B", id: 2}
]

results = FantasyLeagues.format_leagues_for_select(leagues)

assert results == [{"Div A", 1}, {"Div B", 2}]
end
end
end
18 changes: 0 additions & 18 deletions test/ex338_web/views/commish_email_view_test.exs

This file was deleted.

Loading