Add Translations
mix phx.gen.live Translations Translation translations \ lang:string title:string teaser:text content:map \ date:utc_datetime user_id:references:users \ public:boolean unauthorized:boolean article_id:references:articles
This commit is contained in:
87
lib/outlook_web/live/translation_live/form_component.ex
Normal file
87
lib/outlook_web/live/translation_live/form_component.ex
Normal file
@ -0,0 +1,87 @@
|
||||
defmodule OutlookWeb.TranslationLive.FormComponent do
|
||||
use OutlookWeb, :live_component
|
||||
|
||||
alias Outlook.Translations
|
||||
|
||||
@impl true
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
<div>
|
||||
<.header>
|
||||
<%= @title %>
|
||||
<:subtitle>Use this form to manage translation records in your database.</:subtitle>
|
||||
</.header>
|
||||
|
||||
<.simple_form
|
||||
:let={f}
|
||||
for={@changeset}
|
||||
id="translation-form"
|
||||
phx-target={@myself}
|
||||
phx-change="validate"
|
||||
phx-submit="save"
|
||||
>
|
||||
<.input field={{f, :lang}} type="text" label="lang" />
|
||||
<.input field={{f, :title}} type="text" label="title" />
|
||||
<.input field={{f, :teaser}} type="text" label="teaser" />
|
||||
<.input field={{f, :content}} type="text" label="content" />
|
||||
<.input field={{f, :date}} type="datetime-local" label="date" />
|
||||
<.input field={{f, :public}} type="checkbox" label="public" />
|
||||
<.input field={{f, :unauthorized}} type="checkbox" label="unauthorized" />
|
||||
<:actions>
|
||||
<.button phx-disable-with="Saving...">Save Translation</.button>
|
||||
</:actions>
|
||||
</.simple_form>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
@impl true
|
||||
def update(%{translation: translation} = assigns, socket) do
|
||||
changeset = Translations.change_translation(translation)
|
||||
|
||||
{:ok,
|
||||
socket
|
||||
|> assign(assigns)
|
||||
|> assign(:changeset, changeset)}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("validate", %{"translation" => translation_params}, socket) do
|
||||
changeset =
|
||||
socket.assigns.translation
|
||||
|> Translations.change_translation(translation_params)
|
||||
|> Map.put(:action, :validate)
|
||||
|
||||
{:noreply, assign(socket, :changeset, changeset)}
|
||||
end
|
||||
|
||||
def handle_event("save", %{"translation" => translation_params}, socket) do
|
||||
save_translation(socket, socket.assigns.action, translation_params)
|
||||
end
|
||||
|
||||
defp save_translation(socket, :edit, translation_params) do
|
||||
case Translations.update_translation(socket.assigns.translation, translation_params) do
|
||||
{:ok, _translation} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, "Translation updated successfully")
|
||||
|> push_navigate(to: socket.assigns.navigate)}
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
{:noreply, assign(socket, :changeset, changeset)}
|
||||
end
|
||||
end
|
||||
|
||||
defp save_translation(socket, :new, translation_params) do
|
||||
case Translations.create_translation(translation_params) do
|
||||
{:ok, _translation} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, "Translation created successfully")
|
||||
|> push_navigate(to: socket.assigns.navigate)}
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
{:noreply, assign(socket, changeset: changeset)}
|
||||
end
|
||||
end
|
||||
end
|
||||
46
lib/outlook_web/live/translation_live/index.ex
Normal file
46
lib/outlook_web/live/translation_live/index.ex
Normal file
@ -0,0 +1,46 @@
|
||||
defmodule OutlookWeb.TranslationLive.Index do
|
||||
use OutlookWeb, :live_view
|
||||
|
||||
alias Outlook.Translations
|
||||
alias Outlook.Translations.Translation
|
||||
|
||||
@impl true
|
||||
def mount(_params, _session, socket) do
|
||||
{:ok, assign(socket, :translations, list_translations())}
|
||||
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 Translation")
|
||||
|> assign(:translation, Translations.get_translation!(id))
|
||||
end
|
||||
|
||||
defp apply_action(socket, :new, _params) do
|
||||
socket
|
||||
|> assign(:page_title, "New Translation")
|
||||
|> assign(:translation, %Translation{})
|
||||
end
|
||||
|
||||
defp apply_action(socket, :index, _params) do
|
||||
socket
|
||||
|> assign(:page_title, "Listing Translations")
|
||||
|> assign(:translation, nil)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("delete", %{"id" => id}, socket) do
|
||||
translation = Translations.get_translation!(id)
|
||||
{:ok, _} = Translations.delete_translation(translation)
|
||||
|
||||
{:noreply, assign(socket, :translations, list_translations())}
|
||||
end
|
||||
|
||||
defp list_translations do
|
||||
Translations.list_translations()
|
||||
end
|
||||
end
|
||||
45
lib/outlook_web/live/translation_live/index.html.heex
Normal file
45
lib/outlook_web/live/translation_live/index.html.heex
Normal file
@ -0,0 +1,45 @@
|
||||
<.header>
|
||||
Listing Translations
|
||||
<:actions>
|
||||
<.link patch={~p"/translations/new"}>
|
||||
<.button>New Translation</.button>
|
||||
</.link>
|
||||
</:actions>
|
||||
</.header>
|
||||
|
||||
<.table id="translations" rows={@translations} row_click={&JS.navigate(~p"/translations/#{&1}")}>
|
||||
<:col :let={translation} label="Lang"><%= translation.lang %></:col>
|
||||
<:col :let={translation} label="Title"><%= translation.title %></:col>
|
||||
<:col :let={translation} label="Teaser"><%= translation.teaser %></:col>
|
||||
<:col :let={translation} label="Content"><%= translation.content %></:col>
|
||||
<:col :let={translation} label="Date"><%= translation.date %></:col>
|
||||
<:col :let={translation} label="Public"><%= translation.public %></:col>
|
||||
<:col :let={translation} label="Unauthorized"><%= translation.unauthorized %></:col>
|
||||
<:action :let={translation}>
|
||||
<div class="sr-only">
|
||||
<.link navigate={~p"/translations/#{translation}"}>Show</.link>
|
||||
</div>
|
||||
<.link patch={~p"/translations/#{translation}/edit"}>Edit</.link>
|
||||
</:action>
|
||||
<:action :let={translation}>
|
||||
<.link phx-click={JS.push("delete", value: %{id: translation.id})} data-confirm="Are you sure?">
|
||||
Delete
|
||||
</.link>
|
||||
</:action>
|
||||
</.table>
|
||||
|
||||
<.modal
|
||||
:if={@live_action in [:new, :edit]}
|
||||
id="translation-modal"
|
||||
show
|
||||
on_cancel={JS.navigate(~p"/translations")}
|
||||
>
|
||||
<.live_component
|
||||
module={OutlookWeb.TranslationLive.FormComponent}
|
||||
id={@translation.id || :new}
|
||||
title={@page_title}
|
||||
action={@live_action}
|
||||
translation={@translation}
|
||||
navigate={~p"/translations"}
|
||||
/>
|
||||
</.modal>
|
||||
21
lib/outlook_web/live/translation_live/show.ex
Normal file
21
lib/outlook_web/live/translation_live/show.ex
Normal file
@ -0,0 +1,21 @@
|
||||
defmodule OutlookWeb.TranslationLive.Show do
|
||||
use OutlookWeb, :live_view
|
||||
|
||||
alias Outlook.Translations
|
||||
|
||||
@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(:translation, Translations.get_translation!(id))}
|
||||
end
|
||||
|
||||
defp page_title(:show), do: "Show Translation"
|
||||
defp page_title(:edit), do: "Edit Translation"
|
||||
end
|
||||
32
lib/outlook_web/live/translation_live/show.html.heex
Normal file
32
lib/outlook_web/live/translation_live/show.html.heex
Normal file
@ -0,0 +1,32 @@
|
||||
<.header>
|
||||
Translation <%= @translation.id %>
|
||||
<:subtitle>This is a translation record from your database.</:subtitle>
|
||||
<:actions>
|
||||
<.link patch={~p"/translations/#{@translation}/show/edit"} phx-click={JS.push_focus()}>
|
||||
<.button>Edit translation</.button>
|
||||
</.link>
|
||||
</:actions>
|
||||
</.header>
|
||||
|
||||
<.list>
|
||||
<:item title="Lang"><%= @translation.lang %></:item>
|
||||
<:item title="Title"><%= @translation.title %></:item>
|
||||
<:item title="Teaser"><%= @translation.teaser %></:item>
|
||||
<:item title="Content"><%= @translation.content %></:item>
|
||||
<:item title="Date"><%= @translation.date %></:item>
|
||||
<:item title="Public"><%= @translation.public %></:item>
|
||||
<:item title="Unauthorized"><%= @translation.unauthorized %></:item>
|
||||
</.list>
|
||||
|
||||
<.back navigate={~p"/translations"}>Back to translations</.back>
|
||||
|
||||
<.modal :if={@live_action == :edit} id="translation-modal" show on_cancel={JS.patch(~p"/translations/#{@translation}")}>
|
||||
<.live_component
|
||||
module={OutlookWeb.TranslationLive.FormComponent}
|
||||
id={@translation.id}
|
||||
title={@page_title}
|
||||
action={@live_action}
|
||||
translation={@translation}
|
||||
navigate={~p"/translations/#{@translation}"}
|
||||
/>
|
||||
</.modal>
|
||||
@ -83,6 +83,13 @@ defmodule OutlookWeb.Router do
|
||||
|
||||
live "/articles/:id", ArticleLive.Show, :show
|
||||
live "/articles/:id/show/edit", ArticleLive.Show, :edit
|
||||
|
||||
live "/translations", TranslationLive.Index, :index
|
||||
live "/translations/new", TranslationLive.Index, :new
|
||||
live "/translations/:id/edit", TranslationLive.Index, :edit
|
||||
|
||||
live "/translations/:id", TranslationLive.Show, :show
|
||||
live "/translations/:id/show/edit", TranslationLive.Show, :edit
|
||||
end
|
||||
|
||||
scope "/", OutlookWeb do
|
||||
|
||||
Reference in New Issue
Block a user