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
|
||||
Reference in New Issue
Block a user