Skip to content

Commit

Permalink
Support sheaf creation in discuss mode
Browse files Browse the repository at this point in the history
minor changes
  • Loading branch information
rtshkmr committed Nov 17, 2024
1 parent b444b43 commit c37d48d
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 9 deletions.
89 changes: 84 additions & 5 deletions lib/vyasa_web/components/contexts/discuss.ex
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,21 @@ defmodule VyasaWeb.Context.Discuss do
)
end

@doc """
For a particular sheaf, re-registers it by deleting its old entry in the lattice and inserting the new entry.
This is useful for manipulating the lattices in events such as promoting a sheaf from draft to published.
"""
def reregister_sheaf(
%Socket{} = socket,
%Sheaf{id: old_id} = old_sheaf,
%Sheaf{id: new_id} = new_sheaf
)
when old_id == new_id do
socket
|> deregister_sheaf(old_sheaf)
|> register_sheaf(new_sheaf)
end

def deregister_sheaf(
%Socket{
assigns: %{
Expand Down Expand Up @@ -490,7 +505,7 @@ defmodule VyasaWeb.Context.Discuss do
• we don’t want to persist the reply_to in the db (by updating the
current active draft sheaf).
• we won’t do an update query on the draft_sheaf form the
sheaf::quick_reply
sheaf::toggle_quick_reply
• we will only change the reply_to_path that is kept in state
within the discuss context
• the user should expect that since this is a quick reply, this
Expand All @@ -501,7 +516,7 @@ defmodule VyasaWeb.Context.Discuss do
depth, and desires to go and gather marks (e.g. by clicking some
kind of “nav to read mode”), then this transient case will change
to case 2, permanent.
^ this event is will be different from the sheaf::quick_reply event though
^ this event is will be different from the sheaf::toggle_quick_reply event though
2. if “permanent/intentional”
• we update both the reply_to_path in the local socket state as
Expand All @@ -511,7 +526,7 @@ defmodule VyasaWeb.Context.Discuss do
1.1 Conclusions:
────────────────
1. `sheaf::quick_reply' will not do any db write to the current draft
1. `sheaf::toggle_quick_reply' will not do any db write to the current draft
sheaf’s parent field. Will only update the local state (for
`reply_to_path')
2. `sheaf::publish' will be updated so that regardless of the upstream
Expand All @@ -520,7 +535,7 @@ defmodule VyasaWeb.Context.Discuss do
current draft sheaf no matter what.
"""
def handle_event(
"sheaf::quick_reply",
"sheaf::toggle_quick_reply",
%{
"sheaf_path_labels" => immediate_reply_to_sheaf_labels
} = _params,
Expand Down Expand Up @@ -680,6 +695,70 @@ defmodule VyasaWeb.Context.Discuss do
}
end

def handle_event(
"sheaf::publish",
%{
"body" => body,
"is_private" => _is_private
} = _params,
%Socket{
assigns: %{
session: %VyasaWeb.Session{
name: username,
sangh: %Vyasa.Sangh.Session{
id: sangh_id
}
},
draft_reflector_path: %Ltree{
labels: draft_sheaf_lattice_key
},
reply_to_path: %Ltree{
labels: reply_to_latice_key
},
sheaf_lattice: %{} = sheaf_lattice,
sheaf_ui_lattice: %{} = sheaf_ui_lattice
}
} = socket
)
when is_binary(body) do
reply_to_sheaf = sheaf_lattice[reply_to_latice_key]
draft_sheaf = sheaf_lattice[draft_sheaf_lattice_key]

payload_precursor = %{
body: body,
traits: ["published"],
signature: username
}

update_payload =
case(is_nil(draft_sheaf)) do
true ->
payload_precursor

false ->
Map.put(payload_precursor, :parent, reply_to_sheaf)
end

{:ok, updated_sheaf} = Vyasa.Sangh.update_sheaf(draft_sheaf, update_payload)

%Sheaf{
path: %Ltree{} = new_draft_reflector_path
} = new_draft_reflector = Sheaf.draft!(sangh_id)

{
:noreply,
socket
|> assign(
sheaf_ui_lattice:
sheaf_ui_lattice |> SheafLattice.toggle_show_sheaf_modal?(draft_sheaf_lattice_key)
)
|> reregister_sheaf(draft_sheaf, updated_sheaf)
|> register_sheaf(new_draft_reflector)
|> assign(draft_reflector_path: new_draft_reflector_path)
|> assign(reply_to_path: nil)
}
end

@impl true
# TODO @ks0m1c this is an example of what binding/permalinking should handle
# we need to do a push-patch direction from this function
Expand Down Expand Up @@ -867,7 +946,7 @@ defmodule VyasaWeb.Context.Discuss do
sheaf_lattice={@sheaf_lattice}
sheaf_ui_lattice={@sheaf_ui_lattice}
on_replies_click={JS.push("ui::toggle_sheaf_is_expanded?", target: "#content-display")}
on_quick_reply={JS.push("sheaf::quick_reply", target: "#content-display")}
on_quick_reply={JS.push("sheaf::toggle_quick_reply", target: "#content-display")}
on_set_reply_to={JS.push("sheaf::set_reply_to_context", target: "#content-display")}
/>
<!-- <.sheaf_summary sheaf={root_sheaf} /> -->
Expand Down
8 changes: 4 additions & 4 deletions lib/vyasa_web/components/contexts/discuss/sheaf_tree.ex
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,12 @@ defmodule VyasaWeb.Context.Discuss.SheafTree do
class={["border-l-2 border-gray-200", @container_class]}
id={"collapsible-sheaf-container-" <> @id}
>
<.debug_dump
<!-- <.debug_dump
label="collapsible sheaf container"
id={@id}
level={@level}
num_children={@sheafs |> Enum.count()}
/>
/> -->
<!-- Non-Collapsible View -->
<%= if is_nil(@sheafs) or !@sheafs or Enum.empty?(@sheafs) do %>
<p class="text-gray-500">No child sheafs available.</p>
Expand Down Expand Up @@ -256,13 +256,13 @@ defmodule VyasaWeb.Context.Discuss.SheafTree do
id={"level" <> to_string(@level) <> "-sheaf-component_container-" <> @id}
class="flex flex-col"
>
<.debug_dump
<!-- <.debug_dump
id={@id}
label="Sheaf Component"
sheaf_path={@sheaf.path}
level={@level}
num_children={@children |> Enum.count()}
/>
/> -->
<.sheaf_summary
id={"sheaf-tree-node-sheaf-summary-"<> @id}
level={@level}
Expand Down

0 comments on commit c37d48d

Please sign in to comment.