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:
@ -11,6 +11,7 @@ defmodule Outlook.Articles.Article do
|
||||
field :title, :string
|
||||
field :url, :string
|
||||
belongs_to :author, Author
|
||||
has_many :translations, Translation
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
104
lib/outlook/translations.ex
Normal file
104
lib/outlook/translations.ex
Normal file
@ -0,0 +1,104 @@
|
||||
defmodule Outlook.Translations do
|
||||
@moduledoc """
|
||||
The Translations context.
|
||||
"""
|
||||
|
||||
import Ecto.Query, warn: false
|
||||
alias Outlook.Repo
|
||||
|
||||
alias Outlook.Translations.Translation
|
||||
|
||||
@doc """
|
||||
Returns the list of translations.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> list_translations()
|
||||
[%Translation{}, ...]
|
||||
|
||||
"""
|
||||
def list_translations do
|
||||
Repo.all(Translation)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a single translation.
|
||||
|
||||
Raises `Ecto.NoResultsError` if the Translation does not exist.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> get_translation!(123)
|
||||
%Translation{}
|
||||
|
||||
iex> get_translation!(456)
|
||||
** (Ecto.NoResultsError)
|
||||
|
||||
"""
|
||||
def get_translation!(id), do: Repo.get!(Translation, id)
|
||||
|
||||
@doc """
|
||||
Creates a translation.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> create_translation(%{field: value})
|
||||
{:ok, %Translation{}}
|
||||
|
||||
iex> create_translation(%{field: bad_value})
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def create_translation(attrs \\ %{}) do
|
||||
%Translation{}
|
||||
|> Translation.changeset(attrs)
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates a translation.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> update_translation(translation, %{field: new_value})
|
||||
{:ok, %Translation{}}
|
||||
|
||||
iex> update_translation(translation, %{field: bad_value})
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def update_translation(%Translation{} = translation, attrs) do
|
||||
translation
|
||||
|> Translation.changeset(attrs)
|
||||
|> Repo.update()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes a translation.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> delete_translation(translation)
|
||||
{:ok, %Translation{}}
|
||||
|
||||
iex> delete_translation(translation)
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def delete_translation(%Translation{} = translation) do
|
||||
Repo.delete(translation)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns an `%Ecto.Changeset{}` for tracking translation changes.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> change_translation(translation)
|
||||
%Ecto.Changeset{data: %Translation{}}
|
||||
|
||||
"""
|
||||
def change_translation(%Translation{} = translation, attrs \\ %{}) do
|
||||
Translation.changeset(translation, attrs)
|
||||
end
|
||||
end
|
||||
28
lib/outlook/translations/translation.ex
Normal file
28
lib/outlook/translations/translation.ex
Normal file
@ -0,0 +1,28 @@
|
||||
defmodule Outlook.Translations.Translation do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
|
||||
alias Outlook.Accounts.User
|
||||
alias Outlook.Articles.Article
|
||||
|
||||
schema "translations" do
|
||||
field :content, :map
|
||||
field :date, :utc_datetime
|
||||
field :lang, :string
|
||||
field :public, :boolean, default: false
|
||||
field :teaser, :string
|
||||
field :title, :string
|
||||
field :unauthorized, :boolean, default: false
|
||||
belongs_to :user, User
|
||||
belongs_to :article, Article
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
@doc false
|
||||
def changeset(translation, attrs) do
|
||||
translation
|
||||
|> cast(attrs, [:lang, :title, :teaser, :content, :date, :public, :unauthorized])
|
||||
|> validate_required([:lang, :title, :teaser, :content, :date, :public, :unauthorized])
|
||||
end
|
||||
end
|
||||
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