Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add shuttle shape page #978

Merged
merged 26 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
4e65dbc
fix: commit CoreComponents from a phoenix 7 new app
meagharty Jun 5, 2024
88f441f
fix: update arrow_web.ex to match new phoenix app, use CoreComponents
meagharty Jun 5, 2024
91222d0
chore: commit cosmetic npm lockfile change
meagharty Jun 5, 2024
a7ee6f2
feat: hook up phoenix 1.7 verified_routes to endpoint.ex
meagharty Jun 5, 2024
0b47d5f
fix: fix icons by adding to static path list
meagharty Jun 5, 2024
73f7d4a
feat: add tailwind css & hero icons for CoreComponents 1.7
meagharty Jun 5, 2024
3eb2ada
fix: format tailwind.config.js with prettier
meagharty Jun 5, 2024
3976c72
fix: add phx.digest output to .gitignore
meagharty Jun 7, 2024
9bb6d02
fix: add tailwind to mix assets.build, assets.deploy
meagharty Jun 6, 2024
e07c972
fix: fix dev config name for watching tailwind changes
meagharty Jun 6, 2024
41e35df
fix: have phoenix serve static files like images, fonts, icons
meagharty Jun 6, 2024
b72717f
fix: remove asset tagged versions of icons
meagharty Jun 7, 2024
926208f
fix: fix path for checkbox.css image
meagharty Jun 7, 2024
9d35758
fix: separate out css from js build, remove tailwind.css separate file
meagharty Jun 6, 2024
ee79d7b
fix: remove @tailwind/base to prevent collision with bootstrap
meagharty Jun 6, 2024
cf7ee90
fix: update esbuild, remove css refs and add empty loader
meagharty Jun 6, 2024
4823396
fix: fix Dockerfile to work with tailwind with Phoenix .heex
meagharty Jun 6, 2024
64d2bc4
feat: commit mix phx.gen.html output
meagharty Jun 5, 2024
c1abdec
feat: commit result of ecto migration
meagharty Jun 5, 2024
4bbb742
fix: rename Shape -> ShapeView, add auth
meagharty Jun 5, 2024
2acaa83
tests: add authentication to controller tests
meagharty Jun 5, 2024
20dba1e
fix: fix credo issues
meagharty Jun 5, 2024
e40d27d
feat: add file upload input, mark required
meagharty Jun 7, 2024
9de3398
tests: add test with upload params set
meagharty Jun 7, 2024
ed336b8
Merge branch 'master' into meag/add-shuttle-shape-page
meagharty Jun 13, 2024
d607eaf
fix: restore new line to priv/repo/structure.sql
meagharty Jun 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions lib/arrow/shuttle.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
defmodule Arrow.Shuttle do
@moduledoc """
The Shuttle context.
"""

import Ecto.Query, warn: false
alias Arrow.Repo

alias Arrow.Shuttle.Shape

@doc """
Returns the list of shapes.

## Examples

iex> list_shapes()
[%Shape{}, ...]

"""
def list_shapes do
Repo.all(Shape)
end

@doc """
Gets a single shape.

Raises `Ecto.NoResultsError` if the Shape does not exist.

## Examples

iex> get_shape!(123)
%Shape{}

iex> get_shape!(456)
** (Ecto.NoResultsError)

"""
def get_shape!(id), do: Repo.get!(Shape, id)

@doc """
Creates a shape.

## Examples

iex> create_shape(%{field: value})
{:ok, %Shape{}}

iex> create_shape(%{field: bad_value})
{:error, %Ecto.Changeset{}}

"""
def create_shape(attrs \\ %{}) do
%Shape{}
|> Shape.changeset(attrs)
|> Repo.insert()
end

@doc """
Updates a shape.

## Examples

iex> update_shape(shape, %{field: new_value})
{:ok, %Shape{}}

iex> update_shape(shape, %{field: bad_value})
{:error, %Ecto.Changeset{}}

"""
def update_shape(%Shape{} = shape, attrs) do
shape
|> Shape.changeset(attrs)
|> Repo.update()
end

@doc """
Deletes a shape.

## Examples

iex> delete_shape(shape)
{:ok, %Shape{}}

iex> delete_shape(shape)
{:error, %Ecto.Changeset{}}

"""
def delete_shape(%Shape{} = shape) do
Repo.delete(shape)
end

@doc """
Returns an `%Ecto.Changeset{}` for tracking shape changes.

## Examples

iex> change_shape(shape)
%Ecto.Changeset{data: %Shape{}}

"""
def change_shape(%Shape{} = shape, attrs \\ %{}) do
Shape.changeset(shape, attrs)
end
end
19 changes: 19 additions & 0 deletions lib/arrow/shuttle/shape.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule Arrow.Shuttle.Shape do
@moduledoc "schema for shuttle shapes"
use Ecto.Schema
import Ecto.Changeset

schema "shapes" do
field :name, :string

timestamps()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
timestamps()
timestamps(type: :utc_datetime)

and migration update.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we had a few dependent PRs (#984 is branched off of this, this PR was branched off another), I moved this into a new PR.

end

@doc false
def changeset(shape, attrs) do
shape
|> cast(attrs, [:name])
|> validate_required([:name])
|> unique_constraint(:name)
end
end
74 changes: 74 additions & 0 deletions lib/arrow_web/controllers/shape_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
defmodule ArrowWeb.ShapeController do
use ArrowWeb, :controller

alias Arrow.Shuttle
alias Arrow.Shuttle.Shape
alias ArrowWeb.Plug.Authorize

plug(Authorize, :view_disruption when action in [:index, :show])
plug(Authorize, :create_disruption when action in [:new, :create])
plug(Authorize, :update_disruption when action in [:edit, :update, :update_row_status])
plug(Authorize, :delete_disruption when action in [:delete])

def index(conn, _params) do
shapes = Shuttle.list_shapes()
render(conn, :index, shapes: shapes)
end

def new(conn, _params) do
changeset = Shuttle.change_shape(%Shape{})
render(conn, :new, changeset: changeset)
end

def create(conn, %{"shape" => shape_params}) do
case Shuttle.create_shape(shape_params) do
{:ok, shape} ->
conn
|> put_flash(
:info,
"Shape created successfully from #{shape_params["filename"].filename}"
)
|> redirect(to: ~p"/shapes/#{shape}")

{:error, %Ecto.Changeset{} = changeset} ->
render(conn, :new, changeset: changeset)
end
end

def show(conn, %{"id" => id}) do
shape = Shuttle.get_shape!(id)
render(conn, :show, shape: shape)
end

def edit(conn, %{"id" => id}) do
shape = Shuttle.get_shape!(id)
changeset = Shuttle.change_shape(shape)
render(conn, :edit, shape: shape, changeset: changeset)
end

def update(conn, %{"id" => id, "shape" => shape_params}) do
shape = Shuttle.get_shape!(id)

case Shuttle.update_shape(shape, shape_params) do
{:ok, shape} ->
conn
|> put_flash(
:info,
"Shape updated successfully from #{shape_params["filename"].filename}"
)
|> redirect(to: ~p"/shapes/#{shape}")

{:error, %Ecto.Changeset{} = changeset} ->
render(conn, :edit, shape: shape, changeset: changeset)
end
end

def delete(conn, %{"id" => id}) do
shape = Shuttle.get_shape!(id)
{:ok, _shape} = Shuttle.delete_shape(shape)

conn
|> put_flash(:info, "Shape deleted successfully.")
|> redirect(to: ~p"/shapes")
end
end
13 changes: 13 additions & 0 deletions lib/arrow_web/controllers/shape_html.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule ArrowWeb.ShapeView do
use ArrowWeb, :html

embed_templates "shape_html/*"

@doc """
Renders a shape form.
"""
attr :changeset, Ecto.Changeset, required: true
attr :action, :string, required: true

def shape_form(assigns)
end
8 changes: 8 additions & 0 deletions lib/arrow_web/controllers/shape_html/edit.html.heex
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<.header>
Edit Shape <%= @shape.id %>
<:subtitle>Use this form to manage shape records in your database.</:subtitle>
</.header>

<.shape_form changeset={@changeset} action={~p"/shapes/#{@shape}"} />

<.back navigate={~p"/shapes"}>Back to shapes</.back>
23 changes: 23 additions & 0 deletions lib/arrow_web/controllers/shape_html/index.html.heex
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<.header>
Listing Shapes
<:actions>
<.link href={~p"/shapes/new"}>
<.button>New Shape</.button>
</.link>
</:actions>
</.header>

<.table id="shapes" rows={@shapes} row_click={&JS.navigate(~p"/shapes/#{&1}")}>
<:col :let={shape} label="Name"><%= shape.name %></:col>
<:action :let={shape}>
<div class="sr-only">
<.link navigate={~p"/shapes/#{shape}"}>Show</.link>
</div>
<.link navigate={~p"/shapes/#{shape}/edit"}>Edit</.link>
</:action>
<:action :let={shape}>
<.link href={~p"/shapes/#{shape}"} method="delete" data-confirm="Are you sure?">
Delete
</.link>
</:action>
</.table>
8 changes: 8 additions & 0 deletions lib/arrow_web/controllers/shape_html/new.html.heex
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<.header>
New Shape
<:subtitle>Use this form to manage shape records in your database.</:subtitle>
</.header>

<.shape_form changeset={@changeset} action={~p"/shapes"} />

<.back navigate={~p"/shapes"}>Back to shapes</.back>
10 changes: 10 additions & 0 deletions lib/arrow_web/controllers/shape_html/shape_form.html.heex
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<.simple_form :let={f} for={@changeset} action={@action} multipart>
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<.error :if={@changeset.action}>
Oops, something went wrong! Please check the errors below.
</.error>
<.input field={f[:filename]} type="file" label="Filename" required="true"/>
<.input field={f[:name]} type="text" label="Name" />
<:actions>
<.button>Save Shape</.button>
</:actions>
</.simple_form>
15 changes: 15 additions & 0 deletions lib/arrow_web/controllers/shape_html/show.html.heex
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<.header>
Shape <%= @shape.id %>
<:subtitle>This is a shape record from your database.</:subtitle>
<:actions>
<.link href={~p"/shapes/#{@shape}/edit"}>
<.button>Edit shape</.button>
</.link>
</:actions>
</.header>

<.list>
<:item title="Name"><%= @shape.name %></:item>
</.list>

<.back navigate={~p"/shapes"}>Back to shapes</.back>
1 change: 1 addition & 0 deletions lib/arrow_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ defmodule ArrowWeb.Router do
resources("/disruptions", DisruptionController, except: [:index])
put("/disruptions/:id/row_status", DisruptionController, :update_row_status)
post("/disruptions/:id/notes", NoteController, :create)
resources("/shapes", ShapeController)
end

scope "/", ArrowWeb do
Expand Down
13 changes: 13 additions & 0 deletions priv/repo/migrations/20240605185923_create_shapes.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule Arrow.Repo.Migrations.CreateShapes do
use Ecto.Migration

def change do
create table(:shapes) do
add :name, :string

timestamps()
end

create unique_index(:shapes, [:name])
end
end
Loading
Loading