Skip to content

Commit

Permalink
Merge pull request #28 from camino-school/manage_strands
Browse files Browse the repository at this point in the history
Manage strands
  • Loading branch information
endoooo authored Dec 1, 2023
2 parents 2ad0373 + 5da9683 commit d8f777b
Show file tree
Hide file tree
Showing 49 changed files with 1,575 additions and 666 deletions.
16 changes: 12 additions & 4 deletions lib/lanttern/assessments.ex
Original file line number Diff line number Diff line change
Expand Up @@ -663,12 +663,20 @@ defmodule Lanttern.Assessments do
|> Ecto.Multi.run(
:link_activity,
fn _repo, %{insert_assessment_point: assessment_point} ->
position =
from(aap in ActivityAssessmentPoint,
positions =
from(
aap in ActivityAssessmentPoint,
where: aap.activity_id == ^activity_id,
select: count()
select: aap.position,
order_by: [desc: aap.position]
)
|> Repo.one()
|> Repo.all()

position =
case Enum.at(positions, 0) do
nil -> 0
pos -> pos + 1
end

%ActivityAssessmentPoint{}
|> ActivityAssessmentPoint.changeset(%{
Expand Down
2 changes: 1 addition & 1 deletion lib/lanttern/assessments/assessment_point.ex
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ defmodule Lanttern.Assessments.AssessmentPoint do
|> foreign_key_constraint(
:id,
name: :assessment_point_entries_assessment_point_id_fkey,
message: "This jfalskflsealfesal."
message: "Assessment point has linked entries."
)
end
end
7 changes: 6 additions & 1 deletion lib/lanttern/curricula.ex
Original file line number Diff line number Diff line change
Expand Up @@ -237,20 +237,25 @@ defmodule Lanttern.Curricula do
@doc """
Returns the list of curriculum items linked to the given strand.
## Options:
- `:preloads` – preloads associated data
## Examples
iex> list_strand_curriculum_items(1)
[%CurriculumItem{}, ...]
"""
def list_strand_curriculum_items(strand_id) do
def list_strand_curriculum_items(strand_id, opts \\ []) do
from(
ci in CurriculumItem,
join: sci in assoc(ci, :strand_links),
where: sci.strand_id == ^strand_id,
order_by: sci.position
)
|> Repo.all()
|> maybe_preload(opts)
end

@doc """
Expand Down
41 changes: 39 additions & 2 deletions lib/lanttern/learning_context.ex
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ defmodule Lanttern.LearningContext do
"""
def delete_strand(%Strand{} = strand) do
Repo.delete(strand)
strand
|> Strand.delete_changeset()
|> Repo.delete()
end

@doc """
Expand Down Expand Up @@ -212,12 +214,45 @@ defmodule Lanttern.LearningContext do
"""
def create_activity(attrs \\ %{}, opts \\ []) do
attrs = set_activity_position_attr(attrs)

%Activity{}
|> Activity.changeset(attrs)
|> Repo.insert()
|> maybe_preload(opts)
end

defp set_activity_position_attr(%{"position" => _} = attrs), do: attrs

defp set_activity_position_attr(%{position: _} = attrs), do: attrs

defp set_activity_position_attr(attrs) do
strand_id = attrs[:strand_id] || attrs["strand_id"]

positions =
from(
a in Activity,
where: a.strand_id == ^strand_id,
select: a.position,
order_by: [desc: a.position]
)
|> Repo.all()

position =
case Enum.at(positions, 0) do
nil -> 0
pos -> pos + 1
end

cond do
not is_nil(attrs[:strand_id]) ->
Map.put(attrs, :position, position)

not is_nil(attrs["strand_id"]) ->
Map.put(attrs, "position", position)
end
end

@doc """
Updates a activity.
Expand Down Expand Up @@ -254,7 +289,9 @@ defmodule Lanttern.LearningContext do
"""
def delete_activity(%Activity{} = activity) do
Repo.delete(activity)
activity
|> Activity.delete_changeset()
|> Repo.delete()
end

@doc """
Expand Down
10 changes: 10 additions & 0 deletions lib/lanttern/learning_context/activity.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,14 @@ defmodule Lanttern.LearningContext.Activity do
|> validate_required([:name, :description, :position, :strand_id])
|> put_subjects()
end

def delete_changeset(activity) do
activity
|> cast(%{}, [])
|> foreign_key_constraint(
:id,
name: :activities_assessment_points_activity_id_fkey,
message: "Activity has linked assessment points."
)
end
end
12 changes: 12 additions & 0 deletions lib/lanttern/learning_context/strand.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ defmodule Lanttern.LearningContext.Strand do
schema "strands" do
field :name, :string
field :description, :string
field :subject_id, :id, virtual: true
field :subjects_ids, {:array, :id}, virtual: true
field :year_id, :id, virtual: true
field :years_ids, {:array, :id}, virtual: true

has_many :curriculum_items, Lanttern.Curricula.StrandCurriculumItem,
Expand All @@ -34,4 +36,14 @@ defmodule Lanttern.LearningContext.Strand do
|> put_subjects()
|> put_years()
end

def delete_changeset(strand) do
strand
|> cast(%{}, [])
|> foreign_key_constraint(
:id,
name: :activities_strand_id_fkey,
message: "Strand has linked activities."
)
end
end
21 changes: 21 additions & 0 deletions lib/lanttern/taxonomy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ defmodule Lanttern.Taxonomy do
alias Lanttern.Taxonomy.Subject
alias Lanttern.Taxonomy.Year

alias Lanttern.LearningContext.Strand

@years [
{"k0", "Kindergarten 0"},
{"k1", "Kindergarten 1"},
Expand Down Expand Up @@ -147,6 +149,25 @@ defmodule Lanttern.Taxonomy do
}
end

@doc """
Returns the list of strand subjects ordered alphabetically.
## Examples
iex> list_strand_subjects(1)
{[%Subject{}, ...], [%Subject{}, ...]}
"""
def list_strand_subjects(strand_id) do
from(st in Strand,
join: sub in assoc(st, :subjects),
where: st.id == ^strand_id,
order_by: sub.name,
select: sub
)
|> Repo.all()
end

@doc """
Gets a single subject.
Expand Down
2 changes: 2 additions & 0 deletions lib/lanttern_web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ defmodule LantternWeb do
quote do
use Phoenix.LiveComponent

import LantternWeb.LiveComponentHelpers

unquote(html_helpers())
end
end
Expand Down
54 changes: 49 additions & 5 deletions lib/lanttern_web/components/core_components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ defmodule LantternWeb.CoreComponents do
attr :type, :string, default: nil
attr :class, :any, default: nil
attr :theme, :string, default: "default", doc: "default | ghost"
attr :size, :string, default: "normal", doc: "sm | normal"
attr :rounded, :boolean, default: false
attr :icon_name, :string, default: nil
attr :rest, :global, include: ~w(disabled form name value)

Expand All @@ -166,7 +168,7 @@ defmodule LantternWeb.CoreComponents do
<button
type={@type}
class={[
get_button_styles(@theme),
get_button_styles(@theme, @size, @rounded),
@class
]}
{@rest}
Expand All @@ -186,22 +188,26 @@ defmodule LantternWeb.CoreComponents do
<.icon_button name="hero-x-mark" />
"""
attr :type, :string, default: nil
attr :type, :string, default: "button"
attr :class, :any, default: nil
attr :theme, :string, default: "default", doc: "default | ghost"
attr :size, :string, default: "normal", doc: "sm | normal"
attr :rounded, :boolean, default: false
attr :name, :string, required: true
attr :sr_text, :string, required: true
attr :rest, :global, include: ~w(disabled form name value)

def icon_button(assigns) do
~H"""
<button
type={@type}
class={[
get_button_styles(@theme),
get_button_styles(@theme, @size, @rounded),
@class
]}
{@rest}
>
<span class="sr-only"><%= @sr_text %></span>
<.icon name={@name} class="w-5 h-5" />
</button>
"""
Expand All @@ -216,9 +222,11 @@ defmodule LantternWeb.CoreComponents do
<.link patch={~p"/somepath"} class={[get_button_styles()]}>Link</.link>
"""
def get_button_styles(theme \\ "default") do
def get_button_styles(theme \\ "default", size \\ "normal", rounded \\ false) do
[
"inline-flex items-center gap-1 rounded-sm py-2 px-2 font-display text-sm font-bold",
"inline-flex items-center gap-1 font-display text-sm font-bold",
if(size == "sm", do: "p-1", else: "p-2"),
if(rounded, do: "rounded-full", else: "rounded-sm"),
"phx-submit-loading:opacity-50 phx-click-loading:opacity-50 phx-click-loading:pointer-events-none",
button_theme(theme)
]
Expand Down Expand Up @@ -926,6 +934,42 @@ defmodule LantternWeb.CoreComponents do
"""
end

@doc """
Renders a `<button>` or `<.link>` with icon.
Usually used in the context of a collection (e.g. to add a new item to a list).
"""

attr :class, :any, default: nil
attr :type, :string, required: true, doc: "link | button"
attr :icon_name, :string, default: nil
attr :patch, :string, doc: "use with type=\"link\""
attr :rest, :global

slot :inner_block, required: true

def collection_action(%{type: "button"} = assigns) do
~H"""
<button type="button" class={[collection_action_styles(), @class]} {@rest}>
<%= render_slot(@inner_block) %>
<.icon :if={@icon_name} name={@icon_name} class="w-6 h-6 text-ltrn-primary" />
</button>
"""
end

def collection_action(%{type: "link"} = assigns) do
~H"""
<.link patch={@patch} class={[collection_action_styles(), @class]}>
<%= render_slot(@inner_block) %>
<.icon :if={@icon_name} name={@icon_name} class="w-6 h-6 text-ltrn-primary" />
</.link>
"""
end

defp collection_action_styles(),
do:
"shrink-0 flex items-center gap-2 font-display text-sm text-ltrn-dark hover:text-ltrn-subtle"

@doc """
Highlights entring (mounting) elements in DOM.
Expand Down
5 changes: 4 additions & 1 deletion lib/lanttern_web/components/form_components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ defmodule LantternWeb.FormComponents do
~H"""
<div phx-feedback-for={@name} class={@class}>
<.label
:if={@label || @custom_label != []}
for={@id}
show_optional={@show_optional}
custom={if @custom_label == [], do: false, else: true}
Expand Down Expand Up @@ -201,6 +202,7 @@ defmodule LantternWeb.FormComponents do
~H"""
<div phx-feedback-for={@name} class={@class}>
<.label
:if={@label || @custom_label != []}
for={@id}
show_optional={@show_optional}
custom={if @custom_label == [], do: false, else: true}
Expand All @@ -218,6 +220,7 @@ defmodule LantternWeb.FormComponents do
~H"""
<div phx-feedback-for={@name} class={@class}>
<.label
:if={@label || @custom_label != []}
for={@id}
show_optional={@show_optional}
custom={if @custom_label == [], do: false, else: true}
Expand Down Expand Up @@ -315,7 +318,7 @@ defmodule LantternWeb.FormComponents do
id={@id}
name={@name}
class={[
"block w-full min-h-[6rem] rounded-sm border-0 shadow-sm ring-1 sm:text-sm sm:leading-6",
"block w-full min-h-[10rem] rounded-sm border-0 shadow-sm ring-1 sm:text-sm sm:leading-6",
"focus:ring-2 focus:ring-inset",
"phx-no-feedback:ring-ltrn-lighter phx-no-feedback:focus:ring-ltrn-primary",
@errors == [] && "ring-ltrn-lighter focus:ring-ltrn-primary",
Expand Down
1 change: 0 additions & 1 deletion lib/lanttern_web/components/overlay_components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,6 @@ defmodule LantternWeb.OverlayComponents do

def menu_button_item(assigns) do
~H"""
<!-- Active: "bg-gray-50", Not Active: "" -->
<button
id={@id}
type="button"
Expand Down
Loading

0 comments on commit d8f777b

Please sign in to comment.