diff --git a/assets/css/app.css b/assets/css/app.css index 1d9f5a8..a7209a8 100644 --- a/assets/css/app.css +++ b/assets/css/app.css @@ -91,12 +91,10 @@ transition: opacity 1s ease-out; } -.phx-disconnected{ +.phx-loading{ cursor: wait; } -.phx-disconnected *{ - pointer-events: none; -} + .phx-modal { opacity: 1!important; diff --git a/lib/live_beats/media_library.ex b/lib/live_beats/media_library.ex new file mode 100644 index 0000000..9ea7532 --- /dev/null +++ b/lib/live_beats/media_library.ex @@ -0,0 +1,46 @@ +defmodule LiveBeats.MediaLibrary do + @moduledoc """ + The MediaLibrary context. + """ + + import Ecto.Query, warn: false + alias LiveBeats.Repo + + alias LiveBeats.MediaLibrary.{Song, Genre} + + def create_genre(attrs \\ %{}) do + %Genre{} + |> Genre.changeset(attrs) + |> Repo.insert() + end + + def list_genres do + Repo.all(Genre, order_by: [asc: :title]) + end + + def list_songs do + Repo.all(Song) + end + + def get_song!(id), do: Repo.get!(Song, id) + + def create_song(attrs \\ %{}) do + %Song{} + |> Song.changeset(attrs) + |> Repo.insert() + end + + def update_song(%Song{} = song, attrs) do + song + |> Song.changeset(attrs) + |> Repo.update() + end + + def delete_song(%Song{} = song) do + Repo.delete(song) + end + + def change_song(%Song{} = song, attrs \\ %{}) do + Song.changeset(song, attrs) + end +end diff --git a/lib/live_beats/media_library/genre.ex b/lib/live_beats/media_library/genre.ex new file mode 100644 index 0000000..bb15a61 --- /dev/null +++ b/lib/live_beats/media_library/genre.ex @@ -0,0 +1,26 @@ +defmodule LiveBeats.MediaLibrary.Genre do + use Ecto.Schema + import Ecto.Changeset + + schema "genres" do + field :title, :string + field :slug, :string + end + + @doc false + def changeset(song, attrs) do + song + |> cast(attrs, [:title]) + |> validate_required([:title]) + |> put_slug() + end + + defp put_slug(%Ecto.Changeset{valid?: false} = changeset), do: changeset + defp put_slug(%Ecto.Changeset{valid?: true} = changeset) do + if title = get_change(changeset, :title) do + put_change(changeset, :slug, Phoenix.Naming.underscore(title)) + else + changeset + end + end +end diff --git a/lib/live_beats/media_library/song.ex b/lib/live_beats/media_library/song.ex new file mode 100644 index 0000000..57f7e5c --- /dev/null +++ b/lib/live_beats/media_library/song.ex @@ -0,0 +1,24 @@ +defmodule LiveBeats.MediaLibrary.Song do + use Ecto.Schema + import Ecto.Changeset + + schema "songs" do + field :album_artist, :string + field :artist, :string + field :date_recorded, :naive_datetime + field :date_released, :naive_datetime + field :duration, :integer + field :title, :string + belongs_to :user, LiveBeats.Accounts.User + belongs_to :genre, LiveBeats.MediaLibrary.Genre + + timestamps() + end + + @doc false + def changeset(song, attrs) do + song + |> cast(attrs, [:album_artist, :artist, :duration, :title, :date_recorded, :date_released]) + |> validate_required([:artist, :duration, :title]) + end +end diff --git a/lib/live_beats_web.ex b/lib/live_beats_web.ex index 74d26a2..e41e284 100644 --- a/lib/live_beats_web.ex +++ b/lib/live_beats_web.ex @@ -91,6 +91,7 @@ defmodule LiveBeatsWeb do import LiveBeatsWeb.LiveHelpers import LiveBeatsWeb.Gettext alias LiveBeatsWeb.Router.Helpers, as: Routes + alias Phoenix.LiveView.JS end end diff --git a/lib/live_beats_web/controllers/user_auth.ex b/lib/live_beats_web/controllers/user_auth.ex index 780b895..92d46ed 100644 --- a/lib/live_beats_web/controllers/user_auth.ex +++ b/lib/live_beats_web/controllers/user_auth.ex @@ -6,8 +6,9 @@ defmodule LiveBeatsWeb.UserAuth do alias LiveBeats.Accounts alias LiveBeatsWeb.Router.Helpers, as: Routes - def on_mount(:default, _params, session, socket) do + def on_mount(:current_user, _params, session, socket) do socket = LiveView.assign(socket, :nonce, Map.fetch!(session, "nonce")) + case session do %{"user_id" => user_id} -> {:cont, LiveView.assign_new(socket, :current_user, fn -> Accounts.get_user!(user_id) end)} @@ -17,6 +18,19 @@ defmodule LiveBeatsWeb.UserAuth do end end + def on_mount(:ensure_authenticated, _params, session, socket) do + case session do + %{"user_id" => user_id} -> + {:cont, LiveView.assign_new(socket, :current_user, fn -> Accounts.get_user!(user_id) end)} + + %{} -> + {:halt, + socket + |> LiveView.put_flash(:error, "Please sign in") + |> LiveView.redirect(to: Routes.sign_in_path(socket, :index))} + end + end + @doc """ Logs the user in. @@ -102,6 +116,7 @@ defmodule LiveBeatsWeb.UserAuth do def require_authenticated_admin(conn, _opts) do user = conn.assigns[:current_user] + if user && LiveBeats.Accounts.admin?(user) do assign(conn, :current_admin, user) else diff --git a/lib/live_beats_web/live/home_live.ex b/lib/live_beats_web/live/home_live.ex index 2d6bf5e..19ea926 100644 --- a/lib/live_beats_web/live/home_live.ex +++ b/lib/live_beats_web/live/home_live.ex @@ -1,14 +1,19 @@ defmodule LiveBeatsWeb.HomeLive do use LiveBeatsWeb, :live_view + alias LiveBeats.MediaLibrary + def render(assigns) do ~H""" <.title_bar> LiveBeats - Chill - <:action>Share - <:action primary phx-click={show_modal("add-songs")}>Add Songs + <:actions> + <.button>Share + <.button primary phx-click={show_modal("add-songs")}>Add Songs + + <.modal id="add-songs"> <:title>Add Songs a modal @@ -321,164 +326,26 @@ defmodule LiveBeatsWeb.HomeLive do -
- Nextup - | -- likes - | -- user - | -
---|---|---|
- - | -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- +8
- |
- - March 17, 2020 - | -
- - | -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- +4
- |
- - April 4, 2020 - | -
- - | -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- +10
- |
- - March 30, 2020 - | -
+ <%= col.label %> + | + <% end %> +
---|
+
+ <%= if i == 0 do %>
+
+ <% end %>
+ <%= render_slot(col, row) %>
+
+ |
+ <% end %>
+