Skip to content

Commit

Permalink
Add rocket states in render and grid views (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivank authored Jan 14, 2024
1 parent 8169bba commit 9d68bc5
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
13 changes: 12 additions & 1 deletion lib/rocketsized/rocket/vehicle.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ defmodule Rocketsized.Rocket.Vehicle do
timestamps()
end

@type state() :: :planned | :in_development | :operational | :retired | :canceled | nil

@type t :: %__MODULE__{
name: String.t(),
source: String.t() | nil,
Expand All @@ -67,13 +69,22 @@ defmodule Rocketsized.Rocket.Vehicle do
native_name: String.t() | nil,
alternative_name: String.t() | nil,
diameter: Float.t() | nil,
state: :planned | :in_development | :operational | :retired | :canceled | nil
state: state()
}

@spec states() :: [state()]
def states() do
@states
end

@spec state_title(state :: state()) :: String.t()
def state_title(:planned), do: "planned"
def state_title(:in_development), do: "development"
def state_title(:operational), do: "operational"
def state_title(:retired), do: "retired"
def state_title(:canceled), do: "canceled"
def state_title(_state), do: nil

@doc false
def changeset(vehicle, attrs) do
vehicle
Expand Down
7 changes: 6 additions & 1 deletion lib/rocketsized_web/components/render_components.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule RocketsizedWeb.RenderComponent do
use Phoenix.Component

alias Rocketsized.Rocket.Vehicle
alias Rocketsized.Rocket.Vehicle.Image
alias Rocketsized.Creator.Country.Flag
import RectLayout
Expand Down Expand Up @@ -134,7 +135,11 @@ defmodule RocketsizedWeb.RenderComponent do
<text
:for={
{subtitle, index} <-
[sprite_content(item).alternative_name, sprite_content(item).native_name]
[
sprite_content(item).alternative_name,
sprite_content(item).native_name,
Vehicle.state_title(sprite_content(item).state)
]
|> Enum.filter(& &1)
|> Enum.with_index()
}
Expand Down
12 changes: 10 additions & 2 deletions lib/rocketsized_web/live/rocketgrid_live/index.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,18 @@
id={id}
style={"--height:#{rocket.height}"}
class={[
"bg-gradient-171 via-gray-50/10 justify-stretch flex flex-col overflow-clip rounded from-gray-100 to-gray-100 px-2 py-2 sm:px-4",
"h-[calc(60vh-theme(space.4))] sm:h-[calc(100vh-theme(space.4))]"
"bg-gradient-171 via-gray-50/10 justify-stretch relative flex flex-col overflow-clip rounded px-2 py-2 sm:px-4",
"h-[calc(60vh-theme(space.4))] sm:h-[calc(100vh-theme(space.4))]",
rocket.state == :planned && "from-gray-100 to-gray-100",
rocket.state == :in_development && "from-sky-50 to-sky-50",
rocket.state == :operational && "from-green-50 to-green-50",
rocket.state == :retired && "from-gray-200 to-gray-200",
rocket.state == :canceled && "from-red-50 to-red-50"
]}
>
<div class="absolute top-2 right-4 text-xs">
<%= Rocketsized.Rocket.Vehicle.state_title(rocket.state) %>
</div>
<div class="flex flex-grow flex-col items-center justify-end">
<a
data-height={rocket.height}
Expand Down
2 changes: 2 additions & 0 deletions test/rocketsized_web/controllers/render_controller_test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule RocketsizedWeb.RenderControllerTest do
alias Rocketsized.Rocket.Vehicle
use RocketsizedWeb.ConnCase, async: true

import Rocketsized.RocketFixtures
Expand Down Expand Up @@ -36,6 +37,7 @@ defmodule RocketsizedWeb.RenderControllerTest do

for vehicle <- vehicles do
assert body =~ vehicle.name
assert body =~ Vehicle.state_title(vehicle.state)
end

assert body =~ "Credit: AttribTest"
Expand Down
2 changes: 2 additions & 0 deletions test/rocketsized_web/live/rocketgrid_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule RocketsizedWeb.RocketgridLiveTest do
import Rocketsized.RocketFixtures
import Rocketsized.CreatorFixtures

alias Rocketsized.Rocket.Vehicle
alias RocketsizedWeb.RocketgridLive.FilterComponent

defp create_vehicle(_) do
Expand Down Expand Up @@ -40,6 +41,7 @@ defmodule RocketsizedWeb.RocketgridLiveTest do
assert html =~ vehicle.native_name
assert html =~ country.name
assert html =~ vehicle.image_meta.attribution
assert html =~ Vehicle.state_title(vehicle.state)
end

for vehicle <- outside do
Expand Down

0 comments on commit 9d68bc5

Please sign in to comment.