Add Translators/deepl_accounts

mix phx.gen.live Translators DeeplAccount deepl_accounts\
  name:string description:text auth_key:string character_limit:integer\
  character_count:integer our_character_count:integer user_id:references:users
This commit is contained in:
Thelonius Kort
2022-12-26 18:51:30 +01:00
parent f66521dba8
commit 9e9c7b5519
12 changed files with 585 additions and 0 deletions

View File

@ -0,0 +1,86 @@
defmodule OutlookWeb.DeeplAccountLive.FormComponent do
use OutlookWeb, :live_component
alias Outlook.Translators
@impl true
def render(assigns) do
~H"""
<div>
<.header>
<%= @title %>
<:subtitle>Use this form to manage deepl_account records in your database.</:subtitle>
</.header>
<.simple_form
:let={f}
for={@changeset}
id="deepl_account-form"
phx-target={@myself}
phx-change="validate"
phx-submit="save"
>
<.input field={{f, :name}} type="text" label="name" />
<.input field={{f, :description}} type="text" label="description" />
<.input field={{f, :auth_key}} type="text" label="auth_key" />
<.input field={{f, :character_limit}} type="number" label="character_limit" />
<.input field={{f, :character_count}} type="number" label="character_count" />
<.input field={{f, :our_character_count}} type="number" label="our_character_count" />
<:actions>
<.button phx-disable-with="Saving...">Save Deepl account</.button>
</:actions>
</.simple_form>
</div>
"""
end
@impl true
def update(%{deepl_account: deepl_account} = assigns, socket) do
changeset = Translators.change_deepl_account(deepl_account)
{:ok,
socket
|> assign(assigns)
|> assign(:changeset, changeset)}
end
@impl true
def handle_event("validate", %{"deepl_account" => deepl_account_params}, socket) do
changeset =
socket.assigns.deepl_account
|> Translators.change_deepl_account(deepl_account_params)
|> Map.put(:action, :validate)
{:noreply, assign(socket, :changeset, changeset)}
end
def handle_event("save", %{"deepl_account" => deepl_account_params}, socket) do
save_deepl_account(socket, socket.assigns.action, deepl_account_params)
end
defp save_deepl_account(socket, :edit, deepl_account_params) do
case Translators.update_deepl_account(socket.assigns.deepl_account, deepl_account_params) do
{:ok, _deepl_account} ->
{:noreply,
socket
|> put_flash(:info, "Deepl account updated successfully")
|> push_navigate(to: socket.assigns.navigate)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, :changeset, changeset)}
end
end
defp save_deepl_account(socket, :new, deepl_account_params) do
case Translators.create_deepl_account(deepl_account_params) do
{:ok, _deepl_account} ->
{:noreply,
socket
|> put_flash(:info, "Deepl account created successfully")
|> push_navigate(to: socket.assigns.navigate)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, changeset: changeset)}
end
end
end

View File

@ -0,0 +1,46 @@
defmodule OutlookWeb.DeeplAccountLive.Index do
use OutlookWeb, :live_view
alias Outlook.Translators
alias Outlook.Translators.DeeplAccount
@impl true
def mount(_params, _session, socket) do
{:ok, assign(socket, :deepl_accounts, list_deepl_accounts())}
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 Deepl account")
|> assign(:deepl_account, Translators.get_deepl_account!(id))
end
defp apply_action(socket, :new, _params) do
socket
|> assign(:page_title, "New Deepl account")
|> assign(:deepl_account, %DeeplAccount{})
end
defp apply_action(socket, :index, _params) do
socket
|> assign(:page_title, "Listing Deepl accounts")
|> assign(:deepl_account, nil)
end
@impl true
def handle_event("delete", %{"id" => id}, socket) do
deepl_account = Translators.get_deepl_account!(id)
{:ok, _} = Translators.delete_deepl_account(deepl_account)
{:noreply, assign(socket, :deepl_accounts, list_deepl_accounts())}
end
defp list_deepl_accounts do
Translators.list_deepl_accounts()
end
end

View File

@ -0,0 +1,44 @@
<.header>
Listing Deepl accounts
<:actions>
<.link patch={~p"/deepl_accounts/new"}>
<.button>New Deepl account</.button>
</.link>
</:actions>
</.header>
<.table id="deepl_accounts" rows={@deepl_accounts} row_click={&JS.navigate(~p"/deepl_accounts/#{&1}")}>
<:col :let={deepl_account} label="Name"><%= deepl_account.name %></:col>
<:col :let={deepl_account} label="Description"><%= deepl_account.description %></:col>
<:col :let={deepl_account} label="Auth key"><%= deepl_account.auth_key %></:col>
<:col :let={deepl_account} label="Character limit"><%= deepl_account.character_limit %></:col>
<:col :let={deepl_account} label="Character count"><%= deepl_account.character_count %></:col>
<:col :let={deepl_account} label="Our character count"><%= deepl_account.our_character_count %></:col>
<:action :let={deepl_account}>
<div class="sr-only">
<.link navigate={~p"/deepl_accounts/#{deepl_account}"}>Show</.link>
</div>
<.link patch={~p"/deepl_accounts/#{deepl_account}/edit"}>Edit</.link>
</:action>
<:action :let={deepl_account}>
<.link phx-click={JS.push("delete", value: %{id: deepl_account.id})} data-confirm="Are you sure?">
Delete
</.link>
</:action>
</.table>
<.modal
:if={@live_action in [:new, :edit]}
id="deepl_account-modal"
show
on_cancel={JS.navigate(~p"/deepl_accounts")}
>
<.live_component
module={OutlookWeb.DeeplAccountLive.FormComponent}
id={@deepl_account.id || :new}
title={@page_title}
action={@live_action}
deepl_account={@deepl_account}
navigate={~p"/deepl_accounts"}
/>
</.modal>

View File

@ -0,0 +1,21 @@
defmodule OutlookWeb.DeeplAccountLive.Show do
use OutlookWeb, :live_view
alias Outlook.Translators
@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(:deepl_account, Translators.get_deepl_account!(id))}
end
defp page_title(:show), do: "Show Deepl account"
defp page_title(:edit), do: "Edit Deepl account"
end

View File

@ -0,0 +1,31 @@
<.header>
Deepl account <%= @deepl_account.id %>
<:subtitle>This is a deepl_account record from your database.</:subtitle>
<:actions>
<.link patch={~p"/deepl_accounts/#{@deepl_account}/show/edit"} phx-click={JS.push_focus()}>
<.button>Edit deepl_account</.button>
</.link>
</:actions>
</.header>
<.list>
<:item title="Name"><%= @deepl_account.name %></:item>
<:item title="Description"><%= @deepl_account.description %></:item>
<:item title="Auth key"><%= @deepl_account.auth_key %></:item>
<:item title="Character limit"><%= @deepl_account.character_limit %></:item>
<:item title="Character count"><%= @deepl_account.character_count %></:item>
<:item title="Our character count"><%= @deepl_account.our_character_count %></:item>
</.list>
<.back navigate={~p"/deepl_accounts"}>Back to deepl_accounts</.back>
<.modal :if={@live_action == :edit} id="deepl_account-modal" show on_cancel={JS.patch(~p"/deepl_accounts/#{@deepl_account}")}>
<.live_component
module={OutlookWeb.DeeplAccountLive.FormComponent}
id={@deepl_account.id}
title={@page_title}
action={@live_action}
deepl_account={@deepl_account}
navigate={~p"/deepl_accounts/#{@deepl_account}"}
/>
</.modal>