neiam
/apollos_crib
It's Apollo's Crib
public · Issues · Pulls 1 · Labels · Forks · Compare · Actions failure · Badges · Packages
Log in to mark this repository.
most of the pythiae workflows, live preview compass directions
03f670a · gmorell · 2022-08-13 06:10
Files changed
modified
apps/room_sanctum/lib/room_sanctum/configuration.ex
+100
−0
| 434 | 434 | def change_foci(%Foci{} = foci, attrs \\ %{}) do |
| 435 | 435 | Foci.changeset(foci, attrs) |
| 436 | 436 | end |
| 437 | + | |
| 438 | + alias RoomSanctum.Configuration.Pythiae | |
| 439 | + | |
| 440 | + @doc """ | |
| 441 | + Returns the list of cfg_pythiae. | |
| 442 | + | |
| 443 | + ## Examples | |
| 444 | + | |
| 445 | + iex> list_cfg_pythiae() | |
| 446 | + [%Pythiae{}, ...] | |
| 447 | + | |
| 448 | + """ | |
| 449 | + def list_cfg_pythiae do | |
| 450 | + Repo.all(Pythiae) | |
| 451 | + end | |
| 452 | + | |
| 453 | + def list_cfg_pythiae({:user, uid}) do | |
| 454 | + Repo.all(from s in Pythiae, where: s.user_id == ^uid) | |
| 455 | + end | |
| 456 | + | |
| 457 | + @doc """ | |
| 458 | + Gets a single pythiae. | |
| 459 | + | |
| 460 | + Raises `Ecto.NoResultsError` if the Pythiae does not exist. | |
| 461 | + | |
| 462 | + ## Examples | |
| 463 | + | |
| 464 | + iex> get_pythiae!(123) | |
| 465 | + %Pythiae{} | |
| 466 | + | |
| 467 | + iex> get_pythiae!(456) | |
| 468 | + ** (Ecto.NoResultsError) | |
| 469 | + | |
| 470 | + """ | |
| 471 | + def get_pythiae!(id), do: Repo.get!(Pythiae, id) | |
| 472 | + | |
| 473 | + @doc """ | |
| 474 | + Creates a pythiae. | |
| 475 | + | |
| 476 | + ## Examples | |
| 477 | + | |
| 478 | + iex> create_pythiae(%{field: value}) | |
| 479 | + {:ok, %Pythiae{}} | |
| 480 | + | |
| 481 | + iex> create_pythiae(%{field: bad_value}) | |
| 482 | + {:error, %Ecto.Changeset{}} | |
| 483 | + | |
| 484 | + """ | |
| 485 | + def create_pythiae(attrs \\ %{}) do | |
| 486 | + %Pythiae{} | |
| 487 | + |> Pythiae.changeset(attrs) | |
| 488 | + |> Repo.insert() | |
| 489 | + end | |
| 490 | + | |
| 491 | + @doc """ | |
| 492 | + Updates a pythiae. | |
| 493 | + | |
| 494 | + ## Examples | |
| 495 | + | |
| 496 | + iex> update_pythiae(pythiae, %{field: new_value}) | |
| 497 | + {:ok, %Pythiae{}} | |
| 498 | + | |
| 499 | + iex> update_pythiae(pythiae, %{field: bad_value}) | |
| 500 | + {:error, %Ecto.Changeset{}} | |
| 501 | + | |
| 502 | + """ | |
| 503 | + def update_pythiae(%Pythiae{} = pythiae, attrs) do | |
| 504 | + pythiae | |
| 505 | + |> Pythiae.changeset(attrs) | |
| 506 | + |> Repo.update() | |
| 507 | + end | |
| 508 | + | |
| 509 | + @doc """ | |
| 510 | + Deletes a pythiae. | |
| 511 | + | |
| 512 | + ## Examples | |
| 513 | + | |
| 514 | + iex> delete_pythiae(pythiae) | |
| 515 | + {:ok, %Pythiae{}} | |
| 516 | + | |
| 517 | + iex> delete_pythiae(pythiae) | |
| 518 | + {:error, %Ecto.Changeset{}} | |
| 519 | + | |
| 520 | + """ | |
| 521 | + def delete_pythiae(%Pythiae{} = pythiae) do | |
| 522 | + Repo.delete(pythiae) | |
| 523 | + end | |
| 524 | + | |
| 525 | + @doc """ | |
| 526 | + Returns an `%Ecto.Changeset{}` for tracking pythiae changes. | |
| 527 | + | |
| 528 | + ## Examples | |
| 529 | + | |
| 530 | + iex> change_pythiae(pythiae) | |
| 531 | + %Ecto.Changeset{data: %Pythiae{}} | |
| 532 | + | |
| 533 | + """ | |
| 534 | + def change_pythiae(%Pythiae{} = pythiae, attrs \\ %{}) do | |
| 535 | + Pythiae.changeset(pythiae, attrs) | |
| 536 | + end | |
| 437 | 537 | end |
added
apps/room_sanctum/lib/room_sanctum/configuration/pythiae.ex
+25
−0
| 1 | +defmodule RoomSanctum.Configuration.Pythiae do | |
| 2 | + use Ecto.Schema | |
| 3 | + import Ecto.Changeset | |
| 4 | + | |
| 5 | + schema "cfg_pythiae" do | |
| 6 | + field :name, :string | |
| 7 | + field :ankyra, {:array, :integer} | |
| 8 | + field :visions, {:array, :integer} | |
| 9 | + belongs_to :user, RoomSanctum.Accounts.User | |
| 10 | + field :curr_vision, :id | |
| 11 | + field :curr_foci, :id | |
| 12 | + | |
| 13 | + timestamps() | |
| 14 | + end | |
| 15 | + | |
| 16 | + @doc false | |
| 17 | + def changeset(pythiae, attrs) do | |
| 18 | + pythiae | |
| 19 | + |> cast(attrs, [:visions, :ankyra, :user_id, :curr_vision, :curr_foci, :name]) | |
| 20 | + |> foreign_key_constraint(:user_id) | |
| 21 | + |> foreign_key_constraint(:curr_vision) | |
| 22 | + |> foreign_key_constraint(:curr_foci) | |
| 23 | + |> validate_required([:visions, :ankyra, :name]) | |
| 24 | + end | |
| 25 | +end |
added
apps/room_sanctum/lib/room_sanctum_web/live/cfg_pythiae_live/form_component.ex
+109
−0
| 1 | +defmodule RoomSanctumWeb.PythiaeLive.FormComponent do | |
| 2 | + use RoomSanctumWeb, :live_component | |
| 3 | + | |
| 4 | + alias RoomSanctum.Configuration | |
| 5 | + alias RoomSanctum.Accounts | |
| 6 | + | |
| 7 | + def gen_name() do | |
| 8 | + FriendlyID.generate(2, separator: "-") | |
| 9 | + end | |
| 10 | + | |
| 11 | + defp inj_uid(params, socket) do | |
| 12 | + params | |
| 13 | + |> Map.put("user_id", socket.assigns.current_user.id) | |
| 14 | + end | |
| 15 | + | |
| 16 | + @impl true | |
| 17 | + def update(%{pythiae: pythiae} = assigns, socket) do | |
| 18 | + changeset = Configuration.change_pythiae(pythiae) | |
| 19 | + | |
| 20 | + {:ok, | |
| 21 | + socket | |
| 22 | + |> assign(assigns) | |
| 23 | + |> assign(:changeset, changeset) | |
| 24 | + |> assign(:cfg_ankyra, list_cfg_ankyra(assigns.current_user.id)) | |
| 25 | + |> assign(:cfg_visions, list_cfg_visions(assigns.current_user.id)) | |
| 26 | + |> assign( | |
| 27 | + :cfg_ankyra_sel, | |
| 28 | + list_cfg_ankyra(assigns.current_user.id) | |
| 29 | + |> Enum.map(fn x -> {x.topic, x.id} end) | |
| 30 | + |> Enum.into(%{}) | |
| 31 | + ) | |
| 32 | + |> assign( | |
| 33 | + :cfg_visions_sel, | |
| 34 | + list_cfg_visions(assigns.current_user.id) | |
| 35 | + |> Enum.map(fn x -> {x.name, x.id} end) | |
| 36 | + |> Enum.into(%{}) | |
| 37 | + ) | |
| 38 | + |> assign( | |
| 39 | + :cfg_foci_sel, | |
| 40 | + list_cfg_foci(assigns.current_user.id) | |
| 41 | + |> Enum.map(fn x -> {x.name, x.id} end) | |
| 42 | + |> Enum.into(%{}) | |
| 43 | + ) | |
| 44 | + } | |
| 45 | + end | |
| 46 | + | |
| 47 | + @impl true | |
| 48 | + def handle_event("validate", %{"pythiae" => pythiae_params}, socket) do | |
| 49 | + pythiae_params = inj_uid(pythiae_params, socket) | |
| 50 | + changeset = | |
| 51 | + socket.assigns.pythiae | |
| 52 | + |> Configuration.change_pythiae(pythiae_params) | |
| 53 | + |> Map.put(:action, :validate) | |
| 54 | + | |
| 55 | + {:noreply, assign(socket, :changeset, changeset)} | |
| 56 | + end | |
| 57 | + | |
| 58 | + def handle_event("save", %{"pythiae" => pythiae_params}, socket) do | |
| 59 | + pythiae_params = inj_uid(pythiae_params, socket) | |
| 60 | + | |
| 61 | + save_pythiae(socket, socket.assigns.action, pythiae_params) | |
| 62 | + end | |
| 63 | + | |
| 64 | + def handle_event("generate-new-name", _params, socket) do | |
| 65 | + changeset = socket.assigns.changeset |> Ecto.Changeset.change(name: gen_name()) | |
| 66 | + {:noreply, assign(socket, :changeset, changeset)} | |
| 67 | + end | |
| 68 | + | |
| 69 | + | |
| 70 | + defp save_pythiae(socket, :edit, pythiae_params) do | |
| 71 | + IO.inspect(pythiae_params) | |
| 72 | + case Configuration.update_pythiae(socket.assigns.pythiae, pythiae_params) do | |
| 73 | + {:ok, _pythiae} -> | |
| 74 | + _pythiae |> IO.inspect | |
| 75 | + {:noreply, | |
| 76 | + socket | |
| 77 | + |> put_flash(:info, "Pythiae updated successfully") | |
| 78 | + |> push_redirect(to: socket.assigns.return_to)} | |
| 79 | + | |
| 80 | + {:error, %Ecto.Changeset{} = changeset} -> | |
| 81 | + {:noreply, assign(socket, :changeset, changeset)} | |
| 82 | + end | |
| 83 | + end | |
| 84 | + | |
| 85 | + defp save_pythiae(socket, :new, pythiae_params) do | |
| 86 | + case Configuration.create_pythiae(pythiae_params) do | |
| 87 | + {:ok, _pythiae} -> | |
| 88 | + {:noreply, | |
| 89 | + socket | |
| 90 | + |> put_flash(:info, "Pythiae created successfully") | |
| 91 | + |> push_redirect(to: socket.assigns.return_to)} | |
| 92 | + | |
| 93 | + {:error, %Ecto.Changeset{} = changeset} -> | |
| 94 | + {:noreply, assign(socket, changeset: changeset)} | |
| 95 | + end | |
| 96 | + end | |
| 97 | + | |
| 98 | + defp list_cfg_ankyra(uid) do | |
| 99 | + Accounts.list_users_rabbit({:user, uid}) | |
| 100 | + end | |
| 101 | + | |
| 102 | + defp list_cfg_visions(uid) do | |
| 103 | + Configuration.list_visions({:user, uid}) | |
| 104 | + end | |
| 105 | + | |
| 106 | + defp list_cfg_foci(uid) do | |
| 107 | + Configuration.list_focis({:user, uid}) | |
| 108 | + end | |
| 109 | +end |
added
apps/room_sanctum/lib/room_sanctum_web/live/cfg_pythiae_live/form_component.html.heex
+56
−0
| 1 | +<div> | |
| 2 | + <h2 class="text-2xl font-bold text-accent"><%= @title %></h2> | |
| 3 | + | |
| 4 | + <.form | |
| 5 | + let={f} | |
| 6 | + for={@changeset} | |
| 7 | + id="pythiae-form" | |
| 8 | + phx-target={@myself} | |
| 9 | + phx-change="validate" | |
| 10 | + phx-submit="save"> | |
| 11 | + | |
| 12 | + <div class="flex"> | |
| 13 | + <div class="w-full"> | |
| 14 | + <div class="form-control"> | |
| 15 | + <%= label f, :name, class: "label" %> | |
| 16 | + <div class="input-group"> | |
| 17 | + <btn class="btn btn-accent" phx-click="generate-new-name" phx-target={@myself}> | |
| 18 | + <i class="fa-solid fa-arrows-rotate"></i> | |
| 19 | + </btn> | |
| 20 | + <%= text_input f, :name, class: "input w-full max-w-md" %> | |
| 21 | + </div> | |
| 22 | + <label class="label"> | |
| 23 | + <span class="label-text-alt text-error"> | |
| 24 | + <%= error_tag f, :name %> | |
| 25 | + </span> | |
| 26 | + </label> | |
| 27 | + </div> | |
| 28 | + <div class="form-control"> | |
| 29 | + <%= label f, :visions %> | |
| 30 | + <%= multiple_select f, :visions, @cfg_visions_sel, class: "select w-full max-w-xs" %> | |
| 31 | + <%= error_tag f, :visions %> | |
| 32 | + </div> | |
| 33 | + <div class="form-control"> | |
| 34 | + <%= label f, :ankyra %> | |
| 35 | + <%= multiple_select f, :ankyra, @cfg_ankyra_sel, class: "select w-full max-w-xs" %> | |
| 36 | + <%= error_tag f, :ankyra %> | |
| 37 | + </div> | |
| 38 | + <div class="form-control"> | |
| 39 | + <%= label f, :curr_vision %> | |
| 40 | + <%= select f, :curr_vision, @cfg_visions_sel, class: "select w-full max-w-xs" %> | |
| 41 | + <%= error_tag f, :curr_vision %> | |
| 42 | + </div> | |
| 43 | + <div class="form-control"> | |
| 44 | + <%= label f, :curr_foci %> | |
| 45 | + <%= select f, :curr_foci, @cfg_foci_sel, class: "select w-full max-w-xs" %> | |
| 46 | + <%= error_tag f, :curr_foci %> | |
| 47 | + </div> | |
| 48 | + | |
| 49 | + </div> | |
| 50 | + </div> | |
| 51 | + | |
| 52 | + <div> | |
| 53 | + <%= submit "Save", phx_disable_with: "Saving...", class: "btn btn-secondary" %> | |
| 54 | + </div> | |
| 55 | + </.form> | |
| 56 | +</div> |
added
apps/room_sanctum/lib/room_sanctum_web/live/cfg_pythiae_live/index.ex
+52
−0
| 1 | +defmodule RoomSanctumWeb.PythiaeLive.Index do | |
| 2 | + use RoomSanctumWeb, :live_view_a | |
| 3 | + | |
| 4 | + alias RoomSanctum.Configuration | |
| 5 | + alias RoomSanctum.Configuration.Pythiae | |
| 6 | + | |
| 7 | + @impl true | |
| 8 | + def mount(_params, _session, socket) do | |
| 9 | + {:ok, assign(socket, :cfg_pythiae, list_cfg_pythiae(socket.assigns.current_user.id)) |> assign(:show_info, false)} | |
| 10 | + end | |
| 11 | + | |
| 12 | + @impl true | |
| 13 | + def handle_params(params, _url, socket) do | |
| 14 | + {:noreply, apply_action(socket, socket.assigns.live_action, params)} | |
| 15 | + end | |
| 16 | + | |
| 17 | + defp apply_action(socket, :edit, %{"id" => id}) do | |
| 18 | + socket | |
| 19 | + |> assign(:page_title, "Edit Pythiae") | |
| 20 | + |> assign(:pythiae, Configuration.get_pythiae!(id)) | |
| 21 | + end | |
| 22 | + | |
| 23 | + defp apply_action(socket, :new, _params) do | |
| 24 | + socket | |
| 25 | + |> assign(:page_title, "New Pythiae") | |
| 26 | + |> assign(:pythiae, %Pythiae{}) | |
| 27 | + end | |
| 28 | + | |
| 29 | + defp apply_action(socket, :index, _params) do | |
| 30 | + socket | |
| 31 | + |> assign(:page_title, "Listing Cfg pythiae") | |
| 32 | + |> assign(:pythiae, nil) | |
| 33 | + end | |
| 34 | + | |
| 35 | + @impl true | |
| 36 | + def handle_event("delete", %{"id" => id}, socket) do | |
| 37 | + pythiae = Configuration.get_pythiae!(id) | |
| 38 | + {:ok, _} = Configuration.delete_pythiae(pythiae) | |
| 39 | + | |
| 40 | + {:noreply, assign(socket, :cfg_pythiae, list_cfg_pythiae(socket.assigns.current_user.id))} | |
| 41 | + end | |
| 42 | + | |
| 43 | + @impl true | |
| 44 | + def handle_event("info", _params, socket) do | |
| 45 | + {:noreply, socket |> assign(:show_info, !socket.assigns.show_info)} | |
| 46 | + end | |
| 47 | + | |
| 48 | + | |
| 49 | + defp list_cfg_pythiae(uid) do | |
| 50 | + Configuration.list_cfg_pythiae({:user, uid}) | |
| 51 | + end | |
| 52 | +end |
added
apps/room_sanctum/lib/room_sanctum_web/live/cfg_pythiae_live/index.html.heex
+60
−0
Click to load diff…
added
apps/room_sanctum/lib/room_sanctum_web/live/cfg_pythiae_live/show.ex
+33
−0
Click to load diff…
added
apps/room_sanctum/lib/room_sanctum_web/live/cfg_pythiae_live/show.html.heex
+85
−0
Click to load diff…
modified
apps/room_sanctum/lib/room_sanctum_web/live/cfg_query_live/show.html.heex
+1
−1
Click to load diff…
modified
apps/room_sanctum/lib/room_sanctum_web/live/live_preview.ex
+21
−2
Click to load diff…
modified
apps/room_sanctum/lib/room_sanctum_web/router.ex
+7
−0
Click to load diff…
modified
apps/room_sanctum/lib/room_sanctum_web/templates/layout/_action_menu.html.heex
+1
−1
Click to load diff…
added
apps/room_sanctum/priv/repo/migrations/20220812054117_create_cfg_pythiae.exs
+20
−0
Click to load diff…
modified
apps/room_sanctum/test/room_sanctum/configuration_test.exs
+56
−0
Click to load diff…
modified
apps/room_sanctum/test/support/fixtures/configuration_fixtures.ex
+15
−0
Click to load diff…
Parents: ddff734