From c716a36c918ced86e9c808642cb6ae56b9dbc22b Mon Sep 17 00:00:00 2001 From: Ritesh Kumar Date: Thu, 1 Feb 2024 01:19:56 -0500 Subject: [PATCH] Use polymorphic entities in liveview It's a little too bespoke to the structure of the gita now, I should make it more generic so that other texts can easily work with the same seeding function. --- docs/initial_db_helpers.livemd | 60 +++++++++++++++++-- lib/vyasa/written.ex | 10 ++-- lib/vyasa/written/chapter.ex | 4 +- .../live/source_live/chapter/index.ex | 5 +- .../live/source_live/chapter/index.html.heex | 22 +++++-- .../live/source_live/chapter/show_verse.ex | 3 + .../source_live/chapter/show_verse.html.heex | 21 ++++++- lib/vyasa_web/live/source_live/show.ex | 1 + lib/vyasa_web/live/source_live/show.html.heex | 5 +- 9 files changed, 107 insertions(+), 24 deletions(-) diff --git a/docs/initial_db_helpers.livemd b/docs/initial_db_helpers.livemd index 85ebbbd0..8de283fe 100644 --- a/docs/initial_db_helpers.livemd +++ b/docs/initial_db_helpers.livemd @@ -84,6 +84,7 @@ defmodule G do body: chapter["chapter_summary"], source_id: source_id } + |> dbg() end def create_chapter(%{} = chapter, [%{} = _head | _rest] = _verses, source_id) do @@ -137,7 +138,9 @@ defmodule G do Chapter.changeset( %Chapter{ no: chap["chapter_number"], - source_id: src_id + source_id: src_id, + title: chap["name"], + body: chap["chapter_summary"] }, %{ verses: verses @@ -152,8 +155,10 @@ defmodule G do lang: "en", body: chap["chapter_summary"], target: %{ - title: chap["name"], - translit_title: chap["name_transliterated"] + title: chap["name_meaning"], + translit_title: chap["name_transliterated"], + body: chap["chapter_summary"], + body_meant: chap["chapter_summary_hindi"] } }, inserted_chap @@ -246,8 +251,6 @@ defmodule DBHelper do DBHelper.insert_chapters(uuid) |> dbg() end end - -alias Vyasa.Written.{Source} ``` #### Testing the purging and seeding of the db: @@ -258,6 +261,53 @@ DBHelper.purge_db() DBHelper.seed_db() ``` +```elixir +### checking the query functions + +alias Vyasa.Written +alias Vyasa.Repo + +R.recompile() + +src = + Written.list_sources() + |> Enum.find(fn src -> src.title == "Gita" end) + +# src.id + +# Repo.get_by(Chapter, no: 1, source_id: src.id) + +# src = Written.get_source_by_title("Gita") +# foo = src.id +# # h_v = hd(src.verses) +# # h_v.translations +# # foo = src.id +# chap = Written.get_chapter(12, foo) +# hd(chap.translations) + +# source_title = "Gita" + +# src = Written.list_sources() +# |>Enum.find(fn src -> src.title == source_title end) +# no = 2 + +# chap = Repo.get_by(Chapter, no: no, source_id: src.id) +# |> Repo.preload([:verses]) + +# first_verse = hd(chap.verses) + +# R.recompile() +# chap3 = Written.get_chapter(3, "Gita") +# verses = Written.get_verses_in_chapter(3, src.id) +# first_v = hd(verses) +# first_v.translations +# Written.get_verse_via_url_params(3, 1, "Gita") + +# hd(Repo.all(Translation)) + +# chap3.translations +``` + ##### Bala's working example of using the polymorphic translations diff --git a/lib/vyasa/written.ex b/lib/vyasa/written.ex index b036c43f..207891e7 100644 --- a/lib/vyasa/written.ex +++ b/lib/vyasa/written.ex @@ -73,7 +73,7 @@ defmodule Vyasa.Written do def get_source_by_title(title) do list_sources() |>Enum.find(fn src -> src.title == title end) - |> Repo.preload([:chapters, :verses]) + |> Repo.preload([verses: [:translations], chapters: [:translations]]) end @@ -81,23 +81,23 @@ defmodule Vyasa.Written do src = list_sources() |>Enum.find(fn src -> src.title == source_title end) Repo.get_by(Chapter, no: no, source_id: src.id) - |> Repo.preload([:verses]) + |> Repo.preload([:translations, verses: [:translations]]) end def get_verses_in_chapter(no, source_id) do chapter = Repo.get_by(Chapter, no: no, source_id: source_id) - |> Repo.preload([:verses]) + |> Repo.preload([:verses, :translations]) chapter.verses end def get_verse_via_url_params(verse_no, chap_no, source_title) do chapter = get_chapter(chap_no, source_title) - |> Repo.preload([:verses, :source]) + |> Repo.preload([:source, :translations], verses: [:translations]) chapter.verses |> Enum.find(fn verse -> verse.no == verse_no end) - |> Repo.preload([:chapter, :source]) + |> Repo.preload([:chapter, :source, :translations]) end @doc """ diff --git a/lib/vyasa/written/chapter.ex b/lib/vyasa/written/chapter.ex index 6b93896b..1a6f42d6 100644 --- a/lib/vyasa/written/chapter.ex +++ b/lib/vyasa/written/chapter.ex @@ -2,7 +2,7 @@ defmodule Vyasa.Written.Chapter do use Ecto.Schema import Ecto.Changeset - alias Vyasa.Written.{Source, Verse} + alias Vyasa.Written.{Source, Verse, Translation} @primary_key false schema "chapters" do @@ -16,8 +16,8 @@ defmodule Vyasa.Written.Chapter do # field :indic_name_transliteration, :string belongs_to :source, Source, references: :id, foreign_key: :source_id, type: :binary_id, primary_key: :true - has_many :verses, Verse, references: :no, foreign_key: :chapter_no + has_many :translations, Translation, references: :no, foreign_key: :chapter_no end @doc false diff --git a/lib/vyasa_web/live/source_live/chapter/index.ex b/lib/vyasa_web/live/source_live/chapter/index.ex index 0036c977..a241c647 100644 --- a/lib/vyasa_web/live/source_live/chapter/index.ex +++ b/lib/vyasa_web/live/source_live/chapter/index.ex @@ -24,10 +24,10 @@ defmodule VyasaWeb.SourceLive.Chapter.Index do verses: verses, title: title, body: body, - # indic_name: indic_name, - # indic_name_meaning: indic_name_meaning, + translations: translations, } = Written.get_chapter(chap_no, source_title) + en_translation = translations |> Enum.find(fn t -> t.lang == "en" end) socket |> stream(:verses, verses) @@ -35,6 +35,7 @@ defmodule VyasaWeb.SourceLive.Chapter.Index do |> assign(:chap_no, chap_no) |> assign(:chap_body, body) |> assign(:chap_title, title) + |> assign(:en_translation, en_translation) # |> assign(:chap_indic_name, indic_name) # |> assign(:chap_indic_name_meaning, indic_name_meaning) |> assign(:page_title, "#{source_title} Chapter #{chap_no} | #{title}") diff --git a/lib/vyasa_web/live/source_live/chapter/index.html.heex b/lib/vyasa_web/live/source_live/chapter/index.html.heex index 7eff2136..afc4751e 100644 --- a/lib/vyasa_web/live/source_live/chapter/index.html.heex +++ b/lib/vyasa_web/live/source_live/chapter/index.html.heex @@ -1,21 +1,33 @@ <.header>
- STUB FOR INDIC NAME + <%= @en_translation.target.translit_title %> | <%= @chap_title %>
- Chapter <%= @chap_no%> - <%= @chap_title %> + Chapter <%= @chap_no%> - <%= @en_translation.target.title %>
- <:subtitle> - <%= @chap_body %> + <%= @en_translation.target.body %> <.verse_list :for={{_dom_id, verse} <- @streams.verses}> <:item title={"#{verse.chapter_no}.#{verse.no}"} navigate={~p"/explore/#{@source_title}/#{@chap_no}/#{verse.no}"} > -

<%= verse.body |> String.split("।।") |> List.first() %>

+ +

+ <%= verse.body |> String.split("।।") |> List.first() %> +

+ + <:item title={"Transliteration"}> +

+ <%= hd(verse.translations).target.body_translit %> +

+ + <:item title={"Transliteration Meaning"}> +

+ <%= hd(verse.translations).target.body_translit_meant %> +

diff --git a/lib/vyasa_web/live/source_live/chapter/show_verse.ex b/lib/vyasa_web/live/source_live/chapter/show_verse.ex index 26795d0d..9ac147a0 100644 --- a/lib/vyasa_web/live/source_live/chapter/show_verse.ex +++ b/lib/vyasa_web/live/source_live/chapter/show_verse.ex @@ -11,12 +11,15 @@ defmodule VyasaWeb.SourceLive.Chapter.ShowVerse do def handle_params(%{"source_title" => source_title, "chap_no" => chap_no, "verse_no" => verse_no}, _, socket) do verse = Written.get_verse_via_url_params(String.to_integer(verse_no), chap_no, source_title) |> dbg() + en_translation = verse.translations |> Enum.find(fn t -> t.lang == "en" end) + {:noreply, socket |> assign(:source_title, source_title) |> assign(:chap_no, chap_no) |> assign(:verse_no, String.to_integer(verse_no)) |> assign(:verse, verse) + |> assign(:en_translation, en_translation) # |> assign_meta() } # |> assign(:chapter, Written.get_chapter(chap_no, source_id)) diff --git a/lib/vyasa_web/live/source_live/chapter/show_verse.html.heex b/lib/vyasa_web/live/source_live/chapter/show_verse.html.heex index 1dfe99a0..18ee45b1 100644 --- a/lib/vyasa_web/live/source_live/chapter/show_verse.html.heex +++ b/lib/vyasa_web/live/source_live/chapter/show_verse.html.heex @@ -27,10 +27,25 @@ id="verseContainer" > <.header> - <:subtitle><%= @verse.chapter_no %>:<%= @verse.no %> -

- <%= @verse.body %>

+ <:subtitle> + <%= @verse.chapter_no %>:<%= @verse.no %> + +

+ <%= @verse.body %> +

+ +

+ <%= @en_translation.target.body_translit %> +

+ +

+ <%= @en_translation.target.body_translit_meant %> +

+ + + +


diff --git a/lib/vyasa_web/live/source_live/show.ex b/lib/vyasa_web/live/source_live/show.ex index 13fb25d9..22fa2de0 100644 --- a/lib/vyasa_web/live/source_live/show.ex +++ b/lib/vyasa_web/live/source_live/show.ex @@ -18,6 +18,7 @@ defmodule VyasaWeb.SourceLive.Show do title: title } = Written.get_source_by_title(source_title) + { :noreply, socket diff --git a/lib/vyasa_web/live/source_live/show.html.heex b/lib/vyasa_web/live/source_live/show.html.heex index 30290e9a..dc3a971b 100644 --- a/lib/vyasa_web/live/source_live/show.html.heex +++ b/lib/vyasa_web/live/source_live/show.html.heex @@ -14,7 +14,7 @@ row_click={fn {_id, chap} -> JS.navigate(~p"/explore/#{@title}/#{chap.no}") end} > <:col :let={{_id, chap}} label="Chapter">
- <%= chap.no %>. <%= chap.indic_name_translation %> + <%= chap.no %>. <%= hd(chap.translations).target.translit_title %>
<:col :let={{_id, chap}} label="Description"> @@ -22,9 +22,10 @@ row_click={fn {_id, chap} -> JS.navigate(~p"/explore/#{@title}/#{chap.no}") end} <%= chap.title %>
- <%= chap.indic_name %> + <%= hd(chap.translations).target.title %>
+ <.back navigate={~p"/explore/"}>Back to All Sources