Skip to content

Commit

Permalink
refactored to use old syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
JannikStreek committed Sep 17, 2024
1 parent 47c616f commit 9eccc61
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 267 deletions.
82 changes: 0 additions & 82 deletions lib/mindwendel/brainstormings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ defmodule Mindwendel.Brainstormings do
alias Mindwendel.Brainstormings.Lane
alias Mindwendel.Brainstormings.Brainstorming
alias Mindwendel.Brainstormings.BrainstormingModeratingUser
alias Mindwendel.Brainstormings.Lane

require Logger

Expand Down Expand Up @@ -278,85 +277,4 @@ defmodule Mindwendel.Brainstormings do
end

def broadcast({:error, _reason} = error, _event), do: error

@doc """
Gets a single lane.
Raises `Ecto.NoResultsError` if the Lane does not exist.
## Examples
iex> get_lane!(123)
%Lane{}
iex> get_lane!(456)
** (Ecto.NoResultsError)
"""
def get_lane!(id), do: Repo.get!(Lane, id)

@doc """
Creates a lane.
## Examples
iex> create_lane(%{field: value})
{:ok, %Lane{}}
iex> create_lane(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_lane(attrs \\ %{}) do
%Lane{}
|> Lane.changeset(attrs)
|> Repo.insert()
end

@doc """
Updates a lane.
## Examples
iex> update_lane(lane, %{field: new_value})
{:ok, %Lane{}}
iex> update_lane(lane, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_lane(%Lane{} = lane, attrs) do
lane
|> Lane.changeset(attrs)
|> Repo.update()
end

@doc """
Deletes a lane.
## Examples
iex> delete_lane(lane)
{:ok, %Lane{}}
iex> delete_lane(lane)
{:error, %Ecto.Changeset{}}
"""
def delete_lane(%Lane{} = lane) do
Repo.delete(lane)
end

@doc """
Returns an `%Ecto.Changeset{}` for tracking lane changes.
## Examples
iex> change_lane(lane)
%Ecto.Changeset{data: %Lane{}}
"""
def change_lane(%Lane{} = lane, attrs \\ %{}) do
Lane.changeset(lane, attrs)
end
end
1 change: 1 addition & 0 deletions lib/mindwendel_web/live/brainstorming_live/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule MindwendelWeb.BrainstormingLive.Show do
alias Mindwendel.Brainstormings
alias Mindwendel.Ideas
alias Mindwendel.Brainstormings.Idea
alias Mindwendel.Brainstormings.Lane

@impl true
def mount(%{"id" => id}, session, socket) do
Expand Down
11 changes: 11 additions & 0 deletions lib/mindwendel_web/live/brainstorming_live/show.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@
) %>
<% end %>

<%= if @live_action in [:edit_lane] do %>
<%= live_modal(MindwendelWeb.LaneLive.FormComponent,
id: :update,
title: gettext("Update lane"),
action: :update,
brainstorming: @brainstorming,
lane: @lane,
return_to: Routes.brainstorming_show_path(@socket, :show, @brainstorming)
) %>
<% end %>

<%= if @live_action in [:share] do %>
<%= live_modal(MindwendelWeb.BrainstormingLive.ShareComponent,
id: :share,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div id="ideas" class="d-flex overflow-scroll flex-row">
<%= for lane <- @lanes do %>
<div>
<%= lane.name || "Default Lane" %>
<%= lane.name || "Default Lane" %>
<div
id="ideas-col-1"
class="mb-5 d-flex card-columns flex-column flex-wrap sortable"
Expand Down
84 changes: 35 additions & 49 deletions lib/mindwendel_web/live/lane_live/form_component.ex
Original file line number Diff line number Diff line change
@@ -1,82 +1,68 @@
defmodule MindwendelWeb.LaneLive.FormComponent do
use MindwendelWeb, :live_component

alias Mindwendel.Brainstormings

@impl true
def render(assigns) do
~H"""
<div>
<.header>
<%= @title %>
<:subtitle>Use this form to manage lane records in your database.</:subtitle>
</.header>
<.simple_form
for={@form}
id="lane-form"
phx-target={@myself}
phx-change="validate"
phx-submit="save"
>
<.input field={@form[:name]} type="text" label="Name" />
<:actions>
<.button phx-disable-with="Saving...">Save Lane</.button>
</:actions>
</.simple_form>
</div>
"""
end
alias Mindwendel.Lanes

@impl true
def update(%{lane: lane} = assigns, socket) do
changeset = Lanes.change_lane(lane)

{:ok,
socket
|> assign(assigns)
|> assign_new(:form, fn ->
to_form(Brainstormings.change_lane(lane))
end)}
|> assign(:changeset, changeset)}
end

@impl true
def handle_event("validate", %{"lane" => lane_params}, socket) do
changeset = Brainstormings.change_lane(socket.assigns.lane, lane_params)
{:noreply, assign(socket, form: to_form(changeset, action: :validate))}
changeset =
socket.assigns.lane
|> Lanes.change_lane(lane_params)
|> Map.put(:action, :validate)

{:noreply, assign(socket, :changeset, changeset)}
end

def handle_event("save", %{"lane" => lane_params}, socket) do
save_lane(socket, socket.assigns.action, lane_params)
end

defp save_lane(socket, :edit, lane_params) do
case Brainstormings.update_lane(socket.assigns.lane, lane_params) do
{:ok, lane} ->
notify_parent({:saved, lane})
defp save_lane(socket, :update, lane_params) do
lane = Lanes.get_lane!(lane_params["id"])

{:noreply,
socket
|> put_flash(:info, "Lane updated successfully")
|> push_patch(to: socket.assigns.patch)}
%{current_user: current_user, brainstorming: brainstorming} = socket.assigns

{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, form: to_form(changeset))}
if current_user.id in [lane.user_id | brainstorming.moderating_users |> Enum.map(& &1.id)] do
case Lanes.update_lane(
lane,
Map.put(lane_params, "user_id", lane.user_id || current_user.id)
) do
{:ok, _lane} ->
{:noreply,
socket
|> put_flash(:info, gettext("Lane created updated"))
|> push_redirect(to: socket.assigns.return_to)}

{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, changeset: changeset)}
end
end
end

defp save_lane(socket, :new, lane_params) do
case Brainstormings.create_lane(lane_params) do
{:ok, lane} ->
notify_parent({:saved, lane})
Mindwendel.Accounts.update_user(socket.assigns.current_user, %{
username: lane_params["username"]
})

case Lanes.create_lane(Map.put(lane_params, "user_id", socket.assigns.current_user.id)) do
{:ok, _lane} ->
{:noreply,
socket
|> put_flash(:info, "Lane created successfully")
|> push_patch(to: socket.assigns.patch)}
|> put_flash(:info, gettext("Lane created successfully"))
|> push_redirect(to: socket.assigns.return_to)}

{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, form: to_form(changeset))}
{:noreply, assign(socket, changeset: changeset)}
end
end

defp notify_parent(msg), do: send(self(), {__MODULE__, msg})
end
47 changes: 0 additions & 47 deletions lib/mindwendel_web/live/lane_live/index.ex

This file was deleted.

41 changes: 0 additions & 41 deletions lib/mindwendel_web/live/lane_live/index.html.heex

This file was deleted.

21 changes: 0 additions & 21 deletions lib/mindwendel_web/live/lane_live/show.ex

This file was deleted.

26 changes: 0 additions & 26 deletions lib/mindwendel_web/live/lane_live/show.html.heex

This file was deleted.

0 comments on commit 9eccc61

Please sign in to comment.