Add initial project

This commit is contained in:
2023-11-23 00:36:49 +00:00
parent f6860b86c9
commit b9569dcf6b
964 changed files with 9664 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
defmodule WishWeb.HomeLive.Index do
use WishWeb, :live_view
alias Wish.Wishlist
alias Wish.Wishlist.Item
@impl true
def mount(_params, _session, socket) do
{:ok, stream(socket, :items, Wishlist.list_items())}
end
end

View File

@@ -0,0 +1,39 @@
<.header>
Listing Items
</.header>
<.table
id="items"
rows={@streams.items}
row_click={fn {_id, item} -> JS.navigate(~p"/items/#{item}") end}
>
<:col :let={{_id, item}} label="Title"><%= item.title %></:col>
<:col :let={{_id, item}} label="Description"><%= item.description %></:col>
<:col :let={{_id, item}} label="Url"><%= item.url %></:col>
<:col :let={{_id, item}} label="Desire"><%= item.desire %></:col>
<:action :let={{_id, item}}>
<div class="sr-only">
<.link navigate={~p"/items/#{item}"}>Show</.link>
</div>
<.link patch={~p"/items/#{item}/edit"}>Edit</.link>
</:action>
<:action :let={{id, item}}>
<.link
phx-click={JS.push("delete", value: %{id: item.id}) |> hide("##{id}")}
data-confirm="Are you sure?"
>
Delete
</.link>
</:action>
</.table>
<.modal :if={@live_action in [:new, :edit]} id="item-modal" show on_cancel={JS.patch(~p"/items")}>
<.live_component
module={WishWeb.ItemLive.FormComponent}
id={@item.id || :new}
title={@page_title}
action={@live_action}
item={@item}
patch={~p"/items"}
/>
</.modal>

View File

@@ -0,0 +1,94 @@
defmodule WishWeb.ItemLive.FormComponent do
use WishWeb, :live_component
alias Wish.Wishlist
@impl true
def render(assigns) do
~H"""
<div>
<.header>
<%= @title %>
<:subtitle>Use this form to manage item records in your database.</:subtitle>
</.header>
<.simple_form
for={@form}
id="item-form"
phx-target={@myself}
phx-change="validate"
phx-submit="save"
>
<.input field={@form[:title]} type="text" label="Title" />
<.input field={@form[:description]} type="text" label="Description" />
<.input field={@form[:url]} type="text" label="Url" />
<.input field={@form[:received]} type="checkbox" label="Received" />
<.input field={@form[:desire]} type="number" label="Desire" />
<:actions>
<.button phx-disable-with="Saving...">Save Item</.button>
</:actions>
</.simple_form>
</div>
"""
end
@impl true
def update(%{item: item} = assigns, socket) do
changeset = Wishlist.change_item(item)
{:ok,
socket
|> assign(assigns)
|> assign_form(changeset)}
end
@impl true
def handle_event("validate", %{"item" => item_params}, socket) do
changeset =
socket.assigns.item
|> Wishlist.change_item(item_params)
|> Map.put(:action, :validate)
{:noreply, assign_form(socket, changeset)}
end
def handle_event("save", %{"item" => item_params}, socket) do
save_item(socket, socket.assigns.action, item_params)
end
defp save_item(socket, :edit, item_params) do
case Wishlist.update_item(socket.assigns.item, item_params) do
{:ok, item} ->
notify_parent({:saved, item})
{:noreply,
socket
|> put_flash(:info, "Item updated successfully")
|> push_patch(to: socket.assigns.patch)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign_form(socket, changeset)}
end
end
defp save_item(socket, :new, item_params) do
case Wishlist.create_item(item_params) do
{:ok, item} ->
notify_parent({:saved, item})
{:noreply,
socket
|> put_flash(:info, "Item created successfully")
|> push_patch(to: socket.assigns.patch)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign_form(socket, changeset)}
end
end
defp assign_form(socket, %Ecto.Changeset{} = changeset) do
assign(socket, :form, to_form(changeset))
end
defp notify_parent(msg), do: send(self(), {__MODULE__, msg})
end

View File

@@ -0,0 +1,47 @@
defmodule WishWeb.ItemLive.Index do
use WishWeb, :live_view
alias Wish.Wishlist
alias Wish.Wishlist.Item
@impl true
def mount(_params, _session, socket) do
{:ok, stream(socket, :items, Wishlist.list_items())}
end
@impl true
def handle_params(params, _url, socket) do
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
end
defp apply_action(socket, :edit, %{"id" => id}) do
socket
|> assign(:page_title, "Edit Item")
|> assign(:item, Wishlist.get_item!(id))
end
defp apply_action(socket, :new, _params) do
socket
|> assign(:page_title, "New Item")
|> assign(:item, %Item{})
end
defp apply_action(socket, :index, _params) do
socket
|> assign(:page_title, "Listing Items")
|> assign(:item, nil)
end
@impl true
def handle_info({WishWeb.ItemLive.FormComponent, {:saved, item}}, socket) do
{:noreply, stream_insert(socket, :items, item)}
end
@impl true
def handle_event("delete", %{"id" => id}, socket) do
item = Wishlist.get_item!(id)
{:ok, _} = Wishlist.delete_item(item)
{:noreply, stream_delete(socket, :items, item)}
end
end

View File

@@ -0,0 +1,44 @@
<.header>
Listing Items
<:actions>
<.link patch={~p"/items/new"}>
<.button>New Item</.button>
</.link>
</:actions>
</.header>
<.table
id="items"
rows={@streams.items}
row_click={fn {_id, item} -> JS.navigate(~p"/items/#{item}") end}
>
<:col :let={{_id, item}} label="Title"><%= item.title %></:col>
<:col :let={{_id, item}} label="Description"><%= item.description %></:col>
<:col :let={{_id, item}} label="Url"><%= item.url %></:col>
<:col :let={{_id, item}} label="Desire"><%= item.desire %></:col>
<:action :let={{_id, item}}>
<div class="sr-only">
<.link navigate={~p"/items/#{item}"}>Show</.link>
</div>
<.link patch={~p"/items/#{item}/edit"}>Edit</.link>
</:action>
<:action :let={{id, item}}>
<.link
phx-click={JS.push("delete", value: %{id: item.id}) |> hide("##{id}")}
data-confirm="Are you sure?"
>
Delete
</.link>
</:action>
</.table>
<.modal :if={@live_action in [:new, :edit]} id="item-modal" show on_cancel={JS.patch(~p"/items")}>
<.live_component
module={WishWeb.ItemLive.FormComponent}
id={@item.id || :new}
title={@page_title}
action={@live_action}
item={@item}
patch={~p"/items"}
/>
</.modal>

View File

@@ -0,0 +1,21 @@
defmodule WishWeb.ItemLive.Show do
use WishWeb, :live_view
alias Wish.Wishlist
@impl true
def mount(_params, _session, socket) do
{:ok, socket}
end
@impl true
def handle_params(%{"id" => id}, _, socket) do
{:noreply,
socket
|> assign(:page_title, page_title(socket.assigns.live_action))
|> assign(:item, Wishlist.get_item!(id))}
end
defp page_title(:show), do: "Show Item"
defp page_title(:edit), do: "Edit Item"
end

View File

@@ -0,0 +1,30 @@
<.header>
Item <%= @item.id %>
<:subtitle>This is a item record from your database.</:subtitle>
<:actions>
<.link patch={~p"/items/#{@item}/show/edit"} phx-click={JS.push_focus()}>
<.button>Edit item</.button>
</.link>
</:actions>
</.header>
<.list>
<:item title="Title"><%= @item.title %></:item>
<:item title="Description"><%= @item.description %></:item>
<:item title="Url"><%= @item.url %></:item>
<:item title="Received"><%= @item.received %></:item>
<:item title="Desire"><%= @item.desire %></:item>
</.list>
<.back navigate={~p"/items"}>Back to items</.back>
<.modal :if={@live_action == :edit} id="item-modal" show on_cancel={JS.patch(~p"/items/#{@item}")}>
<.live_component
module={WishWeb.ItemLive.FormComponent}
id={@item.id}
title={@page_title}
action={@live_action}
item={@item}
patch={~p"/items/#{@item}"}
/>
</.modal>

View File

@@ -0,0 +1,51 @@
defmodule WishWeb.UserConfirmationInstructionsLive do
use WishWeb, :live_view
alias Wish.Accounts
def render(assigns) do
~H"""
<div class="mx-auto max-w-sm">
<.header class="text-center">
No confirmation instructions received?
<:subtitle>We'll send a new confirmation link to your inbox</:subtitle>
</.header>
<.simple_form for={@form} id="resend_confirmation_form" phx-submit="send_instructions">
<.input field={@form[:email]} type="email" placeholder="Email" required />
<:actions>
<.button phx-disable-with="Sending..." class="w-full">
Resend confirmation instructions
</.button>
</:actions>
</.simple_form>
<p class="text-center mt-4">
<.link href={~p"/users/register"}>Register</.link>
| <.link href={~p"/users/log_in"}>Log in</.link>
</p>
</div>
"""
end
def mount(_params, _session, socket) do
{:ok, assign(socket, form: to_form(%{}, as: "user"))}
end
def handle_event("send_instructions", %{"user" => %{"email" => email}}, socket) do
if user = Accounts.get_user_by_email(email) do
Accounts.deliver_user_confirmation_instructions(
user,
&url(~p"/users/confirm/#{&1}")
)
end
info =
"If your email is in our system and it has not been confirmed yet, you will receive an email with instructions shortly."
{:noreply,
socket
|> put_flash(:info, info)
|> redirect(to: ~p"/")}
end
end

View File

@@ -0,0 +1,58 @@
defmodule WishWeb.UserConfirmationLive do
use WishWeb, :live_view
alias Wish.Accounts
def render(%{live_action: :edit} = assigns) do
~H"""
<div class="mx-auto max-w-sm">
<.header class="text-center">Confirm Account</.header>
<.simple_form for={@form} id="confirmation_form" phx-submit="confirm_account">
<.input field={@form[:token]} type="hidden" />
<:actions>
<.button phx-disable-with="Confirming..." class="w-full">Confirm my account</.button>
</:actions>
</.simple_form>
<p class="text-center mt-4">
<.link href={~p"/users/register"}>Register</.link>
| <.link href={~p"/users/log_in"}>Log in</.link>
</p>
</div>
"""
end
def mount(%{"token" => token}, _session, socket) do
form = to_form(%{"token" => token}, as: "user")
{:ok, assign(socket, form: form), temporary_assigns: [form: nil]}
end
# Do not log in the user after confirmation to avoid a
# leaked token giving the user access to the account.
def handle_event("confirm_account", %{"user" => %{"token" => token}}, socket) do
case Accounts.confirm_user(token) do
{:ok, _} ->
{:noreply,
socket
|> put_flash(:info, "User confirmed successfully.")
|> redirect(to: ~p"/")}
:error ->
# If there is a current user and the account was already confirmed,
# then odds are that the confirmation link was already visited, either
# by some automation or by the user themselves, so we redirect without
# a warning message.
case socket.assigns do
%{current_user: %{confirmed_at: confirmed_at}} when not is_nil(confirmed_at) ->
{:noreply, redirect(socket, to: ~p"/")}
%{} ->
{:noreply,
socket
|> put_flash(:error, "User confirmation link is invalid or it has expired.")
|> redirect(to: ~p"/")}
end
end
end
end

View File

@@ -0,0 +1,50 @@
defmodule WishWeb.UserForgotPasswordLive do
use WishWeb, :live_view
alias Wish.Accounts
def render(assigns) do
~H"""
<div class="mx-auto max-w-sm">
<.header class="text-center">
Forgot your password?
<:subtitle>We'll send a password reset link to your inbox</:subtitle>
</.header>
<.simple_form for={@form} id="reset_password_form" phx-submit="send_email">
<.input field={@form[:email]} type="email" placeholder="Email" required />
<:actions>
<.button phx-disable-with="Sending..." class="w-full">
Send password reset instructions
</.button>
</:actions>
</.simple_form>
<p class="text-center text-sm mt-4">
<.link href={~p"/users/register"}>Register</.link>
| <.link href={~p"/users/log_in"}>Log in</.link>
</p>
</div>
"""
end
def mount(_params, _session, socket) do
{:ok, assign(socket, form: to_form(%{}, as: "user"))}
end
def handle_event("send_email", %{"user" => %{"email" => email}}, socket) do
if user = Accounts.get_user_by_email(email) do
Accounts.deliver_user_reset_password_instructions(
user,
&url(~p"/users/reset_password/#{&1}")
)
end
info =
"If your email is in our system, you will receive instructions to reset your password shortly."
{:noreply,
socket
|> put_flash(:info, info)
|> redirect(to: ~p"/")}
end
end

View File

@@ -0,0 +1,43 @@
defmodule WishWeb.UserLoginLive do
use WishWeb, :live_view
def render(assigns) do
~H"""
<div class="mx-auto max-w-sm">
<.header class="text-center">
Sign in to account
<:subtitle>
Don't have an account?
<.link navigate={~p"/users/register"} class="font-semibold text-brand hover:underline">
Sign up
</.link>
for an account now.
</:subtitle>
</.header>
<.simple_form for={@form} id="login_form" action={~p"/users/log_in"} phx-update="ignore">
<.input field={@form[:email]} type="email" label="Email" required />
<.input field={@form[:password]} type="password" label="Password" required />
<:actions>
<.input field={@form[:remember_me]} type="checkbox" label="Keep me logged in" />
<.link href={~p"/users/reset_password"} class="text-sm font-semibold">
Forgot your password?
</.link>
</:actions>
<:actions>
<.button phx-disable-with="Signing in..." class="w-full">
Sign in <span aria-hidden="true">→</span>
</.button>
</:actions>
</.simple_form>
</div>
"""
end
def mount(_params, _session, socket) do
email = live_flash(socket.assigns.flash, :email)
form = to_form(%{"email" => email}, as: "user")
{:ok, assign(socket, form: form), temporary_assigns: [form: form]}
end
end

View File

@@ -0,0 +1,87 @@
defmodule WishWeb.UserRegistrationLive do
use WishWeb, :live_view
alias Wish.Accounts
alias Wish.Accounts.User
def render(assigns) do
~H"""
<div class="mx-auto max-w-sm">
<.header class="text-center">
Register for an account
<:subtitle>
Already registered?
<.link navigate={~p"/users/log_in"} class="font-semibold text-brand hover:underline">
Sign in
</.link>
to your account now.
</:subtitle>
</.header>
<.simple_form
for={@form}
id="registration_form"
phx-submit="save"
phx-change="validate"
phx-trigger-action={@trigger_submit}
action={~p"/users/log_in?_action=registered"}
method="post"
>
<.error :if={@check_errors}>
Oops, something went wrong! Please check the errors below.
</.error>
<.input field={@form[:email]} type="email" label="Email" required />
<.input field={@form[:password]} type="password" label="Password" required />
<:actions>
<.button phx-disable-with="Creating account..." class="w-full">Create an account</.button>
</:actions>
</.simple_form>
</div>
"""
end
def mount(_params, _session, socket) do
changeset = Accounts.change_user_registration(%User{})
socket =
socket
|> assign(trigger_submit: false, check_errors: false)
|> assign_form(changeset)
{:ok, socket, temporary_assigns: [form: nil]}
end
def handle_event("save", %{"user" => user_params}, socket) do
case Accounts.register_user(user_params) do
{:ok, user} ->
{:ok, _} =
Accounts.deliver_user_confirmation_instructions(
user,
&url(~p"/users/confirm/#{&1}")
)
changeset = Accounts.change_user_registration(user)
{:noreply, socket |> assign(trigger_submit: true) |> assign_form(changeset)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, socket |> assign(check_errors: true) |> assign_form(changeset)}
end
end
def handle_event("validate", %{"user" => user_params}, socket) do
changeset = Accounts.change_user_registration(%User{}, user_params)
{:noreply, assign_form(socket, Map.put(changeset, :action, :validate))}
end
defp assign_form(socket, %Ecto.Changeset{} = changeset) do
form = to_form(changeset, as: "user")
if changeset.valid? do
assign(socket, form: form, check_errors: false)
else
assign(socket, form: form)
end
end
end

View File

@@ -0,0 +1,89 @@
defmodule WishWeb.UserResetPasswordLive do
use WishWeb, :live_view
alias Wish.Accounts
def render(assigns) do
~H"""
<div class="mx-auto max-w-sm">
<.header class="text-center">Reset Password</.header>
<.simple_form
for={@form}
id="reset_password_form"
phx-submit="reset_password"
phx-change="validate"
>
<.error :if={@form.errors != []}>
Oops, something went wrong! Please check the errors below.
</.error>
<.input field={@form[:password]} type="password" label="New password" required />
<.input
field={@form[:password_confirmation]}
type="password"
label="Confirm new password"
required
/>
<:actions>
<.button phx-disable-with="Resetting..." class="w-full">Reset Password</.button>
</:actions>
</.simple_form>
<p class="text-center text-sm mt-4">
<.link href={~p"/users/register"}>Register</.link>
| <.link href={~p"/users/log_in"}>Log in</.link>
</p>
</div>
"""
end
def mount(params, _session, socket) do
socket = assign_user_and_token(socket, params)
form_source =
case socket.assigns do
%{user: user} ->
Accounts.change_user_password(user)
_ ->
%{}
end
{:ok, assign_form(socket, form_source), temporary_assigns: [form: nil]}
end
# Do not log in the user after reset password to avoid a
# leaked token giving the user access to the account.
def handle_event("reset_password", %{"user" => user_params}, socket) do
case Accounts.reset_user_password(socket.assigns.user, user_params) do
{:ok, _} ->
{:noreply,
socket
|> put_flash(:info, "Password reset successfully.")
|> redirect(to: ~p"/users/log_in")}
{:error, changeset} ->
{:noreply, assign_form(socket, Map.put(changeset, :action, :insert))}
end
end
def handle_event("validate", %{"user" => user_params}, socket) do
changeset = Accounts.change_user_password(socket.assigns.user, user_params)
{:noreply, assign_form(socket, Map.put(changeset, :action, :validate))}
end
defp assign_user_and_token(socket, %{"token" => token}) do
if user = Accounts.get_user_by_reset_password_token(token) do
assign(socket, user: user, token: token)
else
socket
|> put_flash(:error, "Reset password link is invalid or it has expired.")
|> redirect(to: ~p"/")
end
end
defp assign_form(socket, %{} = source) do
assign(socket, :form, to_form(source, as: "user"))
end
end

View File

@@ -0,0 +1,167 @@
defmodule WishWeb.UserSettingsLive do
use WishWeb, :live_view
alias Wish.Accounts
def render(assigns) do
~H"""
<.header class="text-center">
Account Settings
<:subtitle>Manage your account email address and password settings</:subtitle>
</.header>
<div class="space-y-12 divide-y">
<div>
<.simple_form
for={@email_form}
id="email_form"
phx-submit="update_email"
phx-change="validate_email"
>
<.input field={@email_form[:email]} type="email" label="Email" required />
<.input
field={@email_form[:current_password]}
name="current_password"
id="current_password_for_email"
type="password"
label="Current password"
value={@email_form_current_password}
required
/>
<:actions>
<.button phx-disable-with="Changing...">Change Email</.button>
</:actions>
</.simple_form>
</div>
<div>
<.simple_form
for={@password_form}
id="password_form"
action={~p"/users/log_in?_action=password_updated"}
method="post"
phx-change="validate_password"
phx-submit="update_password"
phx-trigger-action={@trigger_submit}
>
<.input
field={@password_form[:email]}
type="hidden"
id="hidden_user_email"
value={@current_email}
/>
<.input field={@password_form[:password]} type="password" label="New password" required />
<.input
field={@password_form[:password_confirmation]}
type="password"
label="Confirm new password"
/>
<.input
field={@password_form[:current_password]}
name="current_password"
type="password"
label="Current password"
id="current_password_for_password"
value={@current_password}
required
/>
<:actions>
<.button phx-disable-with="Changing...">Change Password</.button>
</:actions>
</.simple_form>
</div>
</div>
"""
end
def mount(%{"token" => token}, _session, socket) do
socket =
case Accounts.update_user_email(socket.assigns.current_user, token) do
:ok ->
put_flash(socket, :info, "Email changed successfully.")
:error ->
put_flash(socket, :error, "Email change link is invalid or it has expired.")
end
{:ok, push_navigate(socket, to: ~p"/users/settings")}
end
def mount(_params, _session, socket) do
user = socket.assigns.current_user
email_changeset = Accounts.change_user_email(user)
password_changeset = Accounts.change_user_password(user)
socket =
socket
|> assign(:current_password, nil)
|> assign(:email_form_current_password, nil)
|> assign(:current_email, user.email)
|> assign(:email_form, to_form(email_changeset))
|> assign(:password_form, to_form(password_changeset))
|> assign(:trigger_submit, false)
{:ok, socket}
end
def handle_event("validate_email", params, socket) do
%{"current_password" => password, "user" => user_params} = params
email_form =
socket.assigns.current_user
|> Accounts.change_user_email(user_params)
|> Map.put(:action, :validate)
|> to_form()
{:noreply, assign(socket, email_form: email_form, email_form_current_password: password)}
end
def handle_event("update_email", params, socket) do
%{"current_password" => password, "user" => user_params} = params
user = socket.assigns.current_user
case Accounts.apply_user_email(user, password, user_params) do
{:ok, applied_user} ->
Accounts.deliver_user_update_email_instructions(
applied_user,
user.email,
&url(~p"/users/settings/confirm_email/#{&1}")
)
info = "A link to confirm your email change has been sent to the new address."
{:noreply, socket |> put_flash(:info, info) |> assign(email_form_current_password: nil)}
{:error, changeset} ->
{:noreply, assign(socket, :email_form, to_form(Map.put(changeset, :action, :insert)))}
end
end
def handle_event("validate_password", params, socket) do
%{"current_password" => password, "user" => user_params} = params
password_form =
socket.assigns.current_user
|> Accounts.change_user_password(user_params)
|> Map.put(:action, :validate)
|> to_form()
{:noreply, assign(socket, password_form: password_form, current_password: password)}
end
def handle_event("update_password", params, socket) do
%{"current_password" => password, "user" => user_params} = params
user = socket.assigns.current_user
case Accounts.update_user_password(user, password, user_params) do
{:ok, user} ->
password_form =
user
|> Accounts.change_user_password(user_params)
|> to_form()
{:noreply, assign(socket, trigger_submit: true, password_form: password_form)}
{:error, changeset} ->
{:noreply, assign(socket, password_form: to_form(changeset))}
end
end
end